-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlarge_batch_workload.py
More file actions
276 lines (130 loc) · 4.73 KB
/
large_batch_workload.py
File metadata and controls
276 lines (130 loc) · 4.73 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
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
#!/usr/bin/python
import time
import sys
import random
import json
all_json = {"views": [], "blocks": []}
total_view_req_count=0
'''
' Simulates a supply chain
'''
# The nodes of the supply chain
nodes = [
'manufacturer1', 'manufacturer2',
'warehouse1', 'warehouse2', 'warehouse3',
'delivery1', 'delivery2',
'shop1', 'shop2', 'shop3', 'shop4', 'shop5', 'shop6', 'shop7'
]
# The edges of the supply chain
edges = [
('manufacturer1', 'warehouse1'),
('manufacturer2', 'warehouse1'),
('warehouse1', 'delivery1'),
('warehouse1', 'delivery2'),
('delivery1','warehouse2'),
('delivery2', 'warehouse3'),
('warehouse2', 'shop1'),
('warehouse2', 'shop2'),
('warehouse2', 'shop3'),
('warehouse2', 'shop4'),
('warehouse2', 'shop5'),
('warehouse3', 'shop4'),
('warehouse3', 'shop5'),
('warehouse3', 'shop6'),
('warehouse3', 'shop7'),
]
roots = ['manufacturer1', 'manufacturer2']
view_req_counts = {}
# The total number of dispatched items
number_of_items = int(sys.argv[1])
# The number of transfers per block
items_per_block = int(sys.argv[2])
# Output file
output_file = 'supply_chain_transactions.txt'
PRINT_TO_FILE = True
PRINT_TO_OUTPUT = True
'''
' Printing the transaction
'''
def print_transaction(f, str):
if PRINT_TO_FILE:
f.write(str+'\n')
if PRINT_TO_OUTPUT:
print(str)
'''
' Creating the access-control views
'''
def create_views(f):
print_transaction(f, 'CREATE TABLE DELIVERY (tid, item, from, to);')
for v in nodes:
all_json["views"].append(v)
print_transaction(f, 'CREATE VIEW {} (tid);'.format(v))
def create_item_list():
items = [(i, [], []) for i in range(number_of_items)]
return items
'''
' Delivery of item from current node to next node
'''
def print_delivery(tid, item, _current, _next, f):
global all_json
print_transaction(f, 'INSERT INTO DELIVERY VALUES (tid: {}, item: {}, from: {}, to: {});'.format(tid, item[0], _current, _next))
operation = {"op": "insert", "tid": tid, "item": item[0], "from": _current, "to": _next, "views":[]}
for u in item[1]:
operation["views"].append({"name": u, "tid": tid})
print_transaction(f, ' ADD TO VIEW {} VALUES (tid: {});'.format(u, tid))
global total_view_req_count
total_view_req_count+=1
global view_counts
if u not in view_req_counts:
view_req_counts[u] = 0
view_req_counts[u]+=1
item[2].append(tid)
for t in item[2]:
operation["views"].append({"name": _next, "tid": t})
print_transaction(f, ' ADD TO VIEW {} VALUES (tid: {});'.format(_next, t))
global total_view_req_count
total_view_req_count+=1
global view_counts
if _next not in view_req_counts:
view_req_counts[_next] = 0
view_req_counts[_next]+=1
all_json["blocks"][-1].append(operation)
def main():
start = time.time()
print('Starting simulation')
with open(output_file, 'w') as f_out:
create_views(f_out)
print('\n')
tid = 1
items = create_item_list()
while items:
# Select random items from the list of items
n = min(items_per_block, len(items))
rand_items = random.sample(items, n)
start_block = True
for item in rand_items:
if not item[1]:
# Randomly select a root node as a starting point for the item delivery
root = random.choice(roots)
item[1].append(root)
current_node = item[1][-1]
options_for_next_node = [u for v, u in edges if v == current_node]
if options_for_next_node:
if start_block:
all_json["blocks"].append([])
print_transaction(f_out, '\nSTART BLOCK')
start_block = False
next_node = random.choice(options_for_next_node)
print_delivery(tid, item, current_node, next_node, f_out)
item[1].append(next_node)
tid += 1
else:
items.remove(item)
end = time.time()
with open("large_{}items_{}batchsize.json".format(number_of_items, items_per_block), 'w+') as f:
print(json.dump(all_json, f, indent=4))
print("\nTotal View Request:",total_view_req_count )
print("\nView Request Count:",view_req_counts )
print("\nRunning time: {:01f}\n".format(end-start))
if __name__ == "__main__":
main()