forked from scylladb/scylla-bench
-
Notifications
You must be signed in to change notification settings - Fork 1
/
workloads.go
199 lines (171 loc) · 5.5 KB
/
workloads.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
package main
import (
"log"
"math"
"math/rand"
"time"
)
func MinInt64(a int64, b int64) int64 {
if a < b {
return a
} else {
return b
}
}
type WorkloadGenerator interface {
NextPartitionKey() int64
NextClusteringKey() int64
IsPartitionDone() bool
IsDone() bool
}
type SequentialVisitAll struct {
PartitionCount int64
ClusteringRowCount int64
NextPartition int64
NextClusteringRow int64
}
func NewSequentialVisitAll(partitionOffset int64, partitionCount int64, clusteringRowCount int64) *SequentialVisitAll {
return &SequentialVisitAll{partitionOffset + partitionCount, clusteringRowCount, partitionOffset, 0}
}
func (sva *SequentialVisitAll) NextPartitionKey() int64 {
if sva.NextClusteringRow < sva.ClusteringRowCount {
return sva.NextPartition
}
sva.NextClusteringRow = 0
sva.NextPartition++
pk := sva.NextPartition
return pk
}
func (sva *SequentialVisitAll) NextClusteringKey() int64 {
ck := sva.NextClusteringRow
sva.NextClusteringRow++
return ck
}
func (sva *SequentialVisitAll) IsDone() bool {
return sva.NextPartition >= sva.PartitionCount || (sva.NextPartition+1 == sva.PartitionCount && sva.NextClusteringRow >= sva.ClusteringRowCount)
}
func (sva *SequentialVisitAll) IsPartitionDone() bool {
return sva.NextClusteringRow == sva.ClusteringRowCount
}
type RandomUniform struct {
Generator *rand.Rand
PartitionCount int64
ClusteringRowCount int64
}
func NewRandomUniform(i int, partitionCount int64, clusteringRowCount int64) *RandomUniform {
generator := rand.New(rand.NewSource(int64(time.Now().Nanosecond() * (i + 1))))
return &RandomUniform{generator, int64(partitionCount), int64(clusteringRowCount)}
}
func (ru *RandomUniform) NextPartitionKey() int64 {
return ru.Generator.Int63n(ru.PartitionCount)
}
func (ru *RandomUniform) NextClusteringKey() int64 {
return ru.Generator.Int63n(ru.ClusteringRowCount)
}
func (ru *RandomUniform) IsDone() bool {
return false
}
func (ru *RandomUniform) IsPartitionDone() bool {
return false
}
type TimeSeriesWrite struct {
PkStride int64
PkOffset int64
PkCount int64
PkPosition int64
PkGeneration int64
CkCount int64
CkPosition int64
StartTime time.Time
Period time.Duration
MoveToNextPartition bool
}
func NewTimeSeriesWriter(threadId int, threadCount int, pkCount int64, ckCount int64, startTime time.Time, rate int64) *TimeSeriesWrite {
period := time.Duration(int64(time.Second.Nanoseconds()) * (pkCount / int64(threadCount)) / rate)
pkStride := int64(threadCount)
pkOffset := int64(threadId)
return &TimeSeriesWrite{pkStride, pkOffset, pkCount, pkOffset - pkStride, 0,
ckCount, 0, startTime, period, false}
}
func (tsw *TimeSeriesWrite) NextPartitionKey() int64 {
tsw.PkPosition += tsw.PkStride
if tsw.PkPosition >= tsw.PkCount {
tsw.PkPosition = tsw.PkOffset
tsw.CkPosition++
if tsw.CkPosition >= tsw.CkCount {
tsw.PkGeneration++
tsw.CkPosition = 0
}
}
tsw.MoveToNextPartition = false
return tsw.PkPosition<<32 | tsw.PkGeneration
}
func (tsw *TimeSeriesWrite) NextClusteringKey() int64 {
tsw.MoveToNextPartition = true
position := tsw.CkPosition + tsw.PkGeneration*tsw.CkCount
return -(tsw.StartTime.UnixNano() + tsw.Period.Nanoseconds()*position)
}
func (*TimeSeriesWrite) IsDone() bool {
return false
}
func (tsw *TimeSeriesWrite) IsPartitionDone() bool {
return tsw.MoveToNextPartition
}
type TimeSeriesRead struct {
Generator *rand.Rand
HalfNormalDist bool
PkStride int64
PkOffset int64
PkCount int64
PkPosition int64
StartTimestamp int64
CkCount int64
CurrentGeneration int64
Period int64
}
func NewTimeSeriesReader(threadId int, threadCount int, pkCount int64, ckCount int64, writeRate int64, distribution string, startTime time.Time) *TimeSeriesRead {
var halfNormalDist bool
switch distribution {
case "uniform":
halfNormalDist = false
case "hnormal":
halfNormalDist = true
default:
log.Fatal("unknown distribution", distribution)
}
generator := rand.New(rand.NewSource(int64(time.Now().Nanosecond() * (threadId + 1))))
pkStride := int64(threadCount)
pkOffset := int64(threadId) % pkCount
period := time.Second.Nanoseconds() / writeRate
return &TimeSeriesRead{generator, halfNormalDist, pkStride, pkOffset, pkCount, pkOffset - pkStride,
startTime.UnixNano(), ckCount, 0, period}
}
func RandomInt64(generator *rand.Rand, halfNormalDist bool, maxValue int64) int64 {
if halfNormalDist {
value := 1. - math.Min(math.Abs(generator.NormFloat64()), 4.)/4.
return int64(float64(maxValue) * value)
} else {
return generator.Int63n(maxValue)
}
}
func (tsw *TimeSeriesRead) NextPartitionKey() int64 {
tsw.PkPosition += tsw.PkStride
if tsw.PkPosition >= tsw.PkCount {
tsw.PkPosition = tsw.PkOffset
}
maxGeneration := (time.Now().UnixNano()-tsw.StartTimestamp)/(tsw.Period*tsw.CkCount) + 1
tsw.CurrentGeneration = RandomInt64(tsw.Generator, tsw.HalfNormalDist, maxGeneration)
return tsw.PkPosition<<32 | tsw.CurrentGeneration
}
func (tsw *TimeSeriesRead) NextClusteringKey() int64 {
maxRange := (time.Now().UnixNano()-tsw.StartTimestamp)/tsw.Period - tsw.CurrentGeneration*tsw.CkCount + 1
maxRange = MinInt64(tsw.CkCount, maxRange)
timestampDelta := (tsw.CurrentGeneration*tsw.CkCount + RandomInt64(tsw.Generator, tsw.HalfNormalDist, maxRange)) * tsw.Period
return -(timestampDelta + tsw.StartTimestamp)
}
func (*TimeSeriesRead) IsDone() bool {
return false
}
func (tsw *TimeSeriesRead) IsPartitionDone() bool {
return false
}