-
Notifications
You must be signed in to change notification settings - Fork 1
/
collision_check.py
99 lines (68 loc) · 2.07 KB
/
collision_check.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
from ita import Loader, Parser, Generator
import sys
import ita
import sys
import math
from hashlib import md5
from threading import Thread, Event
import queue
import sqlite3
SAMPLES = 600
WORKERS = 2
NONTERMINAL = sys.argv[1] if len(sys.argv) > 1 else "cviceni2"
connection = sqlite3.connect(":memory:")
connection.isolation_level = None # vypnutí relací
cursor = connection.cursor()
query = cursor.execute
query("CREATE TABLE collisions (hash char(32) PRIMARY KEY NOT NULL)")
ita.VERBOSE = False
# BUFFER > WORKERS
BUFFER = 500
shouldDie = Event()
shouldDie.clear()
pendingResolve = queue.Queue(BUFFER)
threads = []
processed = 0
collisions_count = 0;
hashes = []
class WorkerThread(Thread):
def run(self):
global writeAccess, shouldDie
loader = Loader("sablony")
parser = Parser( loader )
generator = Generator( parser )
while not shouldDie.is_set():
text = generator.run(NONTERMINAL)
hashed = md5(text.encode('utf-8')).hexdigest()
pendingResolve.put(hashed);
for dummy in range(WORKERS):
t = WorkerThread()
t.start()
threads.append(t)
BAR_SIZE = 30
COEF = BAR_SIZE/BUFFER
try:
while processed < SAMPLES:
processed += 1
hashed = pendingResolve.get()
# if processed % 500 == 0:
# bar = math.floor(pendingResolve.qsize()*COEF)
# print("["+ ("*"*bar).ljust(BAR_SIZE)+"]", processed)
try:
query("INSERT INTO collisions VALUES ('%s')" % hashed)
except sqlite3.IntegrityError:
collisions_count += 1
pendingResolve.task_done()
sys.stdout.flush()
except KeyboardInterrupt:
pass
print("%d/%d = %1.2f%%" % (collisions_count, SAMPLES, round(collisions_count/SAMPLES*100,2)))
shouldDie.set()
# vycistime zbytek fronty, coz umozni ukonceni vlaknum ktere cekaji na zapis do ni
try:
while True: pendingResolve.get(False)
except queue.Empty:
pass
# pockame na ukonceni vsech vlaken
for thread in threads:
thread.join()