2 # This example measures the performance of encrypted
3 # storage access through a local file.
11 from pyoram.util.misc import MemorySize
12 from pyoram.encrypted_storage.encrypted_block_storage import \
17 pyoram.config.SHOW_PROGRESS_BAR = True
19 # Set the storage location and size
20 storage_name = "heap.bin"
23 # one block per bucket in the
24 # storage heap of height 8
25 block_count = 2**(8+1)-1
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"
36 EncryptedBlockStorage.compute_storage_size(
39 storage_type='file'))))
42 print("Setting Up Encrypted Block Storage")
43 setup_start = time.time()
44 with EncryptedBlockStorage.setup(storage_name,
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)))
55 # We close the device and reopen it after
56 # setup to reset the bytes sent and bytes
58 with EncryptedBlockStorage(storage_name,
60 storage_type='file') as f:
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))
75 # cleanup because this is a test example
76 os.remove(storage_name)
78 if __name__ == "__main__":
79 main() # pragma: no cover