Table compiles
[iotcloud.git] / PyORAM / examples / path_oram_file.py
1 #
2 # This example measures the performance of Path ORAM
3 # when storage is accessed through a local file.
4 #
5
6 import os
7 import random
8 import time
9
10 import pyoram
11 from pyoram.util.misc import MemorySize
12 from pyoram.oblivious_storage.tree.path_oram import \
13     PathORAM
14
15 import tqdm
16
17 pyoram.config.SHOW_PROGRESS_BAR = True
18
19 # Set the storage location and size
20 storage_name = "heap.bin"
21 # 4KB block size
22 block_size = 4000
23 # one block per bucket in the
24 # storage heap of height 8
25 block_count = 2**(8+1)-1
26
27 def main():
28
29     print("Storage Name: %s" % (storage_name))
30     print("Block Count: %s" % (block_count))
31     print("Block Size: %s" % (MemorySize(block_size)))
32     print("Total Memory: %s"
33           % (MemorySize(block_size*block_count)))
34     print("Actual Storage Required: %s"
35           % (MemorySize(
36               PathORAM.compute_storage_size(
37                   block_size,
38                   block_count,
39                   storage_type='file'))))
40     print("")
41
42     print("Setting Up Path ORAM Storage")
43     setup_start = time.time()
44     with PathORAM.setup(storage_name,
45                         block_size,
46                         block_count,
47                         storage_type='file',
48                         ignore_existing=True) as f:
49         print("Total Setup Time: %2.f s"
50               % (time.time()-setup_start))
51         print("Current Stash Size: %s"
52               % len(f.stash))
53         print("Total Data Transmission: %s"
54               % (MemorySize(f.bytes_sent + f.bytes_received)))
55         print("")
56
57     # We close the device and reopen it after
58     # setup to reset the bytes sent and bytes
59     # received stats.
60     with PathORAM(storage_name,
61                   f.stash,
62                   f.position_map,
63                   key=f.key,
64                   storage_type='file') as f:
65
66         test_count = 100
67         start_time = time.time()
68         for t in tqdm.tqdm(list(range(test_count)),
69                            desc="Running I/O Performance Test"):
70             f.read_block(random.randint(0,f.block_count-1))
71         stop_time = time.time()
72         print("Current Stash Size: %s"
73               % len(f.stash))
74         print("Access Block Avg. Data Transmitted: %s (%.3fx)"
75               % (MemorySize((f.bytes_sent + f.bytes_received)/float(test_count)),
76                  (f.bytes_sent + f.bytes_received)/float(test_count)/float(block_size)))
77         print("Access Block Avg. Latency: %.2f ms"
78               % ((stop_time-start_time)/float(test_count)*1000))
79         print("")
80
81     # cleanup because this is a test example
82     os.remove(storage_name)
83
84 if __name__ == "__main__":
85     main()                                             # pragma: no cover