From d1604f35e1899c6cc44a1e0da7c683464dd881f5 Mon Sep 17 00:00:00 2001
From: Jana Zikova <487568@mail.muni.cz>
Date: Mon, 17 May 2021 10:01:37 +0200
Subject: [PATCH] add scripts for performance testing

---
 testing/disk_usage.sh        |  11 +++
 testing/measure_resources.sh | 148 +++++++++++++++++++++++++++++++++++
 testing/parse_times.sh       | 129 ++++++++++++++++++++++++++++++
 3 files changed, 288 insertions(+)
 create mode 100755 testing/disk_usage.sh
 create mode 100755 testing/measure_resources.sh
 create mode 100755 testing/parse_times.sh

diff --git a/testing/disk_usage.sh b/testing/disk_usage.sh
new file mode 100755
index 0000000..7c7800b
--- /dev/null
+++ b/testing/disk_usage.sh
@@ -0,0 +1,11 @@
+#!/bin/bash
+
+file=$1
+i=$2
+
+while true; do
+    df /home | grep "/home" >> $file
+    sleep $i
+done
+
+
diff --git a/testing/measure_resources.sh b/testing/measure_resources.sh
new file mode 100755
index 0000000..2f26afc
--- /dev/null
+++ b/testing/measure_resources.sh
@@ -0,0 +1,148 @@
+#!/bin/bash
+
+create_sandbox()
+{
+    local file=$1
+    IFS="$oIFS"
+    local arguments=($2)
+    IFS=$'\n'
+    python3 create.py ${arguments[@]} $file >/dev/null 2>&1
+    
+}
+
+build_machines()
+{
+    local current_path=$(pwd)
+    cd $path/../sandbox
+    vagrant up --machine-readable > $path/output.txt
+    cd $current_path
+}
+
+destroy_machines()
+{
+    local current_path=$(pwd)
+    cd $path/../sandbox
+    vagrant destroy -f --machine-readable >/dev/null 2>&1
+    cd $current_path
+}
+
+oIFS="$IFS"
+IFS=$'\n'
+
+path="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
+
+printf "topology;build time;used memory;commited memory;used disk space;peak cpu usage;avg cpu usage\n" > $path/resources.csv
+printf "topology;complete time;downloading boxes;booting machines;installing rsync;running rsync;installing ansible;running ansible\n" > $path/times.csv
+
+config_file=$1
+
+files=($(PYTHONPATH=$path python3 -c "from yaml_config import get_topology_files; get_topology_files('$config_file')"))
+
+for file in "${files[@]}"
+do
+    filename=$(basename "$file" .yml)
+
+    arguments_strings=($(PYTHONPATH=$path python3 -c "from yaml_config import get_topology_arguments; get_topology_arguments('$config_file', '$file')"))
+
+    if [ "${#arguments_strings[@]}" -eq 0 ]; then
+        arguments_strings=(" ")
+    fi
+    i="1"
+    for arguments_string in ${arguments_strings[@]}
+    do
+        echo "$filename $arguments_string"
+
+        vagrant box remove munikypo/debian-10 >/dev/null 2>&1
+        vagrant box remove munikypo/kali-2019.4 >/dev/null 2>&1
+        vagrant box remove munikypo/kali-2020.4 >/dev/null 2>&1
+        vagrant box remove munikypo/windows-server-2019 >/dev/null 2>&1
+    
+        for i in {1..2}
+        do
+            sar -r -o $path/mem 1 >/dev/null 2>&1 &
+            mem_pid=$!
+    
+            $path/disk_usage.sh $path/disk.txt 1 &
+            disk_pid=$!
+    
+            sar -o $path/cpu 1 >/dev/null 2>&1 &
+            cpu_pid=$!
+    
+            if create_sandbox $path/../$file $arguments_string; then
+                build_machines
+                    
+                printf "$filename;" >> $path/times.csv
+                printf "$filename;" >> $path/resources.csv
+                bash $path/parse_times.sh
+
+                destroy_machines
+            fi
+    
+            kill $mem_pid
+            kill $disk_pid
+            kill $cpu_pid
+    
+            start=$(head -n 1 $path/output.txt | sed -r "s/(\s*),.*$/\1/")
+            end=$(tail -n 1 $path/output.txt | sed -r "s/(\s*),.*$/\1/")
+    
+            time=$(($end - $start))
+    
+            mem_usg_first=$(sar -r -f $path/mem | tail -n +4 | head -n 1 | awk '{ print $5 }')
+            mem_usg_max=$mem_usg_first
+    
+            mem_commit_first=$(sar -r -f $path/mem | tail -n +4 | head -n 1 | awk '{ print $9 }')
+            mem_commit_max=$mem_commit_first     
+    
+            for line in $(sar -r -f $path/mem | tail -n +4 | sed \$d)
+            do
+                usg=$(echo $line | awk '{ print $5 }')
+                if [ $usg -gt $mem_usg_max ]; then
+                    mem_usg_max=$usg
+                fi
+                commit=$(echo $line | awk '{ print $9 }')
+                if [ $commit -gt $mem_commit_max ]; then
+                    mem_commit_max=$commit
+                fi
+            done     
+    
+            mem_usg=$(( ($mem_usg_max-$mem_usg_first) / 1024 ))
+            mem_commit=$(( ($mem_commit_max-$mem_commit_first) / 1024 ))
+    
+            disk_first=$(head -n 1 $path/disk.txt | awk '{ print $3 }')
+            disk_max=$disk_first
+    
+            while IFS= read -r line
+            do
+                number=$(echo $line | awk '{ print $3 }')
+                if [ $number -gt $disk_max ]; then
+                    disk_max=$number
+                fi
+            done < $path/disk.txt
+    
+            disk=$(( ($disk_max-$disk_first) / 1024 ))
+                
+            cpu_max=$(sar -f $path/cpu | grep "all" | head -n 1 | awk '{ print (100 - $9)*100 }')
+    
+            for line in $(sar -f $path/cpu | tail -n +4 | sed \$d)
+            do
+                number=$(echo $line | awk '{ print (100 - $9)*100 }')
+                if [ $number -gt $cpu_max ]; then
+                    cpu_max=$number
+                fi
+            done
+    
+            cpu_peak=$(awk "BEGIN { print $cpu_max/100 }")
+            cpu_avg=$(sar -f $path/cpu | grep "all" | tail -n 1 | awk '{ print (100 - $8) }')
+    
+            printf "$time;$mem_usg;$mem_commit;$disk;$cpu_peak;$cpu_avg\n" >> $path/resources.csv
+    
+            rm $path/output.txt
+            rm $path/mem
+            rm $path/disk.txt
+            rm $path/cpu
+            i="2"
+        done
+
+    done
+
+done
\ No newline at end of file
diff --git a/testing/parse_times.sh b/testing/parse_times.sh
new file mode 100755
index 0000000..97eb662
--- /dev/null
+++ b/testing/parse_times.sh
@@ -0,0 +1,129 @@
+ #!/bin/bash
+
+path="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
+file="$path/output.txt"
+
+# complete build
+start=$(head -n 1 $file | sed -r "s/(\s*),.*$/\1/")
+end=$(tail -n 1 $file | sed -r "s/(\s*),.*$/\1/")
+
+time=$(($end - $start))
+
+download_time=0
+boot_time=0
+rsync_install_time=0
+rsync_run_time=0
+ansible_install_time=0
+ansible_run_time=0
+
+#machines
+machines=$(grep -oP "(?<=Bringing machine ').+(?=' up with 'virtualbox' provider...)" $file)
+
+for machine in ${machines} 
+do
+	# box
+	box=$(grep -oP "(?<=$machine: Importing base box ').+(?='...)" $file)
+
+	# downloading box
+	if [ -z "$(grep "$machine: Box '$box' could not be found. Attempting to find and install..." $file)" ] ; then
+		machine_download_time=0
+
+		boot_start=$(grep "$machine,action,up,start" $file | sed -r "s/(\s*),.*$/\1/")
+	elif [ -z "$(grep "$machine: Downloading: " $file)" ] ; then
+		machine_download_time=0
+
+		boot_start=$(grep "$machine,action,up,start" $file | sed -r "s/(\s*),.*$/\1/")
+	else
+		download_start=$(grep "$machine,action,up,start" $file | sed -r "s/(\s*),.*$/\1/")
+		download_end=$(grep "$machine: Successfully added box '$box'" $file | sed -r "s/(\s*),.*$/\1/")
+
+		machine_download_time=$(($download_end - $download_start))	
+
+		boot_start=$(grep "$machine: Importing base box '$box'" $file | sed -r "s/(\s*),.*$/\1/")
+	fi
+
+	if [ -z "$(grep "$machine: Running provisioner:" $file)" ] ; then
+		machine_rsync_install_time=0
+		machine_rsync_run_time=0
+		machine_ansible_install_time=0
+		machine_ansible_run_time=0
+
+		boot_end=$(grep "$machine,action,up,end" $file | sed -r "s/(\s*),.*$/\1/")
+	else
+		if [ -z "$(grep "$machine: Installing rsync to the VM..." $file)" ] ; then
+			machine_rsync_install_time=0
+	
+			if [ -z "$(grep "$machine: Rsyncing folder:" $file)" ] ; then
+				machine_rsync_run_time=0
+	
+				boot_end=$(grep -m 1 "$machine: Running provisioner:" $file | sed -r "s/(\s*),.*$/\1/")
+			else
+				boot_end=$(grep "$machine: Rsyncing folder:" $file | sed -r "s/(\s*),.*$/\1/")
+	
+				#rsync
+				rsync_start=$(grep "$machine: Rsyncing folder:" $file | sed -r "s/(\s*),.*$/\1/")
+				rsync_end=$(grep -m 1 "$machine: Running provisioner:" $file | sed -r "s/(\s*),.*$/\1/")
+	
+				machine_rsync_run_time=$(($rsync_end - $rsync_start))
+			fi
+		else
+			boot_end=$(grep "$machine: Installing rsync to the VM..." $file | sed -r "s/(\s*),.*$/\1/")
+	
+			rsync_install_start=$(grep "$machine: Installing rsync to the VM..." $file | sed -r "s/(\s*),.*$/\1/")
+			
+			if [ -z "$(grep "$machine: Rsyncing folder:" $file)" ] ; then
+				machine_rsync_run_time=0
+	
+				rsync_install_end=$(grep -m 1 "$machine: Running provisioner:" $file | sed -r "s/(\s*),.*$/\1/")
+			else
+				rsync_install_end=$(grep "$machine: Rsyncing folder:" $file | sed -r "s/(\s*),.*$/\1/")
+	
+				#rsync
+				rsync_start=$(grep "$machine: Rsyncing folder:" $file | sed -r "s/(\s*),.*$/\1/")
+				rsync_end=$(grep -m 1 "$machine: Running provisioner:" $file | sed -r "s/(\s*),.*$/\1/")
+	
+				machine_rsync_run_time=$(($rsync_end - $rsync_start))
+			fi
+	
+			machine_rsync_install_time=$(($rsync_install_end - $rsync_install_start))
+		fi
+
+		# downloading and installing ansible
+			if [ -z "$(grep "$machine: Installing Ansible..." $file)" ] ; then
+				machine_ansible_install_time=0
+	
+				ansible_run_start=$(grep -m 1 "$machine: Running provisioner:" $file | sed -r "s/(\s*),.*$/\1/")
+			else
+				ansible_install_start=$(grep -m 1 "$machine: Running provisioner:" $file | sed -r "s/(\s*),.*$/\1/")
+				ansible_install_end=$(grep -m 1 "$machine: Running ansible-playbook..." $file | sed -r "s/(\s*),.*$/\1/")
+	
+				machine_ansible_install_time=$(($ansible_install_end - $ansible_install_start))
+	
+				ansible_run_start=$(grep -m 1 "$machine: Running ansible-playbook..." $file | sed -r "s/(\s*),.*$/\1/")
+			fi
+	
+			ansible_run_end=$(grep "$machine,action,up,end" $file | sed -r "s/(\s*),.*$/\1/")
+	
+		machine_ansible_run_time=$(($ansible_run_end - $ansible_run_start))
+
+	fi
+	machine_boot_time=$(($boot_end - $boot_start))
+
+	download_time=$(($download_time+ $machine_download_time))
+	boot_time=$(($boot_time+ $machine_boot_time))
+	rsync_install_time=$(($rsync_install_time+ $machine_rsync_install_time))
+	rsync_run_time=$(($rsync_run_time+ $machine_rsync_run_time))
+	ansible_install_time=$(($ansible_install_time+ $machine_ansible_install_time))
+	ansible_run_time=$(($ansible_run_time + $machine_ansible_run_time))
+done
+
+printf "$time;$download_time;$boot_time;$rsync_install_time;$rsync_run_time;$ansible_install_time;$ansible_run_time\n" >> $path/times.csv
+
+
+
+
+
+
+
+
+
-- 
GitLab