-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
339 lines (267 loc) · 12.1 KB
/
main.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import asyncio
import os
from datetime import datetime, timezone
from time import sleep
from azure.core.exceptions import ResourceExistsError
from azure.data.tables import TableTransactionError
from tabulate import tabulate
from tqdm import tqdm
from multiprocessing.pool import ThreadPool
def generate_entities(start_index: int, count: int, property_shapes: tuple):
from random import choice
from string import ascii_lowercase
entities = []
for i in range(start_index, start_index + count):
entity = {
'startIndex': start_index,
'currentIndex': i
}
for prop_index, prop_size in enumerate(property_shapes):
entity[f'p{prop_index}'] = "".join(choice(ascii_lowercase) for _ in range(prop_size))
entities.append(entity)
return entities
def to_basic_entity(i, e):
e["PartitionKey"] = 'basic'
e["RowKey"] = str(i)
return e
def to_partitioned_entity(i, e, modulo=10):
partition = i % modulo
e["PartitionKey"] = f'batch_{partition:d}'
e["RowKey"] = str(i)
return str(partition), e
def basic_upsert(items):
table_client = get_table_client('basic')
for i, e in enumerate(tqdm(items)):
entity = to_basic_entity(i, e)
table_client.upsert_entity(entity)
# if i % 500 == 0:
# print("\tProcessing entity with index %d" % i)
print("Done, processed a total of %d entities" % len(items))
def batch_upsert(items, batch_size=100):
operations = []
for i, e in enumerate(tqdm(items)):
entity = to_basic_entity(i, e)
operations.append(('upsert', entity))
# if i % 500 == 0:
# print("\tProcessing entity with index %d" % i)
if i % batch_size == 0:
table_client = get_table_client('batch')
try:
table_client.submit_transaction(operations)
operations = []
except TableTransactionError as e:
print("Failed to submit transaction")
raise e
print("Done, processed a total of %d entities" % len(items))
def batch_upsert_partitioned(items, batch_size=100, partition_modulo=10):
partitioned_operations = {}
for i, e in enumerate(tqdm(items)):
p, entity = to_partitioned_entity(i, e, partition_modulo)
# Create the operations list for this partition if it doesn't exist.
if p not in partitioned_operations:
partitioned_operations[p] = []
partition = partitioned_operations[p]
partition.append(('upsert', entity))
# if i % 500 == 0:
# print("\tProcessing entity with index %d" % i)
if len(partition) == batch_size:
submit_partition(partitioned_operations, p, partition_modulo)
# Clean up any partitions with outstanding operations.
for p in partitioned_operations:
submit_partition(partitioned_operations, p, partition_modulo)
print("Done, processed a total of %d entities" % len(items))
async def batch_upsert_partitioned_async(items, batch_size=100, partition_modulo=10):
from azure.data.tables.aio import TableClient
connection_string = get_connection_string()
table_name = f'batchPartitioned{partition_modulo:d}'f'batchPartitioned{partition_modulo:d}'
table_client = TableClient.from_connection_string(conn_str=connection_string, table_name=table_name)
try:
await table_client.create_table()
print('table %s created successfully' % table_name)
except ResourceExistsError:
print('table %s already exists' % table_name)
pass
partitioned_operations = {}
for i, e in enumerate(tqdm(items)):
p, entity = to_partitioned_entity(i, e, partition_modulo)
# Create the operations list this partition if it doesn't exist.
if p not in partitioned_operations:
partitioned_operations[p] = []
partition = partitioned_operations[p]
partition.append(('upsert', entity))
# if i % 500 == 0:
# print("\tProcessing entity with index %d" % i)
if len(partition) == batch_size:
await submit_partition_async(partitioned_operations, p, partition_modulo, table_client)
# Clean up any partitions with outstanding operations.
for p in partitioned_operations:
await submit_partition_async(partitioned_operations, p, partition_modulo, table_client)
await table_client.__aexit__()
print("Done, processed a total of %d entities" % len(items))
def batch_upsert_partitioned_parallel(items, batch_size=100, partition_modulo=10):
from azure.data.tables import TableClient
connection_string = get_connection_string()
table_name = f'batchPartitionedParallel{partition_modulo:d}'
table_client = TableClient.from_connection_string(conn_str=connection_string, table_name=table_name)
try:
table_client.create_table()
print('table %s created successfully' % table_name)
except ResourceExistsError:
print('table %s already exists' % table_name)
pass
# One process per partition
partitioned_operations = {}
for i, e in enumerate(tqdm(items)):
p, entity = to_partitioned_entity(i, e, partition_modulo)
# Create the operations list this partition if it doesn't exist.
if p not in partitioned_operations:
partitioned_operations[p] = []
partition = partitioned_operations[p]
partition.append(('upsert', entity))
# if i % 500 == 0:
# print("\tProcessing entity with index %d" % i)
pool = ThreadPool(partition_modulo)
for p, partition in enumerate(partitioned_operations):
pool.apply_async(process_partition, (partition, partition_modulo, batch_size, table_client))
table_client.close()
def process_partition(whole_partition, partition_modulo, batch_size, table_client):
partition = []
for item in whole_partition:
partition.append(item)
if len(partition) == batch_size:
submit_partition(partition, partition_modulo, table_client)
partition = []
# Clean up any partitions with outstanding operations.
if len(partition) > 0:
submit_partition(partition, partition_modulo, table_client)
print("Done, processed a total of %d entities" % len(whole_partition))
def submit_partition(partitioned_operations, p, partition_modulo):
partition = partitioned_operations[p]
if len(partition) == 0:
return
table_client = get_table_client(f'batchPartitioned{partition_modulo:d}')
try:
table_client.submit_transaction(partition)
partitioned_operations[p] = []
except TableTransactionError as e:
print("Failed to submit transaction")
raise e
async def submit_partition_async(partitioned_operations, p, partition_modulo, table_client):
partition = partitioned_operations[p]
if len(partition) == 0:
return
try:
await table_client.submit_transaction(partition)
partitioned_operations[p] = []
except TableTransactionError as e:
print("Failed to submit transaction")
raise e
def get_connection_string():
from os import environ
return environ.get('TABLE_STORAGE_CONNECTION_STRING', 'UseDevelopmentStorage=true')
def get_table_client(table_name: str):
from azure.data.tables import TableClient
connection_string = get_connection_string()
table_client = TableClient.from_connection_string(conn_str=connection_string, table_name=table_name)
try:
table_client.create_table()
print('table %s created successfully' % table_name)
except ResourceExistsError:
# print('table %s already exists' % table_name)
pass
return table_client
async def get_table_client_async(table_name: str):
from azure.data.tables.aio import TableClient
connection_string = get_connection_string()
table_client = TableClient.from_connection_string(conn_str=connection_string, table_name=table_name)
try:
# print('table %s created successfully' % table_name)
return await table_client.create_table()
except ResourceExistsError:
# print('table %s already exists' % table_name)
pass
except:
try:
await table_client.close()
except:
pass
raise
def run_test(n, property_shapes, insert_function, *args):
print("Starting insert test for function %s with %d entities." % (insert_function.__name__, n))
entities = generate_entities(0, n, property_shapes)
start_time = datetime.now()
insert_function(entities, *args)
execution_time = datetime.now() - start_time
total_seconds = execution_time.total_seconds()
eps = float(n) / total_seconds
print(f"Finished test for function {insert_function.__name__:s} saving {n:d} entities in {total_seconds:f}s "
f"({eps:f} entities per second)")
return (total_seconds, eps, insert_function.__name__) + args
async def run_test_async(n, property_shapes, insert_function, *args):
print("Starting insert test for function %s with %d entities." % (insert_function.__name__, n))
entities = generate_entities(0, n, property_shapes)
start_time = datetime.now()
await insert_function(entities, *args)
execution_time = datetime.now() - start_time
total_seconds = execution_time.total_seconds()
eps = float(n) / total_seconds
print(f"Finished test for function {insert_function.__name__:s} saving {n:d} entities in {total_seconds:f}s "
f"({eps:f} entities per second)")
return (total_seconds, eps, insert_function.__name__) + args
def async_test(function,args):
return asyncio.run(function(*args))
def cleanup():
from azure.data.tables import TableServiceClient
connection_string = get_connection_string()
table_service_client = TableServiceClient.from_connection_string(connection_string)
for table in table_service_client.list_tables():
#if table.name == 'results':
# continue
print(f'deleting table {table.name}')
table_service_client.delete_table(table.name)
print("sleeping 5 seconds...")
sleep(5)
def result_to_entity(partition_name, result_row, headers):
e = {
'PartitionKey': partition_name,
}
for i, header in enumerate(headers):
if(len(result_row) > i):
e[header] = result_row[i]
function = e.get("function", "")
partitionSize = e.get("partitionSize", "")
partitionCount = e.get("partitionCount", "")
e['RowKey'] = f'{function}_{partitionSize}_{partitionCount}'
return e
def save_results(partition_name, results, headers):
table_client = get_table_client(f'results')
operations = [('upsert',result_to_entity(partition_name, result, headers)) for result in results]
try:
table_client.submit_transaction(operations)
except TableTransactionError as e:
print("Failed to submit transaction")
raise e
if __name__ == '__main__':
cleanup()
# test with 300k entities, 4 props, 40,40,300,100 chars respectively
n_entities = os.environ.get("N_ENTITIES", 1000)
#n_entities = 300000
property_shapes = (40, 40, 300, 100)
partition_name = datetime.now(timezone.utc).isoformat()
print(f'results will be saved with partition key {partition_name}')
tests = []
results = []
tests.append((run_test,(n_entities, property_shapes, basic_upsert)))
tests.append((run_test,(n_entities, property_shapes, batch_upsert)))
partition_counts = (100, 200, 500, 1000, 2000, 2500, 5000)
for partition_count in partition_counts:
tests.append((run_test, (n_entities, property_shapes, batch_upsert_partitioned, 100, partition_count)))
for partition_count in partition_counts:
tests.append((async_test, (run_test_async, (n_entities, property_shapes, batch_upsert_partitioned_async, 100, partition_count))))
for partition_count in partition_counts:
tests.append((run_test, (n_entities, property_shapes, batch_upsert_partitioned_parallel, 100, partition_count)))
for test in tests:
results.append(test[0](*test[1]))
headers = ["elapsed", "eps", "function", "partitionSize", "partitionCount"]
print(tabulate(results, headers=headers))
save_results(partition_name, results,headers)