forked from db-camp/TPCC-Tester
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtester.py
More file actions
173 lines (151 loc) · 6.3 KB
/
Copy pathtester.py
File metadata and controls
173 lines (151 loc) · 6.3 KB
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
import time
import random
from mysql.driver import SQLState
from config import set_warehouse_count
from record.record import (
put_txn, put_new_order
)
from util import (
get_w_id, get_d_id, get_c_id, get_ol_i_id, get_ol_supply_w_id,
get_ol_quantity, get_c_w_id_d_id, query_cus_by, get_h_amount, get_o_carrier_id
)
# 输入一个列表,其中每项代表选择该项目的概率,返回选择的项目的下标
def get_choice(choices):
r = random.random() * sum(choices)
upto = 0
for i in range(len(choices)):
if upto + choices[i] >= r:
return i
upto += choices[i]
assert False, "Shouldn't get here"
def do_test(driver, lock, txns, txn_prob, cnt_w):
set_warehouse_count(cnt_w)
# print(duration)
# print('Test')
t1 = 0
t2 = 0
w_id = 0
d_id = 0
c_id = 0
ol_i_id = 0
ol_supply_w_id = 0
ol_quantity = 0
o_carrier_id = 0
c_w_id = 0
c_d_id = 0
h_amount = 0.0
query_cus = 0
threshold = 0
if txn_prob is None:
txn_prob = [10 / 23, 10 / 23, 1 / 23, 1 / 23, 1 / 23]
t_start = time.time()
for i in range(txns):
txn = get_choice(txn_prob)
ret = SQLState.ABORT
# 预生成操作
if txn == 0: # NewOrder
w_id = get_w_id()
d_id = get_d_id() # 获得地区id,1~10的随机数
c_id = get_c_id() # 获得客户id,1~3000的随机数
ol_i_id = get_ol_i_id() # 获得新订单中的商品id列表
ol_supply_w_id = get_ol_supply_w_id(w_id, driver._scale, len(ol_i_id)) # 为新订单中每个商品选择一个供应仓库,当前设定就一个供应仓库
ol_quantity = get_ol_quantity(len(ol_i_id)) # 为新订单中每个商品设置购买数量
driver._client.send_cmd("-- NewOrder\n")
elif txn == 1: # Payment
w_id = get_w_id()
d_id = get_d_id() # 获得地区id,1~10的随机数
query_cus = query_cus_by(True)
h_amount = get_h_amount()
c_w_id, c_d_id = get_c_w_id_d_id(w_id, d_id, driver._scale) # 获得客户所属的仓库id和地区id
driver._client.send_cmd("-- Payment\n")
elif txn == 2: # Delivery
w_id = get_w_id()
o_carrier_id = get_o_carrier_id()
driver._client.send_cmd("-- Delivery\n")
elif txn == 3: # OrderStatus
w_id = get_w_id()
d_id = get_d_id() # 获得地区id,1~10的随机数
query_cus = query_cus_by()
driver._client.send_cmd("-- OrderStatus\n")
elif txn == 4: # StockLevel
w_id = get_w_id()
d_id = get_d_id() # 获得地区id,1~10的随机数
threshold = random.randrange(10, 101)
driver._client.send_cmd("-- StockLevel\n")
while ret == SQLState.ABORT:
if txn == 0: # NewOrder
t1 = time.time()
ret = driver.do_new_order(w_id, d_id, c_id, ol_i_id, ol_supply_w_id, ol_quantity)
t2 = time.time()
put_new_order(lock, t2 - t_start)
elif txn == 1: # Payment
t1 = time.time()
ret = driver.do_payment(w_id, d_id, c_w_id, c_d_id, query_cus, h_amount)
t2 = time.time()
elif txn == 2: # Delivery
t1 = time.time()
ret = driver.do_delivery(w_id, o_carrier_id)
t2 = time.time()
elif txn == 3: # OrderStatus
t1 = time.time()
ret = driver.do_order_status(w_id, d_id, query_cus)
t2 = time.time()
elif txn == 4: # StockLevel
t1 = time.time()
ret = driver.do_stock_level(w_id, d_id, threshold)
t2 = time.time()
# if ret != SQLState.ABORT:
# put_txn(lock, txn, t2 - t1, True)
if ret == SQLState.ABORT:
put_txn(lock, txn, t2 - t1, False)
else:
put_txn(lock, txn, t2 - t1, True)
# for i in range(txns):
# txn = get_choice(txn_prob)
# ret = SQLState.ABORT
# while ret == SQLState.ABORT:
# if txn == 0: # NewOrder
# w_id = get_w_id()
# d_id = get_d_id() # 获得地区id,1~10的随机数
# c_id = get_c_id() # 获得客户id,1~3000的随机数
# ol_i_id = get_ol_i_id() # 获得新订单中的商品id列表
# ol_supply_w_id = get_ol_supply_w_id(w_id, driver._scale, len(ol_i_id)) # 为新订单中每个商品选择一个供应仓库,当前设定就一个供应仓库
# ol_quantity = get_ol_quantity(len(ol_i_id)) # 为新订单中每个商品设置购买数量
#
# t1 = time.time()
# ret = driver.do_new_order(w_id, d_id, c_id, ol_i_id, ol_supply_w_id, ol_quantity)
# t2 = time.time()
#
# put_new_order(lock, t2 - t_start)
#
# elif txn == 1: # Payment
# w_id = get_w_id()
# d_id = get_d_id() # 获得地区id,1~10的随机数
# c_w_id, c_d_id = get_c_w_id_d_id(w_id, d_id, driver._scale) # 获得客户所属的仓库id和地区id
#
# t1 = time.time()
# ret = driver.do_payment(w_id, d_id, c_w_id, c_d_id, query_cus_by(), random.random() * (5000 - 1) + 1)
# t2 = time.time()
#
# elif txn == 2: # Delivery
# w_id = get_w_id()
# t1 = time.time()
# ret = driver.do_delivery(w_id, get_o_carrier_id())
# t2 = time.time()
#
# elif txn == 3: # OrderStatus
# w_id = get_w_id()
# t1 = time.time()
# ret = driver.do_order_status(w_id, get_d_id(), query_cus_by())
# t2 = time.time()
#
# elif txn == 4: # StockLevel
# w_id = get_w_id()
# t1 = time.time()
# ret = driver.do_stock_level(w_id, get_d_id(), random.randrange(10, 21))
# t2 = time.time()
#
# if ret == SQLState.ABORT:
# put_txn(lock, txn, t2 - t1, False)
# else:
# put_txn(lock, txn, t2 - t1, True)