Fixing timer for encrypted storage benchmark.
[iotcloud.git] / PyORAM / examples / encrypted_storage_s3.py
1 #
2 # This example measures the performance of encrypted
3 # storage access through Amazon Simple Storage Service
4 # (S3).
5 #
6 # In order to run this example, you must provide a valid
7 # S3 bucket name and have the following variables defined
8 # in your current environment:
9 #  - AWS_ACCESS_KEY_ID
10 #  - AWS_SECRET_ACCESS_KEY
11 #  - AWS_DEFAULT_REGION
12 # These can also be set using keywords.
13 #
14
15 import os
16 import random
17 import time
18
19 import pyoram
20 from pyoram.util.misc import MemorySize
21 from pyoram.encrypted_storage.encrypted_block_storage import \
22     EncryptedBlockStorage
23
24 import tqdm
25
26 pyoram.SHOW_PROGRESS_BAR = True
27
28 # Set S3 bucket name here
29 # (by default, we pull this from the environment
30 # for testing purposes)
31 bucket_name = os.environ.get('PYORAM_AWS_TEST_BUCKET')
32
33 # Set the storage location and size
34 storage_name = "heap.bin"
35 # 4KB block size
36 block_size = 4000
37 # one block per bucket in the
38 # storage heap of height 8
39 block_count = 2**(8+1)-1
40
41 def main():
42
43     print("Storage Name: %s" % (storage_name))
44     print("Block Count: %s" % (block_count))
45     print("Block Size: %s" % (MemorySize(block_size)))
46     print("Total Memory: %s"
47           % (MemorySize(block_size*block_count)))
48     print("Actual Storage Required: %s"
49           % (MemorySize(
50               EncryptedBlockStorage.compute_storage_size(
51                   block_size,
52                   block_count,
53                   storage_type='s3'))))
54     print("")
55
56     print("Setting Up Encrypted Block Storage")
57     setup_start = time.time()
58     with EncryptedBlockStorage.setup(storage_name,
59                                      block_size,
60                                      block_count,
61                                      storage_type='s3',
62                                      bucket_name=bucket_name,
63                                      ignore_existing=True) as f:
64         print("Total Setup Time: %2.f s"
65               % (time.time()-setup_start))
66         print("Total Data Transmission: %s"
67               % (MemorySize(f.bytes_sent + f.bytes_received)))
68         print("")
69
70     # We close the device and reopen it after
71     # setup to reset the bytes sent and bytes
72     # received stats.
73     with EncryptedBlockStorage(storage_name,
74                                key=f.key,
75                                storage_type='s3',
76                                bucket_name=bucket_name) as f:
77
78         test_count = 1000
79         start_time = time.time()
80         for t in tqdm.tqdm(list(range(test_count)),
81                            desc="Running I/O Performance Test"):
82             f.read_block(random.randint(0,f.block_count-1))
83         stop_time = time.time()
84         print("Access Block Avg. Data Transmitted: %s (%.3fx)"
85               % (MemorySize((f.bytes_sent + f.bytes_received)/float(test_count)),
86                  (f.bytes_sent + f.bytes_received)/float(test_count)/float(block_size)))
87         print("Access Block Avg. Latency: %.2f ms"
88               % ((stop_time-start_time)/float(test_count)*1000))
89         print("")
90
91 if __name__ == "__main__":
92     main()                                             # pragma: no cover