/
generator.go
168 lines (151 loc) · 3.25 KB
/
generator.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
/**
* Based on:
* https://github.com/apavlo/py-tpcc/blob/7c3ff501bbe98a6a7abd3c13267523c3684b62d6/pytpcc/runtime/executor.py
*/
package main
import (
"math/rand"
)
const (
TXN_NEWORDER = iota
TXN_PAYMENT
TXN_ORDERSTATUS
TXN_DELIVERY
TXN_STOCKLEVEL
)
type Generator struct {
rd *rand.Rand
wvec []uint64
wid uint8
nItems uint32
nWarehouses uint8
nLocalDistricts uint8
nLocalCustomers uint32
}
func NewGenerator(
wid uint8,
workloads []uint64,
nItems uint32,
nWarehouses uint8,
nLocalDistricts uint8,
nLocalCustomers uint32,
) *Generator {
wvec := make([]uint64, 5)
var accu uint64 = 0
for i, x := range workloads {
wvec[i] = accu + x
accu += x
}
rd := rand.New(rand.NewSource(int64(wid)))
gen := &Generator {
rd : rd,
wvec : wvec,
wid : wid,
nItems : nItems,
nWarehouses : nWarehouses,
nLocalDistricts : nLocalDistricts,
nLocalCustomers : nLocalCustomers,
}
return gen
}
func (g *Generator) PickTxn() int {
x := g.rd.Uint64() % 100
if x < g.wvec[0] {
return TXN_NEWORDER
} else if x < g.wvec[1] {
return TXN_PAYMENT
} else if x < g.wvec[2] {
return TXN_ORDERSTATUS
} else if x < g.wvec[3] {
return TXN_DELIVERY
} else {
return TXN_STOCKLEVEL
}
}
func (g *Generator) GetNewOrderInput() *NewOrderInput {
/* Generate item info (including iid, wid, and qty) in the new order. */
n := pickNOrderLines(g.rd)
iids := make([]uint32, n)
iwids := make([]uint8, n)
iqtys := make([]uint8, n)
for i := range iids {
iids[i] = g.iid()
iqtys[i] = pickQuantity(g.rd)
/* 1% of order lines are remote. */
if trueWithProb(g.rd, 1) {
iwids[i] = pickWarehouseIdExcept(g.rd, g.nWarehouses, g.wid)
} else {
iwids[i] = g.wid
}
}
p := &NewOrderInput {
W_ID : g.wid,
D_ID : g.did(),
C_ID : g.cid(),
O_ENTRY_D : getTime(),
I_IDS : iids,
I_W_IDS : iwids,
I_QTYS : iqtys,
}
return p
}
func (g *Generator) GetPaymentInput() *PaymentInput {
/* 15% of payments are remote, i.e., W_ID != C_W_ID. */
did := g.did()
var cwid, cdid uint8
if trueWithProb(g.rd, 15) {
cwid = pickWarehouseIdExcept(g.rd, g.nWarehouses, g.wid)
cdid = g.did()
} else {
cwid = g.wid
cdid = did
}
p := &PaymentInput {
W_ID : g.wid,
D_ID : did,
H_AMOUNT : 2.5 /* TODO */,
C_W_ID : cwid,
C_D_ID : cdid,
C_ID : g.cid(),
H_DATE : getTime(),
}
return p
}
func (g *Generator) GetOrderStatusInput() *OrderStatusInput {
p := &OrderStatusInput {
W_ID : g.wid,
D_ID : g.did(),
C_ID : g.cid(),
}
return p
}
func (g *Generator) GetDeliveryInput() *DeliveryInput {
p := &DeliveryInput {
W_ID : g.wid,
O_CARRIER_ID : uint8(pickBetween(g.rd, 1, 10)),
OL_DELIVERY_D : getTime(),
}
return p
}
func (g *Generator) GetStockLevelInput() *StockLevelInput {
p := &StockLevelInput {
W_ID : g.wid,
D_ID : g.did(),
THRESHOLD : uint16(pickBetween(g.rd, 10, 20)),
}
return p
}
func (g *Generator) did() uint8 {
n := uint8(pickBetween(g.rd, 1, uint32(g.nLocalDistricts)))
return n
}
func (g *Generator) cid() uint32 {
/* See Silo tpcc.cc:L376. */
n := uint32(pickBetweenNonUniformly(g.rd, 1023, 259, 1, g.nLocalCustomers))
return n
}
func (g *Generator) iid() uint32 {
/* See Silo tpcc.cc:L369. */
n := uint32(pickBetweenNonUniformly(g.rd, 8191, 7911, 1, g.nItems))
return n
}