Adding Fidelius manual.
[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("Test Setup Time: " + str(time.time() - setup_start))
78             print("Total Data Transmission: %s"
79                   % (MemorySize(f.bytes_sent + f.bytes_received)))
80             print("")
81
82         # We close the device and reopen it after
83         # setup to reset the bytes sent and bytes
84         # received stats.
85         with EncryptedBlockStorage(storage_name,
86                                    key=f.key,
87                                    storage_type='sftp',
88                                    sshclient=ssh) as f:
89
90             test_count = 1000
91             start_time = time.time()
92             for t in tqdm.tqdm(list(range(test_count)),
93                                desc="Running I/O Performance Test"):
94                 block = f.read_block(random.randint(0,f.block_count-1))
95             stop_time = time.time()
96             print("Access Block Avg. Data Transmitted: %s (%.3fx)"
97                   % (MemorySize((f.bytes_sent + f.bytes_received)/float(test_count)),
98                      (f.bytes_sent + f.bytes_received)/float(test_count)/float(block_size)))
99             print("Access Block Avg. Latency: %.2f ms"
100                   % ((stop_time-start_time)/float(test_count)*1000))
101             print("")
102
103
104             keys = []
105             keys.extend(range(0, block_count))
106             random.shuffle(keys)
107
108             print("Starting Ali Test 2")
109             timer.resetTimer()
110             test_count = block_count
111             start_time = time.time()
112             ind = block_count
113
114             for t in tqdm.tqdm(list(range(test_count)), desc="Running I/O Performance Test"):
115               ind = keys[t]
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