Skip to content

Commit d28fe19

Browse files
authored
add performance benchmarking code
1 parent 00885c1 commit d28fe19

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

bench/bench.py

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
from cryptomite.dodis import Dodis
2+
from cryptomite.toeplitz import Toeplitz
3+
from cryptomite.utils import von_neumann
4+
from cryptomite.trevisan import Trevisan
5+
from numpy.random import randint
6+
import numpy as np
7+
8+
from time import time
9+
10+
import pandas as pd
11+
12+
13+
cache1 = dict()
14+
cache2 = dict()
15+
16+
N_REPEATS = 5
17+
18+
seed_caches = [dict() for _ in range(N_REPEATS)]
19+
cache3 = dict()
20+
def get_random(cache, n):
21+
if n not in cache:
22+
cache[n] = randint(0, 2, size=n)
23+
24+
return cache[n]
25+
26+
# compare with naive
27+
28+
inp_lens = [x * 10 ** n for x in [10, 18, 32, 56] for n in range(1, 10)]
29+
# inp_lens = [10 ** 8]
30+
names = ["Extractor", "Input Length", "Ratio"]
31+
#ratios = (0.5, 0.75)
32+
ratios = [0.5]
33+
columns = ["Wait (s)"]
34+
indices = []
35+
rows = []
36+
37+
# Dodis
38+
print("\nDodis: ", end="", flush=True)
39+
for inp_len in inp_lens:
40+
inp1 = get_random(cache1, inp_len)
41+
inp2 = get_random(cache2, inp_len)
42+
43+
for ratio in ratios:
44+
waits = []
45+
for i in range(N_REPEATS):
46+
inp2 = get_random(seed_caches[i], inp_len)
47+
out_len = int(inp_len * ratio)
48+
ext = Dodis(inp_len, out_len)
49+
start = time()
50+
ext.extract(inp1, inp2)
51+
wait = time() - start
52+
waits.append(wait)
53+
print(".", end="", flush=True)
54+
print("|", end="", flush=True)
55+
rows.append(sum(waits) / len(waits))
56+
indices.append(('Dodis', inp_len, ratio))
57+
58+
# VN
59+
print("\nVN: ", end="", flush=True)
60+
for inp_len in inp_lens:
61+
waits = []
62+
for i in range(N_REPEATS):
63+
inp1 = get_random(seed_caches[i], inp_len)
64+
start = time()
65+
von_neumann(inp1)
66+
wait = time() - start
67+
waits.append(wait)
68+
print(".", end="", flush=True)
69+
print("|", end="", flush=True)
70+
rows.append(sum(waits) / len(waits))
71+
indices.append(('Von Neumann', inp_len, np.nan))
72+
73+
# Toeplitz
74+
print("\nToeplitz", end="", flush=True)
75+
for inp_len in inp_lens:
76+
inp1 = get_random(cache1, inp_len)
77+
78+
for ratio in ratios:
79+
out_len = int(inp_len * ratio)
80+
waits = []
81+
for i in range(N_REPEATS):
82+
seed = get_random(seed_caches[i], inp_len + out_len - 1)
83+
ext = Toeplitz(inp_len, out_len)
84+
start = time()
85+
ext.extract(inp1, seed)
86+
wait = time() - start
87+
waits.append(wait)
88+
print(".", end="", flush=True)
89+
print("|", end="", flush=True)
90+
rows.append(sum(waits) / len(waits))
91+
indices.append(('Toeplitz', inp_len, ratio))
92+
93+
# Trevisan
94+
print("\nTrevisan: ", end="", flush=True)
95+
for inp_len in inp_lens:
96+
if inp_len > 10000:
97+
continue
98+
inp1 = get_random(cache1, inp_len)
99+
100+
for ratio in ratios:
101+
min_ent = int(inp_len * ratio)
102+
ext = Trevisan(inp_len, min_ent, 2 ** -20)
103+
seed_length = ext.ext.get_seed_length()
104+
waits = []
105+
for i in range(N_REPEATS):
106+
seed = get_random(seed_caches[i], seed_length)
107+
start = time()
108+
ext.extract(inp1, seed)
109+
wait = time() - start
110+
waits.append(wait)
111+
print(".", end="", flush=True)
112+
rows.append(sum(waits) / len(waits))
113+
indices.append(('Trevisan', inp_len, ratio))
114+
print("")
115+
116+
index = pd.MultiIndex.from_tuples(indices, names=names)
117+
118+
df = pd.DataFrame(rows, index=index, columns=columns)
119+
120+
print(df)
121+
122+
ndf = df.copy().reset_index()
123+
ndf.loc[ndf["Extractor"] == "Von Neumann", "Ratio"] = 0.5
124+
ndf["Rate"] = ndf["Input Length"] * ndf["Ratio"] / ndf["Wait (s)"]

0 commit comments

Comments
 (0)