/
original.go
140 lines (107 loc) · 2.72 KB
/
original.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
140
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"
)
type Person struct {
Index string
Data string
}
func main() {
in := ""
memCh := make(chan bool)
mongoCh := make(chan bool)
timeout := make(chan bool, 1)
prefix := "MemFlash."
statsdclient := statsd.NewStatsdClient(STATSD_IP+STATSD_PORT, prefix)
err := statsdclient.CreateSocket()
check(err)
//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{}
start := time.Now()
for i := 0; i < 10000; i++ {
time.Sleep(10 * time.Millisecond)
start1 := time.Now()
go func() {
time.Sleep(3 * time.Second)
timeout <- true
}()
go func(i int) {
item, err := ref.Get(strconv.Itoa(i))
if err == memcache.ErrCacheMiss {
//fmt.Println("CACHE MISS!!!!!")
// err = c.Find(bson.M{"index": strconv.Itoa(i)}).One(&result)
// //fmt.Println(err)
// check(err)
// //log.Println("Mongo Result:", result.Data)
// //fmt.Println("MONGO RES ~~~~~~~~~~~~~~")
// mongoCh <- true
}
if item != nil {
//log.Println("******************* Mem Result:", string(item.Value))
//fmt.Println("HIT")
memCh <- true
}
}(i)
go func(i int) {
err = c.Find(bson.M{"index": strconv.Itoa(i)}).One(&result)
//fmt.Println(err)
check(err)
//log.Println("Mongo Result:", result.Data)
//fmt.Println("MONGO RES ~~~~~~~~~~~~~~")
mongoCh <- true
}(i)
select {
case <-memCh:
fmt.Println("MEMCACHE WINS!!")
fmt.Printf("* time %s \n ", time.Since(start1))
statsdclient.Absolute("p11", int64(time.Since(start1)))
case <-mongoCh:
fmt.Println("MONGO wins xxxxxxxxxxxxxxxxxx")
fmt.Printf("* time %s \n ", time.Since(start1))
statsdclient.Absolute("p11", int64(time.Since(start1)))
case <-timeout:
fmt.Println("TOO SLOW------------------------------------------")
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)
}
}