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