2 # This example measures the performance of encrypted
3 # storage access through RAM.
11 from pyoram.util.misc import MemorySize
12 from pyoram.encrypted_storage.encrypted_block_storage import \
14 from pyoram.storage.block_storage_ram import \
19 pyoram.config.SHOW_PROGRESS_BAR = True
21 # Set the storage location and size
22 storage_name = "heap.bin"
25 # one block per bucket in the
26 # storage heap of height 8
27 block_count = 2**(8+1)-1
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"
38 EncryptedBlockStorage.compute_storage_size(
41 storage_type='ram'))))
44 print("Setting Up Encrypted Block Storage")
45 setup_start = time.time()
46 with EncryptedBlockStorage.setup(storage_name, # RAM storage ignores this argument
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)))
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
60 f.raw_storage.tofile(storage_name)
62 # We close the device and reopen it after
63 # setup to reset the bytes sent and bytes
65 with EncryptedBlockStorage(BlockStorageRAM.fromfile(storage_name),
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))
81 # cleanup because this is a test example
82 os.remove(storage_name)
84 if __name__ == "__main__":
85 main() # pragma: no cover