Adding test for encrypted cloud storage; Adding preparation timing test for Fidelius...
[iotcloud.git] / PyORAM / examples / es.py
1 #
2 # This example measures the performance of encrypted storage
3 # access through an SSH client using the Secure File
4 # 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
14 import pyoram
15 from pyoram.util.misc import MemorySize
16 from pyoram.encrypted_storage.encrypted_block_storage import \
17     EncryptedBlockStorage
18
19 from AliTimer import *
20
21 import paramiko
22 import tqdm
23
24 pyoram.config.SHOW_PROGRESS_BAR = True
25
26 # Set SSH login credentials here
27 # (by default, we pull these from the environment
28 # for testing purposes)
29 ssh_host = os.environ.get('PYORAM_SSH_TEST_HOST')
30 ssh_username = os.environ.get('PYORAM_SSH_TEST_USERNAME')
31 ssh_password = os.environ.get('PYORAM_SSH_TEST_PASSWORD')
32
33 # Set the storage location and size
34 storage_name = "heap.bin"
35 # 4KB block size
36 #block_size = 4000
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
42 def main():
43     timer = Foo.Instance()
44
45     print("Storage Name: %s" % (storage_name))
46     print("Block Count: %s" % (block_count))
47     print("Block Size: %s" % (MemorySize(block_size)))
48     print("Total Memory: %s"
49           % (MemorySize(block_size*block_count)))
50     print("Actual Storage Required: %s"
51           % (MemorySize(
52               EncryptedBlockStorage.compute_storage_size(
53                   block_size,
54                   block_count,
55                   storage_type='sftp'))))
56     print("")
57
58     # Start an SSH client using paramiko
59     print("Starting SSH Client")
60     with paramiko.SSHClient() as ssh:
61         ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
62         ssh.load_system_host_keys()
63         ssh.connect(ssh_host,
64                     username=ssh_username,
65                     password=ssh_password)
66
67         print("Setting Up Encrypted Block Storage")
68         setup_start = time.time()
69         with EncryptedBlockStorage.setup(storage_name,
70                                          block_size,
71                                          block_count,
72                                          storage_type='sftp',
73                                          sshclient=ssh,
74                                          ignore_existing=True) as f:
75             print("Total Setup Time: %2.f s"
76                   % (time.time()-setup_start))
77             print("Total Data Transmission: %s"
78                   % (MemorySize(f.bytes_sent + f.bytes_received)))
79             print("")
80
81         # We close the device and reopen it after
82         # setup to reset the bytes sent and bytes
83         # received stats.
84         with EncryptedBlockStorage(storage_name,
85                                    key=f.key,
86                                    storage_type='sftp',
87                                    sshclient=ssh) as f:
88
89             test_count = 1000
90             start_time = time.time()
91             for t in tqdm.tqdm(list(range(test_count)),
92                                desc="Running I/O Performance Test"):
93                 block = f.read_block(random.randint(0,f.block_count-1))
94             stop_time = time.time()
95             print("Access Block Avg. Data Transmitted: %s (%.3fx)"
96                   % (MemorySize((f.bytes_sent + f.bytes_received)/float(test_count)),
97                      (f.bytes_sent + f.bytes_received)/float(test_count)/float(block_size)))
98             print("Access Block Avg. Latency: %.2f ms"
99                   % ((stop_time-start_time)/float(test_count)*1000))
100             print("")
101
102
103             keys = []
104             keys.extend(range(0, block_count))
105             random.shuffle(keys)
106
107             print("Starting Ali Test 2")
108             timer.resetTimer()
109             test_count = block_count
110             start_time = time.time()
111             
112             for t in tqdm.tqdm(list(range(test_count)), desc="Running I/O Performance Test"):
113               ind = keys[t]
114               # ind = t
115               s = "a" + str(ind)                
116               f.write_block(ind, block)
117             print("Total Data Transmission: %s" % (MemorySize(f.bytes_sent + f.bytes_received)))
118             
119     stop_time = time.time()
120     print("Test Processing Time: " + str(stop_time - start_time))
121     print("Test Network Time: " + str(timer.getTime()))
122     print("")
123
124 if __name__ == "__main__":
125     main()                                             # pragma: no cover