perfromance.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/bin/bash
  2. ############## COMMAND EXAMPLES ################
  3. ## ENCRYPT AES-256 SALTED by OPENSSL
  4. ## openssl aes-256-ctr -a -salt -pbkdf2 -in plain.openssl -out encrypted.openssl -pass pass:'hello'
  5. ## ENCRYPT AES-256 by GPG
  6. ## gpg --cipher-algo AES256 --symmetric --batch --yes --passphrase 1234 --output encrypted.file plain.file
  7. ## ENCRYPT XORON EBS
  8. ## $HOME/xoronos/build/xrnlib-cli ebs -o $HOME/xoronos/cache/filecompressed_symblock.rnd -i attack-plan.txt -m $HOME/xoronos/cache/matrix.decompressed -k $HOME/xoronos/cache/pri.json -b 0 1
  9. ## DECRYPT AES-256 SALTED
  10. ## openssl aes-256-ctr -d -a -pbkdf2 -in encrypted.openssl -out decrypted.openssl -pass pass:'hello'
  11. ## DECRYPT AES-256 by GPG
  12. ## gpg -d --batch --yes --passphrase 1234 --output decrypted.gpg encrypted.gpg
  13. ############################################################
  14. # Help #
  15. ############################################################
  16. Help()
  17. {
  18. # Display Help
  19. echo
  20. echo "$(basename "$0") [-h] [-i <command1,command2,...>] [-n <occurr>] [-o <filename.json>]"
  21. echo "perfromance evaluates the timing performance of a number of occurrences of a specified command."
  22. echo
  23. echo "Syntax: ./perfromance.sh [-h|i|n|o]"
  24. echo
  25. echo "Options:"
  26. echo "h Print this Help."
  27. echo "i Input commands."
  28. echo "n Set the test occurrences."
  29. echo "o Print to a JSON file."
  30. echo
  31. echo "Usage Example:"
  32. echo "./perfromance.sh -i \"gpg --cipher-algo AES256 --symmetric --batch --yes --passphrase 1234 --output encrypted.gpg plain.gpg,openssl aes-256-ctr -a -salt -pbkdf2 -in plain.openssl -out encrypted.openssl -pass pass:'hello',openssl aes-256-ctr -d -a -pbkdf2 -in encrypted.openssl -out decrypted.openssl -pass pass:'hello',gpg -d --batch --yes --passphrase 1234 --output decrypted.gpg encrypted.gpg\" -n 3"
  33. echo
  34. }
  35. ############################################################
  36. # Banner #
  37. ############################################################
  38. #cat banner.txt | gzip | base64
  39. base64 -d <<<"H4sIAAAAAAAAA51Uy26EMAy89yt8W5Dq+N5KVfcLetuTtd6v6IWQb68nLI8gsgUSSBwFjyeOGaJ9
  40. zayy8bYTQMjiMQQbgpqPMCPFgxwiXYwUkX28AOEoB8RsCX4mmCJV/F8gBPiqnyIieKwxeMWh5rEX
  41. oR5zJ8J2C5xS4nAaQUajP4kgE49jHHQ0prz0qy/WCM9KnFamAUfnNCAE4rQqzhWCl0Bwuio6ftfR
  42. D+I/QD/n8JPIIecfbYFg1t3d0Xpstk5X7jhDYk7C0RQsPvJb5GJG8NAe2b2CY2AhY/ZkSIMmYu+r
  43. RM4IjT/Oovlue6/lPoiYmF29tjF4bfuEIsdimq4FB9+WThpr1TzdrSqpMvMvo914NBfrbBeZbCjz
  44. aL0LMEi/6P9W3AVoTIbfh2y6vEIYfN+ZL3tcNxGytkHZFhYNQrdQvVJ0CwRom2Vlyyr3oKfSPRaq
  45. N+zXEdCbrG9zb6y0aczWFgL2cekSszrnF5mx2c5FEWsI59ofP8Bv/LQGAAA=" | gunzip
  46. echo
  47. ############################################################
  48. ############################################################
  49. # Main program #
  50. ############################################################
  51. ############################################################
  52. # Set variables
  53. occurr=1
  54. ############################################################
  55. # Process the input options. Add options as needed. #
  56. ############################################################
  57. # Get the options
  58. while getopts ":hi:n:o:" option; do #When using getopts, putting : after an option character means that it requires an argument (i.e., 'h').
  59. case "${option}" in
  60. h) # display Help
  61. Help >&2
  62. exit 2
  63. ;;
  64. i) # Enter a command
  65. cmd=$OPTARG
  66. ;;
  67. o) # Enter an output filename
  68. out=$OPTARG
  69. if [ -f ${out} ]; then
  70. echo "${out} already exist! Do you want to overwrite it?"
  71. select yn in "Yes" "No"; do
  72. case $yn in
  73. Yes ) rm -rf ${out}; break;;
  74. No ) break;;
  75. esac
  76. done
  77. fi
  78. ;;
  79. n) # Execute the test <occurr> times
  80. occurr=$OPTARG
  81. ;;
  82. : )
  83. echo "Missing option argument for -$OPTARG" >&2; exit 1;;
  84. * )
  85. echo "Unimplemented option: -$OPTARG" >&2; exit 1;;
  86. \?) # Invalid option
  87. echo "Error: Invalid option" >&2
  88. ;;
  89. esac
  90. done
  91. echo
  92. # mandatory arguments
  93. if [ ! "$cmd" ]; then
  94. Help >&2
  95. echo "perfromance: error: missing a mandatory option (-i). Use -h for help"
  96. exit 1
  97. fi
  98. readarray -td, cmd_array < <(printf '%s' "${cmd}"); declare -p cmd_array >/dev/null ## < < to remove the final newline on the last item of the array added by <<<
  99. for index in "${cmd_array[@]}"
  100. do
  101. runtime_bc=0
  102. echo "Executing \"${index}\" command..."
  103. for i in $(seq 1 $occurr)
  104. do
  105. start=`date +%s.%N` #This command computes the number of seconds.nanoseconds since 01/01/1970 to the current time
  106. ${index} #> /dev/null
  107. end=`date +%s.%N`
  108. runtime_bc=$( echo "$runtime_bc + $( echo \"$end - $start\" | bc -l)" | bc -l )
  109. done
  110. result=$(echo "{\"command\": \"${index}\", \"execution_time\": \"${runtime_bc}\", \"test_occurrences\": \"${occurr}\", \"average_time\": \"$( echo "scale=9; ${runtime_bc} / ${occurr}" | bc -l )\"}")
  111. if [ ! "$out" ]; then
  112. echo
  113. echo ${result} | python -c "import json, sys, collections; print(json.dumps(json.loads(sys.stdin.read(), object_pairs_hook=collections.OrderedDict), indent=4))"
  114. echo
  115. else
  116. echo ${result} | python -c "import json, sys, collections; print(json.dumps(json.loads(sys.stdin.read(), object_pairs_hook=collections.OrderedDict), indent=4))" >> $out
  117. echo "Result correctly stored in ${out} file"
  118. echo
  119. fi
  120. done