-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
224 lines (204 loc) · 5.23 KB
/
main.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import (
"bufio"
"context"
"encoding/json"
"fmt"
"math"
"math/rand"
"os"
"os/signal"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
btrdb "github.com/BTrDB/btrdb"
"github.com/montanaflynn/stats"
"github.com/pborman/uuid"
)
//Constants
const InsertSize = 120
const InsertInterval = 1 * time.Second
const DelayBetweenWorkerSets = 500 * time.Millisecond
const WorkerSets = 10
var StreamsPerWorker int64 = 5
const DelayBetweenStreams = 15 * time.Millisecond
var activeStreams int64
var droppedRecords int64
var logSpanCh chan span
var exstart time.Time
var gsetcode int
var totalinserts int64
var totaltime int64
var contexts [][]context.CancelFunc
var windowtimes []float64
var windowmu sync.Mutex
func main() {
exstart = time.Now()
rand.Seed(time.Now().UnixNano())
gsetcode = rand.Int() % 0xFFFFFF
fmt.Printf("SET CODE IS %06x\n", gsetcode)
logSpanCh = make(chan span, 1000)
go logSpans()
go printAverages()
dorun()
}
func printAverages() {
var lastinserts int64
var lasttime int64
for {
time.Sleep(15 * time.Second)
tinserts := atomic.LoadInt64(&totalinserts)
ttime := atomic.LoadInt64(&totaltime)
avg := (float64(ttime/1000) / float64(tinserts)) / 1000.0
dinserts := tinserts - lastinserts
dtime := ttime - lasttime
davg := (float64(dtime/1000) / float64(dinserts)) / 1000.0
windowmu.Lock()
dat := windowtimes
windowtimes = make([]float64, 0)
windowmu.Unlock()
p95, e := stats.Percentile(stats.Float64Data(dat), 95)
if e != nil {
fmt.Printf("percentile error: %v\n", e)
}
fmt.Printf("total_inserts=%d avg_time=%.3fms [last15s: +inserts=%d +avg_time=%.3fms p95=%.3fms]\n", tinserts, avg, dinserts, davg, p95)
lastinserts = tinserts
lasttime = ttime
}
}
func dorun() {
contexts = make([][]context.CancelFunc, WorkerSets)
for i := 0; i < WorkerSets; i++ {
contexts[i] = make([]context.CancelFunc, 0)
}
for i := 0; i < WorkerSets; i++ {
go updateWorkerSet(i)
fmt.Fprintf(os.Stderr, "started worker set %d\n", i)
time.Sleep(DelayBetweenWorkerSets)
}
for {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Total streams: ")
text, _ := reader.ReadString('\n')
text = strings.TrimSpace(text)
v, err := strconv.ParseInt(text, 10, 64)
if err != nil {
fmt.Println(err.Error())
} else {
v /= WorkerSets
atomic.StoreInt64(&StreamsPerWorker, v)
}
}
}
func updateWorkerSet(number int) {
b, err := btrdb.Connect(context.Background(), btrdb.EndpointsFromEnv()...)
if err != nil {
panic(err)
}
for {
SPW := atomic.LoadInt64(&StreamsPerWorker)
for i := len(contexts[number]); i < int(SPW); i++ {
atomic.AddInt64(&activeStreams, 1)
fmt.Fprintf(os.Stderr, "started stream %d::%d (%d total)\n", number, i, activeStreams)
ctx, ccancel := context.WithCancel(context.Background())
contexts[number] = append(contexts[number], ccancel)
go beginStream(ctx, b)
time.Sleep(DelayBetweenStreams)
}
for i := int(SPW); i < len(contexts[number]); i++ {
atomic.AddInt64(&activeStreams, -1)
fmt.Fprintf(os.Stderr, "killed stream %d::%d (%d total)\n", number, i, activeStreams)
contexts[number][i]()
//time.Sleep(DelayBetweenStreams)
}
contexts[number] = contexts[number][:SPW]
time.Sleep(500 * time.Millisecond)
}
}
func beginStream(ctx context.Context, db *btrdb.BTrDB) {
uu := uuid.NewRandom()
collection := fmt.Sprintf("sim/%06x/%16x", gsetcode, uint64(rand.Int63()))
stream, err := db.Create(context.Background(), uu, collection, btrdb.M{"name": "stream"}, nil)
if err != nil {
panic(err)
}
last := time.Now()
for {
if ctx.Err() != nil {
return
}
data := make([]btrdb.RawPoint, InsertSize)
for i := 0; i < InsertSize; i++ {
t := last.Add(time.Duration(i) * (InsertInterval / InsertSize)).UnixNano()
data[i] = btrdb.RawPoint{Time: t, Value: math.Sin(float64(t / 1e9))}
}
istart := time.Now()
err = stream.Insert(context.Background(), data)
if err != nil {
panic(err)
}
iend := time.Now()
atomic.AddInt64(&totalinserts, 1)
atomic.AddInt64(&totaltime, int64(iend.Sub(istart)))
windowmu.Lock()
windowtimes = append(windowtimes, float64(int64(iend.Sub(istart))/1000)/1000.0)
windowmu.Unlock()
select {
case logSpanCh <- span{Duration: float64(iend.Sub(istart)/1000) / 1000.0, Time: float64(int64((istart.Sub(exstart) / time.Millisecond)) / 1000.0), ActiveStreams: atomic.LoadInt64(&activeStreams)}:
default:
atomic.AddInt64(&droppedRecords, 1)
}
if ctx.Err() != nil {
return
}
next := last.Add(InsertInterval)
if next.Before(time.Now()) {
next = time.Now()
} else {
time.Sleep(next.Sub(time.Now()))
}
last = next
}
}
type span struct {
Duration float64
ActiveStreams int64
Time float64 `json:"offset"`
}
var spanmu sync.Mutex
var spandata []span
func logSpans() {
f, err := os.Create("results.json")
if err != nil {
panic(err)
}
go func() {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt)
<-c
f.Close()
os.Exit(1)
}()
enc := json.NewEncoder(f)
for s := range logSpanCh {
/*err := enc.Encode(s)
if err != nil {
panic(err)
}*/
_ = s
_ = enc
}
// spanmu.Lock()
// spandata = append(spandata)
// spanmu.Unlock()
}
//Important baselines
//latency distribution of
// -insert vs insert concurrency
// for size 1
// size 10
// size 100
// size 1000
// size 10000