Browse Source

- Instruments code for performance measurement

Patrick Jattke 8 years ago
parent
commit
e998eefca1
5 changed files with 165 additions and 21 deletions
  1. 27 21
      code/CLI.py
  2. 9 0
      code/ID2TLib/AttackController.py
  3. 7 0
      code/ID2TLib/PcapFile.py
  4. 7 0
      code/ID2TLib/Statistics.py
  5. 115 0
      code/profile-process.py

+ 27 - 21
code/CLI.py

@@ -105,26 +105,32 @@ def main(args):
 
 
 # Uncomment to enable calling by terminal
+# if __name__ == '__main__':
+#     main(sys.argv[1:])
+
 if __name__ == '__main__':
-    main(sys.argv[1:])
+    INPUT = ['-i']
 
-# if __name__ == '__main__':
-#     FILE = ['-i', '/mnt/hgfs/datasets/95M.pcap']
-#     FILE2 = ['-i', '/mnt/hgfs/datasets/95M_20161103-185151.pcap']
-#     FILE3 = ['-i', '/home/pjattke/temp/test_me_short.pcap']
-#     ATTACK_NO_PARAM = ['-a', 'DDoSAttack', 'attackers.count=10']
-#
-#     ATTACK = ['-a', 'PortscanAttack', 'ip.src=10.2.2.4', 'mac.dst=05:AB:47:B5:19:11',
-#               'inject.at-timestamp=1449038705.316721', 'attack.note=Portscan2']
-#     ATTACK2 = ['-a', 'PortscanAttack', 'ip.dst=193.133.122.23', 'ip.src=192.124.34.12', 'inject.after-pkt=34']
-#
-#     STATS_RECALC = ['-r']
-#     STATS_PRINT = ['-s']
-#     STATS_PLOT = ['-p', 'format=pdf']
-#
-#     QUERY_MODE_LOOP = ['-q']
-#     QUERY_DB = ['-q', 'ipAddress(pktsSent > 1000, kbytesSent >= 20)']
-#
-#     main(FILE + STATS_PLOT)
-
-    # main(['-c', '/home/pjattke/Thesis/development/code/config'])
+    FILES = ['/root/datasets/201506021400_1G.pcap',
+             '/root/datasets/201506021400_2G.pcap',
+             '/root/datasets/201506021400_5G.pcap']
+
+    # FILES = ['/root/datasets/201506021400.pcap']
+
+    # FILES = ['/mnt/hgfs/datasets/95M.pcap']
+
+    ATTACK_PS = ['-a', 'PortscanAttack', 'ip.src=10.2.2.4', 'mac.dst=05:AB:47:B5:19:11',
+                 'inject.at-timestamp=1449038705.316721', 'attack.note=Portscan2']
+    ATTACK_PS2 = ['-a', 'PortscanAttack', 'ip.dst=193.133.122.23', 'ip.src=192.124.34.12', 'inject.after-pkt=34']
+    ATTACK_DD = ['-a', 'DDoSAttack', 'attackers.count=10', 'packets.limit=10000']
+
+    STATS_RECALC = ['-r']
+    STATS_PRINT = ['-s']
+    STATS_PLOT = ['-p']
+
+    QUERY_MODE_LOOP = ['-q']
+    QUERY_DB = ['-q', 'ipAddress(pktsSent > 1000, kbytesSent >= 20)']
+
+    for f in FILES:
+        main(INPUT + [f] + STATS_RECALC)  # Statistics Calculation
+        #main(INPUT + ATTACK_DD)  # Attack Packet Generation -> insert exit() | Merging

+ 9 - 0
code/ID2TLib/AttackController.py

@@ -2,6 +2,8 @@ import importlib
 import os
 import tempfile
 import sys
+
+import time
 from scapy.utils import PcapWriter
 from Attack.AttackParameters import Parameter
 from ID2TLib import LabelManager
@@ -83,7 +85,14 @@ class AttackController:
         # Write attack into pcap file
         print("Generating attack packets...", end=" ")
         sys.stdout.flush()  # force python to print text immediately
+
+        time_s = time.time()
         temp_attack_pcap_path = self.current_attack.generate_attack_pcap()
+        time_e = time.time()
+        f = open("/root/perfresults/runtime_packetgen.txt", "a")
+        f.write(time_e - time_s)
+        f.close()
+
         print("done.")
 
         # Merge attack with existing pcap

+ 7 - 0
code/ID2TLib/PcapFile.py

@@ -3,6 +3,7 @@ import os.path
 import sys
 
 import ID2TLib.libpcapreader as pr
+import time
 
 
 class PcapFile(object):
@@ -24,7 +25,13 @@ class PcapFile(object):
         print("Merging base pcap with attack pcap...", end=" ")
         sys.stdout.flush()  # force python to print text immediately
 
+        time_s = time.time()
         pcap = pr.pcap_processor(self.pcap_file_path)
+        time_e = time.time()
+        f = open("/root/perfresults/runtime_merging.txt", "a")
+        f.write(time_e - time_s)
+        f.close()
+
         file_out_path = pcap.merge_pcaps(attack_pcap_path)
         print("done.")
 

+ 7 - 0
code/ID2TLib/Statistics.py

@@ -47,7 +47,14 @@ class Statistics:
         # Recalculate statistics if database not exists OR param -r/--recalculate was provided
         if (not self.stats_db.get_db_exists()) or flag_recalculate_stats:
             self.pcap_proc = pr.pcap_processor(self.pcap_filepath)
+
+            time_s = time.time()
             self.pcap_proc.collect_statistics()
+            time_e = time.time()
+            f = open("/root/perfresults/runtime_stats.txt", "a")
+            f.write(time_e - time_s)
+            f.close()
+
             self.pcap_proc.write_to_database(self.path_db)
             outstring_datasource = "by PCAP file processor."
         else:

+ 115 - 0
code/profile-process.py

@@ -0,0 +1,115 @@
+#! /usr/bin/env python3
+
+import sys
+import subprocess
+import time
+import psutil
+import matplotlib
+import matplotlib.pyplot as plt
+from matplotlib.backends.backend_pdf import PdfPages
+
+
+def log_performance():
+    def limit(n, limit):
+        if n > limit:
+            return limit
+        else:
+            return n
+
+    # Interval for data collection (in seconds)
+    probe_interval = 0.1
+
+    i = 0
+    x = []
+    # Memory
+    stats_rss = []
+    stats_vms = []
+    # CPU
+    stats_usr = []
+    stats_sys = []
+    stats_cpu = []
+    # Disk
+    stats_io_r = []
+    stats_io_w = []
+    stats_io_r_b = []
+    stats_io_w_b = []
+
+    proc = subprocess.Popen("/home/pjattke/Thesis/public/code/CLI.py")
+    p = psutil.Process(proc.pid)
+    start_time = time.time()
+    while proc is None or proc.poll() is None:
+        m = p.memory_info()
+        c = p.cpu_times()
+        io = p.io_counters()
+        x.append(i)
+        stats_rss.append(m[0] / 10 ** 6)
+        stats_vms.append(m[1] / 10 ** 6)
+        stats_usr.append(c[0])
+        stats_sys.append(c[1])
+        stats_cpu.append(limit(p.cpu_percent(interval=None), 100))
+        stats_io_r.append(io[0])
+        stats_io_w.append(io[1])
+        stats_io_r_b.append(io[2])
+        stats_io_w_b.append(io[3])
+        time.sleep(probe_interval)
+        i += probe_interval
+
+    print("=== MEASUREMENT RECORDED " + str(time.time() - start_time) + " seconds.")
+
+    # Define style for plots
+    f, axs = plt.subplots(4, 1, figsize=(8, 12))
+    plt.rcParams["font.family"] = "monospace"
+    plt.rcParams["font.size"] = 8.5
+    plt.rcParams["legend.fontsize"] = 8.0
+    # plt.autoscale(enable=True, axis='both')
+    f.subplots_adjust(hspace=0.5)
+    pp = PdfPages('multipage.pdf')
+
+    # f.add_subplot(4,1,2)
+    plt.subplot(411)
+    plt.grid(True)
+    plt.plot(x, stats_cpu, label="CPU Usage")
+    # plt.plot(x, stats_usr, label="User")
+    # plt.plot(x, stats_sys, label="System")
+    plt.xlabel("Execution time (seconds)")
+    plt.ylabel("CPU Load (%)")
+    plt.legend(loc="upper left")
+    # plt.savefig("profile_cpu.png")
+
+    # f.add_subplot(4,1,1)
+    plt.subplot(412)
+    plt.grid(True)
+    plt.plot(x, stats_rss, label="RSS")
+    plt.plot(x, stats_vms, label="VMS")
+    plt.xlabel("Execution time (seconds)")
+    plt.ylabel("Memory Usage (MBytes)")
+    plt.legend(loc="upper left")
+    # plt.savefig("profile_memory.png")
+
+    # f.add_subplot(4,1,3)
+    plt.subplot(413)
+    plt.grid(True)
+    plt.plot(x, stats_io_r, label="IO Reads")
+    # plt.plot(x, stats_io_w, label="IO Writes")
+    # plt.plot(x, stats_io_r_b, label="IO Reads Bytes")
+    # plt.plot(x, stats_io_w_b, label="IO Writes Bytes")
+    # plt.xlabel("Execution time (seconds)")
+    plt.xlabel("Execution time (seconds)")
+    plt.ylabel("I/O reads counter")
+    # plt.ylabel("IO Profile")
+    plt.legend(loc="upper left")
+
+    # f.add_subplot(4,1,4)
+    plt.subplot(414)
+    plt.grid(True)
+    plt.plot(x, stats_io_w, label="IO Writes")
+    plt.xlabel("Execution time (seconds)")
+    plt.ylabel("I/O writes counter")
+    plt.legend(loc="upper left")
+    plt.savefig(pp, format='pdf')
+
+    pp.close()
+    sys.exit(proc.returncode)
+
+
+log_performance()