/
one.go
139 lines (106 loc) · 2.51 KB
/
one.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"fmt"
"strconv"
"github.com/bradfitz/gomemcache/memcache"
"github.com/gunjan5/memflash/memflash"
// "database/sql"
//_ "github.com/lib/pq"
"log"
"time"
"github.com/quipo/statsd"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
const (
DB_USER = "admin"
DB_PASSWORD = "mypassword"
DB_NAME = "test"
DB_HOST = "192.168.99.100"
MEM_IP = "192.168.99.100"
MEM_PORT = ":11211"
MONGO_IP = "192.168.99.100"
MONGO_PORT = ":27017"
STATSD_IP = "192.168.99.100"
STATSD_PORT = ":8125"
TIMEOUT = 100
)
type Person struct {
Index string
Data string
}
func main() {
in := ""
prefix := "MemFlash."
stats := statsd.NewStatsdClient(STATSD_IP+STATSD_PORT, prefix)
err := stats.CreateSocket()
check(err)
//stats:=statsd.NewStatsdBuffer(1*time.Second, statsdclient)
//mc := memcache.New("192.168.99.100:11211")
//fmt.Printf("mc: %T\n", mc)
mydb := memflash.DB{MEM_IP + MEM_PORT, "", nil}
ref := mydb.New()
session, err := mgo.Dial(MONGO_IP + MONGO_PORT)
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
c := session.DB("test").C("people")
result := Person{}
// s1 := rand.NewSource(time.Now().UnixNano())
// r1 := rand.New(s1)
start := time.Now()
for i := 0; i < 10000; i++ {
memCh := make(chan bool)
mongoCh := make(chan bool)
timeout := make(chan bool, 1)
fmt.Println("Iteration: ", i)
//time.Sleep(5*time.Millisecond)
start1 := time.Now()
go func() {
time.Sleep(TIMEOUT * time.Millisecond)
timeout <- true
}()
go func(i int) {
item, err := ref.Get(strconv.Itoa(i))
if err == memcache.ErrCacheMiss {
//Cache MISS
}
if item != nil {
//HIT
memCh <- true
}
}(i)
go func(i int) {
err = c.Find(bson.M{"index": strconv.Itoa(i)}).One(&result)
check(err)
//time.Sleep(time.Duration(r1.Intn(5)) * time.Millisecond)
mongoCh <- true
}(i)
select {
case <-memCh:
fmt.Println("Memcache WINS")
fmt.Printf("*** time %s \n ", time.Since(start1))
stats.Gauge("p1", int64(time.Since(start1)))
<-timeout
case <-mongoCh:
fmt.Println("MONGO wins")
fmt.Printf("*** time %s \n ", time.Since(start1))
stats.Gauge("p1", int64(time.Since(start1)))
fmt.Println(int64(time.Since(start1)))
<-timeout
case <-timeout:
fmt.Println("Slow connection")
continue
}
}
fmt.Printf("****** Executed in time %s \n ", time.Since(start))
fmt.Scanf("%s", &in)
}
func check(err error) {
if err != nil {
//panic(err)
log.Fatalf("ERROR: %s", err)
}
}