Table compiles
[iotcloud.git] / PyORAM / examples / path_oram_s3.py
1 #
2 # This example measures the performance of Path ORAM when
3 # storage is accessed 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.oblivious_storage.tree.path_oram import \
22     PathORAM
23
24 import tqdm
25
26 pyoram.config.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               PathORAM.compute_storage_size(
51                   block_size,
52                   block_count,
53                   storage_type='s3'))))
54     print("")
55
56     print("Setting Up Path ORAM Storage")
57     setup_start = time.time()
58     with PathORAM.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: %.2f s"
65               % (time.time()-setup_start))
66         print("Current Stash Size: %s"
67               % len(f.stash))
68         print("Total Data Transmission: %s"
69               % (MemorySize(f.bytes_sent + f.bytes_received)))
70         print("")
71
72     # We close the device and reopen it after
73     # setup to reset the bytes sent and bytes
74     # received stats.
75     with PathORAM(storage_name,
76                   f.stash,
77                   f.position_map,
78                   key=f.key,
79                   storage_type='s3',
80                   bucket_name=bucket_name) as f:
81
82         test_count = 100
83         start_time = time.time()
84         for t in tqdm.tqdm(list(range(test_count)),
85                            desc="Running I/O Performance Test"):
86             f.read_block(random.randint(0,f.block_count-1))
87         stop_time = time.time()
88         print("Current Stash Size: %s"
89               % len(f.stash))
90         print("Access Block Avg. Data Transmitted: %s (%.3fx)"
91               % (MemorySize((f.bytes_sent + f.bytes_received)/float(test_count)),
92                  (f.bytes_sent + f.bytes_received)/float(test_count)/float(block_size)))
93         print("Fetch Block Avg. Latency: %.2f ms"
94               % ((stop_time-start_time)/float(test_count)*1000))
95         print("")
96
97 if __name__ == "__main__":
98     main()                                             # pragma: no cover