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