-
Notifications
You must be signed in to change notification settings - Fork 0
/
kv.go
117 lines (91 loc) · 2.88 KB
/
kv.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
/*
Copyright 2016, Alex Tomic
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
// Testers for the K-V stores we will compare against - memcached for now
import (
"github.com/bradfitz/gomemcache/memcache"
"time"
"strconv"
"fmt"
)
type Memcache struct {
mc *memcache.Client
valStr string
}
func (m *Memcache) Init(host string, port int, user string, password string) {
_connStr := "%v:%v"
connStr := fmt.Sprintf(_connStr, host, port)
m.mc = memcache.New(connStr)
// We can get i/o timeouts with parallel executions without this
m.mc.Timeout = time.Second * 2
m.valStr = "1234567890ABCDEF"
}
func (m *Memcache) CreateTables() {
err := m.mc.DeleteAll()
checkErr(err, "Error purging memcache for prepare")
}
func (m *Memcache) InsertByPkRandom(start int, end int, t chan time.Duration) {
// There are no 'tables' to set up in memcached so just pre-write the keys
var err error
tm_s := time.Now()
for i := start; i< end ; i++ {
k := strconv.Itoa(i)
err = m.mc.Add(&memcache.Item{Key: k, Value: []byte(m.valStr)})
checkErr(err, "Failed to add data to memcache: ", k)
}
tm_e := time.Now()
t <- tm_e.Sub(tm_s)
}
func (m *Memcache) SelectByPkRandom(start int, end int, t chan time.Duration) {
l := end - start
rnd := InitSampleSet(end - start, start)
tm_s := time.Now()
for i := 0; i < l; i++ {
var k string
// float -> int -> string, yay
k = strconv.Itoa(int(rnd[i]))
_, err := m.mc.Get(k)
checkErr(err, "Failed on stmt exec for k/v: ", k)
//v := string(item.Value)
//assert(v == m.valStr, "Value string not same")
//fmt.Printf("k,v : %v , %v\n", k, v)
}
tm_e := time.Now()
t <- tm_e.Sub(tm_s)
}
func (m *Memcache) UpdateByPkRandom(start int, end int, t chan time.Duration) {
l := end - start
rnd := InitSampleSet(end - start, start)
tm_s := time.Now()
m.valStr = "00012312301230"
for i := 0; i<l ; i++ {
k := strconv.Itoa(int(rnd[i]))
err := m.mc.Replace(&memcache.Item{Key: k, Value: []byte(m.valStr)})
checkErr(err, "Failed to update data to memcache:", k)
}
tm_e := time.Now()
t <- tm_e.Sub(tm_s)
}
func (m *Memcache) DeleteByPkRandom(start int, end int, t chan time.Duration) {
l := end - start
rnd := InitSampleSet(end - start, start)
tm_s := time.Now()
m.valStr = "00012312301230"
for i := 0; i<l ; i++ {
k := strconv.Itoa(int(rnd[i]))
err := m.mc.Delete(k)
checkErr(err, "Failed to delete key from memcache", k)
}
tm_e := time.Now()
t <- tm_e.Sub(tm_s)
}