Table compiles
[iotcloud.git] / PyORAM / examples / ali.py
1 #
2 # This example measures the performance of Path ORAM when
3 # storage is accessed through an SSH client using the Secure
4 # File Transfer Protocol (SFTP).
5 #
6 # In order to run this example, you must provide a host
7 # (server) address along with valid login credentials
8 #
9
10 import os
11 import random
12 import time
13 import array
14
15 import pyoram
16 from pyoram.util.misc import MemorySize
17 from pyoram.oblivious_storage.tree.path_oram import \
18     PathORAM
19
20 from pyoram.storage.AliTimer import *
21
22 import paramiko
23 import tqdm
24
25 pyoram.config.SHOW_PROGRESS_BAR = True
26
27 # Set SSH login credentials here
28 # (by default, we pull these from the environment
29 # for testing purposes)
30 ssh_host = os.environ.get('PYORAM_SSH_TEST_HOST')
31 ssh_username = os.environ.get('PYORAM_SSH_TEST_USERNAME')
32 ssh_password = os.environ.get('PYORAM_SSH_TEST_PASSWORD')
33
34 # Set the storage location and size
35 storage_name = "heap.bin"
36 # 4KB block size
37 block_size = 2048
38 # one block per bucket in the
39 # storage heap of height 8
40 # block_count = 2**(8+1)-1
41 # block_count = 2**(12+1)-1
42 # block_count = 2**(15+1)-1
43 block_count = 2**(8+1)-1
44
45 def main():
46     timer = Foo.Instance()
47     
48     print("Storage Name: %s" % (storage_name))
49     print("Block Count: %s" % (block_count))
50     print("Block Size: %s" % (MemorySize(block_size)))
51     print("Total Memory: %s"
52           % (MemorySize(block_size*block_count)))
53     print("Actual Storage Required: %s"
54           % (MemorySize(
55               PathORAM.compute_storage_size(
56                   block_size,
57                   block_count,
58                   storage_type='sftp'))))
59     print("")
60
61     # Start an SSH client using paramiko
62     print("Starting SSH Client")
63     with paramiko.SSHClient() as ssh:
64         ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
65         ssh.load_system_host_keys()
66         ssh.connect(ssh_host,
67                     username=ssh_username,
68                     password=ssh_password)
69
70         print("Setting Up Path ORAM Storage")
71         start_time = time.time()
72         with PathORAM.setup(storage_name,
73                             block_size,
74                             block_count,
75                             storage_type='sftp',
76                             sshclient=ssh,
77                             cached_levels=2,
78                             concurrency_level = 0,
79                             ignore_existing=True) as f:
80             print("Total Data Transmission: %s" % (MemorySize(f.bytes_sent + f.bytes_received)))
81             print("Done Initializing")
82             pass
83
84         stop_time = time.time()
85         print("Initial Setup Processing Time: " + str(stop_time - start_time))
86         print("Initial Setup Network Time: " + str(timer.getTime()))
87         print("")
88
89             # print("Total Setup Time: %.2f s"
90             #       % (time.time()-setup_start))
91             # print("Current Stash Size: %s"
92             #       % len(f.stash))
93            
94             # print("")
95
96         start_time = time.time()
97         timer.resetTimer()
98         # We close the device and reopen it after
99         # setup to reset the bytes sent and bytes
100         # received stats.
101
102         print("Starting test Run...")
103
104
105         with PathORAM(storage_name,
106                       f.stash,
107                       f.position_map,
108                       key=f.key,
109                       cached_levels=2,
110                       concurrency_level = 0,
111                       storage_type='sftp',
112                       sshclient=ssh) as f:
113             
114             stop_time = time.time()
115             print("Total Data Transmission: %s" % (MemorySize(f.bytes_sent + f.bytes_received)))
116             print("Test Setup Processing Time: " + str(stop_time - start_time))
117             print("Test Setup Network Time: " + str(timer.getTime()))
118             print("")
119
120
121
122
123
124             keys = []
125             keys.extend(range(0, block_count))
126             random.shuffle(keys)
127
128
129             print("Starting Ali Test 2")
130             timer.resetTimer()
131             test_count = block_count
132             start_time = time.time()
133             
134             for t in tqdm.tqdm(list(range(test_count)), desc="Running I/O Performance Test"):
135               ind = keys[t]
136               # ind = t
137               s = "a" + str(ind)                
138               f.write_block(ind,  bytearray(s.ljust(block_size, '\0')))
139             print("Total Data Transmission: %s" % (MemorySize(f.bytes_sent + f.bytes_received)))
140             
141             # for t in tqdm.tqdm(list(range(test_count)), desc="Running I/O Performance Test"):
142             #   ind = keys[t]
143             #   # print(f.read_block(ind))
144             #   f.read_block(ind)
145
146
147
148     stop_time = time.time()
149     print("Test Processing Time: " + str(stop_time - start_time))
150     print("Test Network Time: " + str(timer.getTime()))
151     print("")
152
153
154             
155
156
157             # print("Current Stash Size: %s"
158             #       % len(f.stash))
159             # print("Access Block Avg. Data Transmitted: %s (%.3fx)"
160             #       % (MemorySize((f.bytes_sent + f.bytes_received)/float(test_count)),
161             #          (f.bytes_sent + f.bytes_received)/float(test_count)/float(block_size)))
162             # print("Fetch Block Avg. Latency: %.2f ms"
163             #       % ((stop_time-start_time)/float(test_count)*1000))
164             # print("")
165             # print("")
166             # print("")
167             # print("")
168             # print("")
169             # print("") 
170
171            
172
173
174
175 if __name__ == "__main__":
176     main()                                             # pragma: no cover