Fixing timer for encrypted storage benchmark.
[iotcloud.git] / PyORAM / examples / encrypted_storage_ram.py
1 #
2 # This example measures the performance of encrypted
3 # storage access 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.encrypted_storage.encrypted_block_storage import \
13     EncryptedBlockStorage
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               EncryptedBlockStorage.compute_storage_size(
39                   block_size,
40                   block_count,
41                   storage_type='ram'))))
42     print("")
43
44     print("Setting Up Encrypted Block Storage")
45     setup_start = time.time()
46     with EncryptedBlockStorage.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("Total Data Transmission: %s"
54               % (MemorySize(f.bytes_sent + f.bytes_received)))
55         print("")
56
57     # This must be done after closing the file to ensure the lock flag
58     # is set to False in the saved data. The tofile method only exists
59     # on BlockStorageRAM
60     f.raw_storage.tofile(storage_name)
61
62     # We close the device and reopen it after
63     # setup to reset the bytes sent and bytes
64     # received stats.
65     with EncryptedBlockStorage(BlockStorageRAM.fromfile(storage_name),
66                                key=f.key) as f:
67
68         test_count = 1000
69         start_time = time.time()
70         for t in tqdm.tqdm(list(range(test_count)),
71                            desc="Running I/O Performance Test"):
72             f.read_block(random.randint(0,f.block_count-1))
73         stop_time = time.time()
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