Table compiles
[iotcloud.git] / PyORAM / examples / encrypted_storage_sftp.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 import paramiko
20 import tqdm
21
22 pyoram.config.SHOW_PROGRESS_BAR = True
23
24 # Set SSH login credentials here
25 # (by default, we pull these from the environment
26 # for testing purposes)
27 ssh_host = os.environ.get('PYORAM_SSH_TEST_HOST')
28 ssh_username = os.environ.get('PYORAM_SSH_TEST_USERNAME')
29 ssh_password = os.environ.get('PYORAM_SSH_TEST_PASSWORD')
30
31 # Set the storage location and size
32 storage_name = "heap.bin"
33 # 4KB block size
34 block_size = 4000
35 # one block per bucket in the
36 # storage heap of height 8
37 block_count = 2**(8+1)-1
38
39 def main():
40
41     print("Storage Name: %s" % (storage_name))
42     print("Block Count: %s" % (block_count))
43     print("Block Size: %s" % (MemorySize(block_size)))
44     print("Total Memory: %s"
45           % (MemorySize(block_size*block_count)))
46     print("Actual Storage Required: %s"
47           % (MemorySize(
48               EncryptedBlockStorage.compute_storage_size(
49                   block_size,
50                   block_count,
51                   storage_type='sftp'))))
52     print("")
53
54     # Start an SSH client using paramiko
55     print("Starting SSH Client")
56     with paramiko.SSHClient() as ssh:
57         ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
58         ssh.load_system_host_keys()
59         ssh.connect(ssh_host,
60                     username=ssh_username,
61                     password=ssh_password)
62
63         print("Setting Up Encrypted Block Storage")
64         setup_start = time.time()
65         with EncryptedBlockStorage.setup(storage_name,
66                                          block_size,
67                                          block_count,
68                                          storage_type='sftp',
69                                          sshclient=ssh,
70                                          ignore_existing=True) as f:
71             print("Total Setup Time: %2.f s"
72                   % (time.time()-setup_start))
73             print("Total Data Transmission: %s"
74                   % (MemorySize(f.bytes_sent + f.bytes_received)))
75             print("")
76
77         # We close the device and reopen it after
78         # setup to reset the bytes sent and bytes
79         # received stats.
80         with EncryptedBlockStorage(storage_name,
81                                    key=f.key,
82                                    storage_type='sftp',
83                                    sshclient=ssh) as f:
84
85             test_count = 1000
86             start_time = time.time()
87             for t in tqdm.tqdm(list(range(test_count)),
88                                desc="Running I/O Performance Test"):
89                 f.read_block(random.randint(0,f.block_count-1))
90             stop_time = time.time()
91             print("Access Block Avg. Data Transmitted: %s (%.3fx)"
92                   % (MemorySize((f.bytes_sent + f.bytes_received)/float(test_count)),
93                      (f.bytes_sent + f.bytes_received)/float(test_count)/float(block_size)))
94             print("Access Block Avg. Latency: %.2f ms"
95                   % ((stop_time-start_time)/float(test_count)*1000))
96             print("")
97
98 if __name__ == "__main__":
99     main()                                             # pragma: no cover