PyORAm
[iotcloud.git] / PyORAM / examples / ali.py
diff --git a/PyORAM/examples/ali.py b/PyORAM/examples/ali.py
new file mode 100644 (file)
index 0000000..f758de3
--- /dev/null
@@ -0,0 +1,176 @@
+#
+# This example measures the performance of Path ORAM when
+# storage is accessed through an SSH client using the Secure
+# File Transfer Protocol (SFTP).
+#
+# In order to run this example, you must provide a host
+# (server) address along with valid login credentials
+#
+
+import os
+import random
+import time
+import array
+
+import pyoram
+from pyoram.util.misc import MemorySize
+from pyoram.oblivious_storage.tree.path_oram import \
+    PathORAM
+
+from pyoram.storage.AliTimer import *
+
+import paramiko
+import tqdm
+
+pyoram.config.SHOW_PROGRESS_BAR = True
+
+# Set SSH login credentials here
+# (by default, we pull these from the environment
+# for testing purposes)
+ssh_host = os.environ.get('PYORAM_SSH_TEST_HOST')
+ssh_username = os.environ.get('PYORAM_SSH_TEST_USERNAME')
+ssh_password = os.environ.get('PYORAM_SSH_TEST_PASSWORD')
+
+# Set the storage location and size
+storage_name = "heap.bin"
+# 4KB block size
+block_size = 2048
+# one block per bucket in the
+# storage heap of height 8
+# block_count = 2**(8+1)-1
+# block_count = 2**(12+1)-1
+# block_count = 2**(15+1)-1
+block_count = 2**(8+1)-1
+
+def main():
+    timer = Foo.Instance()
+    
+    print("Storage Name: %s" % (storage_name))
+    print("Block Count: %s" % (block_count))
+    print("Block Size: %s" % (MemorySize(block_size)))
+    print("Total Memory: %s"
+          % (MemorySize(block_size*block_count)))
+    print("Actual Storage Required: %s"
+          % (MemorySize(
+              PathORAM.compute_storage_size(
+                  block_size,
+                  block_count,
+                  storage_type='sftp'))))
+    print("")
+
+    # Start an SSH client using paramiko
+    print("Starting SSH Client")
+    with paramiko.SSHClient() as ssh:
+        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
+        ssh.load_system_host_keys()
+        ssh.connect(ssh_host,
+                    username=ssh_username,
+                    password=ssh_password)
+
+        print("Setting Up Path ORAM Storage")
+        start_time = time.time()
+        with PathORAM.setup(storage_name,
+                            block_size,
+                            block_count,
+                            storage_type='sftp',
+                            sshclient=ssh,
+                            cached_levels=2,
+                            concurrency_level = 0,
+                            ignore_existing=True) as f:
+            print("Total Data Transmission: %s" % (MemorySize(f.bytes_sent + f.bytes_received)))
+            print("Done Initializing")
+            pass
+
+        stop_time = time.time()
+        print("Initial Setup Processing Time: " + str(stop_time - start_time))
+        print("Initial Setup Network Time: " + str(timer.getTime()))
+        print("")
+
+            # print("Total Setup Time: %.2f s"
+            #       % (time.time()-setup_start))
+            # print("Current Stash Size: %s"
+            #       % len(f.stash))
+           
+            # print("")
+
+        start_time = time.time()
+        timer.resetTimer()
+        # We close the device and reopen it after
+        # setup to reset the bytes sent and bytes
+        # received stats.
+
+        print("Starting test Run...")
+
+
+        with PathORAM(storage_name,
+                      f.stash,
+                      f.position_map,
+                      key=f.key,
+                      cached_levels=2,
+                      concurrency_level = 0,
+                      storage_type='sftp',
+                      sshclient=ssh) as f:
+            
+            stop_time = time.time()
+            print("Total Data Transmission: %s" % (MemorySize(f.bytes_sent + f.bytes_received)))
+            print("Test Setup Processing Time: " + str(stop_time - start_time))
+            print("Test Setup Network Time: " + str(timer.getTime()))
+            print("")
+
+
+
+
+
+            keys = []
+            keys.extend(range(0, block_count))
+            random.shuffle(keys)
+
+
+            print("Starting Ali Test 2")
+            timer.resetTimer()
+            test_count = block_count
+            start_time = time.time()
+            
+            for t in tqdm.tqdm(list(range(test_count)), desc="Running I/O Performance Test"):
+              ind = keys[t]
+              # ind = t
+              s = "a" + str(ind)                
+              f.write_block(ind,  bytearray(s.ljust(block_size, '\0')))
+            print("Total Data Transmission: %s" % (MemorySize(f.bytes_sent + f.bytes_received)))
+            
+            # for t in tqdm.tqdm(list(range(test_count)), desc="Running I/O Performance Test"):
+            #   ind = keys[t]
+            #   # print(f.read_block(ind))
+            #   f.read_block(ind)
+
+
+
+    stop_time = time.time()
+    print("Test Processing Time: " + str(stop_time - start_time))
+    print("Test Network Time: " + str(timer.getTime()))
+    print("")
+
+
+            
+
+
+            # print("Current Stash Size: %s"
+            #       % len(f.stash))
+            # print("Access Block Avg. Data Transmitted: %s (%.3fx)"
+            #       % (MemorySize((f.bytes_sent + f.bytes_received)/float(test_count)),
+            #          (f.bytes_sent + f.bytes_received)/float(test_count)/float(block_size)))
+            # print("Fetch Block Avg. Latency: %.2f ms"
+            #       % ((stop_time-start_time)/float(test_count)*1000))
+            # print("")
+            # print("")
+            # print("")
+            # print("")
+            # print("")
+            # print("") 
+
+           
+
+
+
+if __name__ == "__main__":
+    main()                                             # pragma: no cover