forked from danielvegamyhre/minicache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
191 lines (160 loc) · 4.51 KB
/
main_test.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package main
import (
"context"
"path/filepath"
"strconv"
"sync"
"testing"
"time"
"github.com/malwaredllc/minicache/client/cache_client"
"github.com/malwaredllc/minicache/server"
)
const (
RELATIVE_CONFIG_PATH = "configs/nodes-local.json"
RELATIVE_CONFIG_PATH_HTTP = "configs/nodes-local-insecure.json"
RELATIVE_CLIENT_CERT_DIR = "certs"
)
// 10 goroutines make 10k requests each via REST API. Count cache misses.
func Test10kConcurrentRestApiPuts(t *testing.T) {
// start servers
capacity := 100
verbose := false
insecure := false
abs_cert_dir, _ := filepath.Abs(RELATIVE_CLIENT_CERT_DIR)
abs_config_path, _ := filepath.Abs(RELATIVE_CONFIG_PATH)
shutdown_chan := make(chan bool, 1)
components := server.CreateAndRunAllFromConfig(capacity, abs_config_path, verbose, insecure)
// start client
c := cache_client.NewClientWrapper(abs_cert_dir, abs_config_path, insecure)
c.StartClusterConfigWatcher(shutdown_chan)
var wg sync.WaitGroup
var mutex sync.Mutex
miss := 0.0
// start timer
start := time.Now()
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for i := 1; i <= 1000; i++ {
v := strconv.Itoa(i)
err := c.Put(v, v)
if err != nil {
mutex.Lock()
miss += 1
mutex.Unlock()
}
}
}()
}
wg.Wait()
elapsed := time.Since(start)
t.Logf("Time to complete 10k puts via REST: %s", elapsed)
t.Logf("Cache misses: %d/10,000 (%f%%)", int(miss), miss/10000)
// cleanup
for _, srv_comps := range components {
srv_comps.GrpcServer.Stop()
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
if err := srv_comps.HttpServer.Shutdown(ctx); err != nil {
t.Logf("Http server shutdown error: %s", err)
}
}
shutdown_chan <- true
}
// 10 goroutines make 10k requests each via REST API (but http). Count cache misses.
func Test10kConcurrentRestApiPutsInsecure(t *testing.T) {
// start servers
capacity := 100
verbose := false
insecure := true
abs_cert_dir, _ := filepath.Abs(RELATIVE_CLIENT_CERT_DIR)
abs_config_path, _ := filepath.Abs(RELATIVE_CONFIG_PATH_HTTP)
shutdown_chan := make(chan bool, 1)
components := server.CreateAndRunAllFromConfig(capacity, abs_config_path, verbose, insecure)
// start client
cl := cache_client.NewClientWrapper(abs_cert_dir, abs_config_path, insecure)
cl.StartClusterConfigWatcher(shutdown_chan)
var wg sync.WaitGroup
var mutex sync.Mutex
miss := 0.0
// start timer
start := time.Now()
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for i := 1; i <= 1000; i++ {
v := strconv.Itoa(i)
err := cl.Put(v, v)
if err != nil {
mutex.Lock()
miss += 1
mutex.Unlock()
}
}
}()
}
wg.Wait()
elapsed := time.Since(start)
t.Logf("Time to complete 10k puts via REST: %s", elapsed)
t.Logf("Cache misses: %d/10,000 (%f%%)", int(miss), miss/10000)
// cleanup
for _, srv_comps := range components {
srv_comps.GrpcServer.Stop()
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
if err := srv_comps.HttpServer.Shutdown(ctx); err != nil {
t.Logf("Http server shutdown error: %s", err)
}
}
shutdown_chan <- true
}
// 10 goroutines make 10k requests each vi gRPC. Count cache misses.
func Test10kConcurrentGrpcPuts(t *testing.T) {
// start servers
capacity := 100
verbose := false
insecure := false
abs_cert_dir, _ := filepath.Abs(RELATIVE_CLIENT_CERT_DIR)
abs_config_path, _ := filepath.Abs(RELATIVE_CONFIG_PATH)
shutdown_chan := make(chan bool, 1)
components := server.CreateAndRunAllFromConfig(capacity, abs_config_path, verbose, insecure)
// start client
c := cache_client.NewClientWrapper(abs_cert_dir, abs_config_path, insecure)
c.StartClusterConfigWatcher(shutdown_chan)
var wg sync.WaitGroup
var mutex sync.Mutex
miss := 0.0
// start timer
start := time.Now()
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for i := 1; i <= 1000; i++ {
v := strconv.Itoa(i)
err := c.PutGrpc(v, v)
if err != nil {
mutex.Lock()
miss += 1
mutex.Unlock()
}
}
}()
}
wg.Wait()
elapsed := time.Since(start)
t.Logf("Time to complete 10k puts via gRPC API: %s", elapsed)
t.Logf("Cache misses: %d/10,000 (%f%%)", int(miss), miss/10000)
// cleanup
for _, srv_comps := range components {
srv_comps.GrpcServer.Stop()
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
if err := srv_comps.HttpServer.Shutdown(ctx); err != nil {
t.Logf("Http server shutdown error: %s", err)
}
}
shutdown_chan <- true
}