PyORAm
[iotcloud.git] / PyORAM / examples / encrypted_storage_file.py
1 #
2 # This example measures the performance of encrypted
3 # storage access 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.encrypted_storage.encrypted_block_storage import \
13     EncryptedBlockStorage
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               EncryptedBlockStorage.compute_storage_size(
37                   block_size,
38                   block_count,
39                   storage_type='file'))))
40     print("")
41
42     print("Setting Up Encrypted Block Storage")
43     setup_start = time.time()
44     with EncryptedBlockStorage.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("Total Data Transmission: %s"
52               % (MemorySize(f.bytes_sent + f.bytes_received)))
53         print("")
54
55     # We close the device and reopen it after
56     # setup to reset the bytes sent and bytes
57     # received stats.
58     with EncryptedBlockStorage(storage_name,
59                                key=f.key,
60                                storage_type='file') as f:
61
62         test_count = 1000
63         start_time = time.time()
64         for t in tqdm.tqdm(list(range(test_count)),
65                            desc="Running I/O Performance Test"):
66             f.read_block(random.randint(0,f.block_count-1))
67         stop_time = time.time()
68         print("Access Block Avg. Data Transmitted: %s (%.3fx)"
69               % (MemorySize((f.bytes_sent + f.bytes_received)/float(test_count)),
70                  (f.bytes_sent + f.bytes_received)/float(test_count)/float(block_size)))
71         print("Access Block Avg. Latency: %.2f ms"
72               % ((stop_time-start_time)/float(test_count)*1000))
73         print("")
74
75     # cleanup because this is a test example
76     os.remove(storage_name)
77
78 if __name__ == "__main__":
79     main()                                             # pragma: no cover