-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction_tester.py
67 lines (55 loc) · 1.94 KB
/
transaction_tester.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
from db import Database
from query import Query
from transaction import Transaction
from transaction_worker import TransactionWorker
import threading
from random import choice, randint, sample, seed
db = Database()
# db.open('/home/pkhorsand/165a-winter-2020-private/db')
db.open('~/ECS165a')
grades_table = db.create_table('Grades', 5, 0)
keys = []
records = {}
num_threads = 8
# seed(8739878934)
# Generate random records
for i in range(0, 10000):
key = 92106429 + i
keys.append(key)
records[key] = [key, 0, 0, 0, 0]
q = Query(grades_table)
q.insert(*records[key])
# create TransactionWorkers
transaction_workers = []
for i in range(num_threads):
transaction_workers.append(TransactionWorker([]))
# generates 10k random transactions
# each transaction will increment the first column of a record 5 times
for i in range(1000):
k = randint(0, 2000 - 1)
transaction = Transaction()
for j in range(5):
key = keys[k * 5 + j]
q = Query(grades_table)
transaction.add_query(q.select, key, 0, [1, 1, 1, 1, 1])
q = Query(grades_table)
transaction.add_query(q.increment, key, 1)
transaction_workers[i % num_threads].add_transaction(transaction)
threads = []
for transaction_worker in transaction_workers:
threads.append(threading.Thread(target = transaction_worker.run, args = ()))
for i, thread in enumerate(threads):
print('Thread', i, 'started')
thread.start()
for i, thread in enumerate(threads):
thread.join()
print('Thread', i, 'finished')
num_committed_transactions = sum(t.result for t in transaction_workers)
print(num_committed_transactions, 'transaction committed.')
query = Query(grades_table)
s = query.sum(keys[0], keys[-1], 1)
if s != num_committed_transactions * 5:
print('Expected sum:', num_committed_transactions * 5, ', actual:', s, '. Failed.')
else:
print('Expected sum:', num_committed_transactions * 5, ', actual:', s, '. Success')
print('Pass.')