-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
181 lines (148 loc) · 5.03 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
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"os/signal"
"sync"
"sync/atomic"
"time"
"github.com/gocql/gocql"
)
var (
keyspaceName string
tableName string
clusterName string
replicationFactor int
concurrency int
maxRate int
processTimeout time.Duration
timeout time.Duration
errorToTimeoutCutoffTime time.Duration
dropDBAfterTest bool
totalLatency int64
completedRequests int64
reportInterval time.Duration
tracker *WindowTracker
requestsThrottled int64
stopAll bool
)
func ExecuteQuery(ctx context.Context, session *gocql.Session, request string) {
err := session.Query(request).WithContext(ctx).Exec()
if err != nil {
log.Fatal(err)
}
}
func PrepareDatabase(ctx context.Context, session *gocql.Session, replicationFactor int) {
// NOTE: SimpleStrategy strategy was used here but `NetworkTopologyStrategy` should be used for production case
ExecuteQuery(ctx, session, fmt.Sprintf("CREATE KEYSPACE IF NOT EXISTS %s WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : %d }", keyspaceName, replicationFactor))
// Create table and columns
// keys and values will always be random
ExecuteQuery(ctx, session, "CREATE TABLE IF NOT EXISTS "+keyspaceName+"."+tableName+" (key text, value text, PRIMARY KEY(key)) WITH compression = { }")
}
func DropDatabase(ctx context.Context, session *gocql.Session) {
ExecuteQuery(ctx, session, fmt.Sprintf("DROP KEYSPACE IF EXISTS %s", keyspaceName))
}
func cleanup(ctx context.Context, session *gocql.Session) {
defer session.Close()
if dropDBAfterTest {
DropDatabase(ctx, session)
}
}
func main() {
flag.StringVar(&clusterName, "cluster", "", "cluster name (required)")
flag.StringVar(&keyspaceName, "keyspace", "scylla_bench", "keyspace name (optional)")
flag.StringVar(&tableName, "table", "test", "table to use (optional)")
flag.IntVar(&concurrency, "parallelism", 0, "maximum number of concurrent queries (required)")
flag.IntVar(&maxRate, "rate-limit", 0, "maximum number of requests per second req/s (required)")
flag.DurationVar(&processTimeout, "process-timeout", 30*time.Minute, "set how long this process should run before signaling a timeout error")
flag.DurationVar(&timeout, "timeout", 5*time.Second, "each request timeout")
flag.BoolVar(&dropDBAfterTest, "drop-db", true, "drop database after process is completed")
flag.DurationVar(&reportInterval, "report-interval", 1*time.Millisecond, "at what interval should total requests performed and the avarage latencies be printed")
flag.Parse()
if clusterName == "" {
log.Fatal("--cluster flag: cluster name must be specified")
}
if concurrency == 0 {
log.Fatal("--parallelism flag: Number of queries to be performed in parallel must be specified")
}
if maxRate == 0 {
log.Fatal("--rate-limit flag: Maximum number of queries to be executed per seconds must be specified")
}
cluster := gocql.NewCluster(clusterName)
cluster.Consistency = gocql.Quorum
cluster.Timeout = timeout
session, err := cluster.CreateSession()
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
replicationFactor = 1 // Should be > 1 for fault tolerance in production mode
PrepareDatabase(ctx, session, replicationFactor)
defer cleanup(ctx, session)
rlconfig := NewRateLimiterConfig(maxRate)
tracker = NewWindowTracker(rlconfig)
interrupted := make(chan os.Signal, 1)
signal.Notify(interrupted, os.Interrupt)
go func() {
<-interrupted
fmt.Println("\ninterrupted")
<-interrupted
fmt.Println("\nkilled")
cleanup(ctx, session)
os.Exit(1)
}()
go func() {
time.Sleep(processTimeout)
fmt.Println("\nProcess took longer than expected, timeout error")
cleanup(ctx, session)
os.Exit(1)
}()
if timeout != 0 {
errorToTimeoutCutoffTime = timeout / 5
} else {
errorToTimeoutCutoffTime = time.Second
}
fmt.Println("Configuration")
fmt.Println("Concurrency:\t\t", concurrency)
fmt.Println("Maximum Rate Limit:\t", maxRate, "req/s")
wg := &sync.WaitGroup{}
wg.Add(concurrency)
startTime := time.Now()
err = RunConcurrently(wg, func(threadId int) {
DoWrite(threadId, session)
})
if err != nil {
log.Fatal(err)
}
// Print total requests and the average latencies at report interval
go func() {
ticker := time.NewTicker(reportInterval)
defer ticker.Stop()
for range ticker.C {
if !stopAll {
printStat()
}
}
}()
wg.Wait()
endTime := time.Now()
totalTimeTaken := endTime.Sub(startTime)
printStat()
fmt.Printf("\nFINAL RESULT:\n")
fmt.Printf("Requests Sent: \t\t%d\n", concurrency)
fmt.Printf("Maximum Requests Rate: \t%d/sec\n", maxRate)
fmt.Printf("Requests Processed: \t%d\n", completedRequests)
fmt.Printf("Requests Throttled: \t%d\n", requestsThrottled)
fmt.Printf("Process Execution Time: %v\n", totalTimeTaken)
stopAll = true
}
func printStat() {
if atomic.LoadInt64(&completedRequests) == 0 {
return
}
avgLatency := time.Duration(totalLatency / atomic.LoadInt64(&completedRequests))
fmt.Printf("Total requests: %d, Average latency: %v\n", atomic.LoadInt64(&completedRequests), avgLatency)
}