forked from huanchenz/index-microbench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_workload.py
201 lines (170 loc) · 6.96 KB
/
gen_workload.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
import sys
import csv
import os
import json
import random
from datetime import datetime
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
#####################################################################################
def reverseHostName ( email ) :
name, sep, host = email.partition('@')
hostparts = host[:-1].split('.')
r_host = ''
for part in hostparts :
r_host = part + '.' + r_host
return r_host + sep + name
#####################################################################################
ycsb_dir = 'YCSB/'
workload_dir = 'workload_spec/'
output_dir='workloads/'
def generateWorkload(workload, key_type, rawKeysFilename, workloadIdentifier) :
print bcolors.OKGREEN + 'workload = ' + workload
print 'key type = ' + key_type + bcolors.ENDC
workload_name = workload + '_' + key_type;
workloadIdentifier = '_' + workloadIdentifier if workloadIdentifier else ''
output_workload_name = workload + '_' + key_type + workloadIdentifier
out_ycsb_load = output_dir + 'ycsb_load_' + workload_name;
out_ycsb_txn = output_dir + 'ycsb_txn_' + workload_name
out_load_ycsbkey = output_dir + 'load_' + 'ycsbkey' + '_' + workload_name
out_txn_ycsbkey = output_dir + 'txn_' + 'ycsbkey' + '_' + workload_name
out_load = output_dir + output_workload_name + '_load.dat'
out_txn = output_dir + output_workload_name + '_txn.dat'
cmd_ycsb_load = ycsb_dir + 'bin/ycsb load basic -P ' + workload_dir + workload + ' -s > ' + out_ycsb_load
cmd_ycsb_txn = ycsb_dir + 'bin/ycsb run basic -P ' + workload_dir + workload + ' -s > ' + out_ycsb_txn
if not os.path.exists(output_dir):
os.makedirs(output_dir)
os.system(cmd_ycsb_load)
os.system(cmd_ycsb_txn)
#####################################################################################
originalKeys = []
f_load = open (out_ycsb_load, 'r')
f_load_out = open (out_load_ycsbkey, 'w')
for line in f_load :
cols = line.split()
if len(cols) > 0 and cols[0] == 'INSERT':
originalKeys.append(long(cols[2][4:]))
f_load_out.write (cols[0] + " " + cols[2][4:] + "\n")
f_load.close()
f_load_out.close()
f_txn = open (out_ycsb_txn, 'r')
f_txn_out = open (out_txn_ycsbkey, 'w')
for line in f_txn :
cols = line.split()
if (cols[0] == 'SCAN') or (cols[0] == 'INSERT') or (cols[0] == 'READ') or (cols[0] == 'UPDATE'):
startkey = cols[2][4:]
if cols[0] == 'INSERT':
originalKeys.append(long(startkey))
if cols[0] == 'SCAN' :
numkeys = cols[3]
f_txn_out.write (cols[0] + ' ' + startkey + ' ' + numkeys + '\n')
else :
f_txn_out.write (cols[0] + ' ' + startkey + '\n')
f_txn.close()
f_txn_out.close()
originalKeys.sort()
numberUniqueKeys = len(originalKeys)
cmd = 'rm -f ' + out_ycsb_load
os.system(cmd)
cmd = 'rm -f ' + out_ycsb_txn
os.system(cmd)
#####################################################################################
valueDictionary = {}
random.seed(datetime.now())
if key_type == 'randint':
randomNumbers = [];
for key in originalKeys:
randomNumbers.append(random.getrandbits(63))
randomNumbers.sort()
currentId = 0;
for key in originalKeys:
valueDictionary[key] = str(randomNumbers[currentId])
currentId = currentId + 1
elif key_type == 'monoint':
currentId = 0
for key in originalKeys:
valueDictionary[key] = str(currentId)
currentId = currentId + 1
elif key_type == 'email':
f_email = open (rawKeysFilename, 'r')
emails = f_email.readlines()
for i, email in enumerate(emails):
emails[i] = json.dumps(reverseHostName(email))
emails.sort()
gap = len(emails) / numberUniqueKeys;
currentId = 0;
for key in originalKeys:
valueDictionary[key] = emails[currentId]
currentId = currentId + gap;
elif key_type == 'mappedint':
f_inputKeys = open (rawKeysFilename, 'r')
inputKeys = f_inputKeys.readlines()
selectedKeys = []
for i, key in enumerate(originalKeys):
selectedKeys.append(long(inputKeys[i]))
selectedKeys.sort()
for i, key in enumerate(originalKeys):
valueDictionary[key] = str(selectedKeys[i]);
keymap = {}
f_load = open (out_load_ycsbkey, 'r')
f_load_out = open (out_load, 'w')
for line in f_load :
cols = line.split()
f_load_out.write (cols[0] + ' ' + valueDictionary[long(cols[1])] + '\n')
f_txn = open (out_txn_ycsbkey, 'r')
f_txn_out = open (out_txn, 'w')
for line in f_txn :
cols = line.split()
if cols[0] == 'SCAN' :
f_txn_out.write (cols[0] + ' ' + valueDictionary[long(cols[1])] + ' ' + cols[2] + '\n')
else :
f_txn_out.write (cols[0] + ' ' + valueDictionary[long(cols[1])] + '\n')
f_load_out.close()
f_txn.close()
f_txn_out.close()
cmd = 'rm -f ' + out_load_ycsbkey
os.system(cmd)
cmd = 'rm -f ' + out_txn_ycsbkey
os.system(cmd)
def printUsage() :
print bcolors.WARNING + 'Usage: gen_workload.py <workload_file>'
def checkArguments(argv) :
if(len(sys.argv) != 2) :
printUsage()
sys.exit(1)
if(not os.path.isfile(argv[1])) :
printUsage()
print bcolors.FAIL + argv[1] + " does not exist."
sys.exit(1)
def checkWorkloadRow(row, line) :
if 'workload' not in row or 'keytype' not in row :
print row
print bcolors.FAIL + 'Workload file must be a CSV file with header line consisting of: "workload, keytype"' + bcolors.ENDC
sys.exit(1)
key_type = row['keytype']
workload_file = workload_dir + '/' + row['workload']
if not os.path.isfile(workload_file) :
print bcolors.FAIL + 'Workload definition ' + workload_file + ' (line ' + line + ') does not exist.' + bcolors.ENDC
sys.exit(1)
if key_type not in [ 'randint', 'monoint', 'email', 'mappedint' ]:
print bcolors.FAIL + 'Keytype ' + key_type + ' on line ' + line + ' is unknown. Only randint, monoint, mappedint or email are supported. ' + bcolors.ENDC
sys.exit(1)
def main(argv):
checkArguments(argv)
config_file = argv[1]
print bcolors.OKGREEN + 'generating workloads defined in ' + config_file + bcolors.ENDC
with open(config_file) as csvfile:
reader = csv.DictReader(filter(lambda line: not line.startswith("#"), csvfile), skipinitialspace = True)
linenumber = 1;
for row in reader:
checkWorkloadRow(row, ++linenumber)
generateWorkload(row['workload'], row['keytype'], row.get('filename', ''), row.get('workloadidentifier', ''))
if __name__ == '__main__':
main(sys.argv)