forked from mongodb-labs/py-tpcc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tpcc.py
300 lines (257 loc) · 10.8 KB
/
tpcc.py
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------
# Copyright (C) 2011
# Andy Pavlo
# http:##www.cs.brown.edu/~pavlo/
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# -----------------------------------------------------------------------
import sys
import os
import string
import datetime
import logging
import re
import argparse
import glob
import time
import multiprocessing
from configparser import ConfigParser
from pprint import pprint, pformat
from functools import partial
from util import results, scaleparameters
from runtime import executor, loader
open_utf8 = partial(open, encoding='UTF-8')
log_file = open('results.log', encoding='utf-8', mode='a+')
logging.basicConfig(level=logging.INFO,
format="%(asctime)s [%(funcName)s:%(lineno)03d] %(levelname)-5s: %(message)s",
datefmt="%m-%d-%Y %H:%M:%S",
)
ch = logging.StreamHandler(log_file)
ch.setLevel(logging.INFO)
ch.setFormatter(logging.Formatter('%(asctime)s [%(funcName)s:%(lineno)03d] %(levelname)-5s: %(message)s'))
logging.getLogger().addHandler(ch)
## ==============================================
## createDriverClass
## ==============================================
def createDriverClass(name):
full_name = "%sDriver" % name.title()
mod = __import__('drivers.%s' % full_name.lower(), globals(), locals(), [full_name])
klass = getattr(mod, full_name)
return klass
## DEF
## ==============================================
## getDrivers
## ==============================================
def getDrivers():
drivers = []
for f in [os.path.basename(drv).replace("driver.py", "") for drv in glob.glob("./drivers/*driver.py")]:
if f != "abstract":
drivers.append(f)
return drivers
## DEF
## ==============================================
## startLoading
## ==============================================
def startLoading(driverClass, scaleParameters, args, config):
logging.debug("Creating client pool with %d processes", args['clients'])
pool = multiprocessing.Pool(args['clients'])
# Split the warehouses into chunks
w_ids = [[] for _ in range(args['clients'])]
for w_id in range(scaleParameters.starting_warehouse, scaleParameters.ending_warehouse + 1):
idx = w_id % args['clients']
w_ids[idx].append(w_id)
## FOR
loader_results = []
for i in range(args['clients']):
r = pool.apply_async(loaderFunc, (driverClass, scaleParameters, args, config, w_ids[i]))
loader_results.append(r)
## FOR
pool.close()
logging.debug("Waiting for %d loaders to finish", args['clients'])
pool.join()
## DEF
## ==============================================
## loaderFunc
## ==============================================
def loaderFunc(driverClass, scaleParameters, args, config, w_ids):
driver = driverClass(args['ddl'])
assert driver != None, "Driver in loadFunc is none!"
logging.debug("Starting client execution: %s [warehouses=%d]", driver, len(w_ids))
config['load'] = True
config['execute'] = False
config['reset'] = False
config['warehouses'] = args['warehouses']
driver.loadConfig(config)
try:
loadItems = (1 in w_ids)
l = loader.Loader(driver, scaleParameters, w_ids, loadItems)
driver.loadStart()
l.execute()
driver.loadFinish()
except KeyboardInterrupt:
return -1
except (Exception, AssertionError) as ex:
logging.warn("Failed to load data: %s", ex)
raise
## DEF
## ==============================================
## startExecution
## ==============================================
def startExecution(driverClass, scaleParameters, args, config):
logging.debug("Creating client pool with %d processes", args['clients'])
pool = multiprocessing.Pool(args['clients'])
del args['config']
worker_results = []
for _ in range(args['clients']):
r = pool.apply_async(executorFunc, (driverClass, scaleParameters, args, config))
worker_results.append(r)
## FOR
pool.close()
pool.join()
total_results = results.Results()
for asyncr in worker_results:
asyncr.wait()
r = asyncr.get()
assert r is not None, "No results object returned by thread!"
if r == -1:
sys.exit(1)
total_results.append(r)
## FOR
return total_results
## DEF
## ==============================================
## executorFunc
## ==============================================
def executorFunc(driverClass, scaleParameters, args, config):
driver = driverClass(args['ddl'])
assert driver is not None, "No driver in executorFunc"
logging.debug("Starting client execution: %s", driver)
config['execute'] = True
config['reset'] = False
driver.loadConfig(config)
e = executor.Executor(driver, scaleParameters, stop_on_error=args['stop_on_error'])
driver.executeStart()
results = e.execute(args['duration'])
driver.executeFinish()
return results
## DEF
## ==============================================
## main
## ==============================================
if __name__ == '__main__':
aparser = argparse.ArgumentParser(description='Python implementation of the TPC-C Benchmark')
aparser.add_argument('system', choices=getDrivers(),
help='Target system driver')
aparser.add_argument('--config', type=open_utf8,
help='Path to driver configuration file')
aparser.add_argument('--reset', action='store_true',
help='Instruct the driver to reset the contents of the database')
aparser.add_argument('--scalefactor', default=1, type=float, metavar='SF',
help='Benchmark scale factor')
aparser.add_argument('--warehouses', default=4, type=int, metavar='W',
help='Number of Warehouses')
aparser.add_argument('--duration', default=60, type=int, metavar='D',
help='How long to run the benchmark in seconds')
aparser.add_argument('--ddl',
default=os.path.realpath(os.path.join(os.path.dirname(__file__), "tpcc.sql")),
help='Path to the TPC-C DDL SQL file')
aparser.add_argument('--clients', default=1, type=int, metavar='N',
help='The number of blocking clients to fork')
aparser.add_argument('--stop-on-error', action='store_true',
help='Stop the transaction execution when the driver throws an exception.')
aparser.add_argument('--no-load', action='store_true',
help='Disable loading the data')
aparser.add_argument('--no-execute', action='store_true',
help='Disable executing the workload')
aparser.add_argument('--print-config', action='store_true',
help='Print out the default configuration file for the system and exit')
aparser.add_argument('--debug', action='store_true',
help='Enable debug log messages')
args = vars(aparser.parse_args())
if args['debug']:
logging.getLogger().setLevel(logging.DEBUG)
## Create a handle to the target client driver
driverClass = createDriverClass(args['system'])
assert driverClass is not None, "Failed to find '%s' class" % args['system']
driver = driverClass(args['ddl'])
assert driver is not None, "Failed to create '%s' driver" % args['system']
if args['print_config']:
config = driver.makeDefaultConfig()
print(driver.formatConfig(config))
print()
sys.exit(0)
## Load Configuration file
if args['config']:
logging.debug("Loading configuration file '%s'", args['config'])
cparser = ConfigParser()
cparser.read(os.path.realpath(args['config'].name))
config = dict(cparser.items(args['system']))
else:
logging.debug("Using default configuration for %s", args['system'])
defaultConfig = driver.makeDefaultConfig()
config = dict([(param, defaultConfig[param][1]) for param in defaultConfig.keys()])
config['reset'] = args['reset']
config['load'] = False
config['execute'] = False
if config['reset']:
logging.info("Reseting database")
config['warehouses'] = args['warehouses']
driver.loadConfig(config)
logging.info("Initializing TPC-C benchmark using %s", driver)
## Create ScaleParameters
scaleParameters = scaleparameters.makeWithScaleFactor(args['warehouses'], args['scalefactor'])
if args['debug']:
logging.debug("Scale Parameters:\n%s", scaleParameters)
## DATA LOADER!!!
load_time = None
if not args['no_load']:
logging.info("Loading TPC-C benchmark data using %s", (driver))
load_start = time.time()
if args['clients'] == 1:
l = loader.Loader(
driver,
scaleParameters,
range(scaleParameters.starting_warehouse, scaleParameters.ending_warehouse + 1),
True
)
driver.loadStart()
l.execute()
driver.loadFinish()
else:
startLoading(driverClass, scaleParameters, args, config)
load_time = time.time() - load_start
## IF
## WORKLOAD DRIVER!!!
if not args['no_execute']:
if args['clients'] == 1:
e = executor.Executor(driver, scaleParameters, stop_on_error=args['stop_on_error'])
driver.executeStart()
results = e.execute(args['duration'])
driver.executeFinish()
else:
results = startExecution(driverClass, scaleParameters, args, config)
assert results, "No results from execution for %d client!" % args['clients']
logging.info("Final Results")
logging.info("Threads: %d", args['clients'])
logging.info(results.show(load_time, driver, args['clients']))
## IF
## MAIN