benchmark_compare.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #!/bin/bash
  2. # This script is designed to compare the output of wolfcrypt/benchmark test
  3. # application. If the file has an extension ".csv", then it will parse the
  4. # comma separated format, otherwise it will use the standard output format. The
  5. # green colored output field is the better result.
  6. # Usage: benchmark_compare.sh <first file> <second file>
  7. # You can define a few variables to set options:
  8. # THRESHOLD - set the threshold for equality between two results
  9. # OUTPUT_CSV - set to "1" to print CSV
  10. FIRST_FILE=$1
  11. SECOND_FILE=$2
  12. THRESHOLD=${THRESHOLD:-"10"}
  13. OUTPUT_CSV=${OUTPUT_CSV:-"0"}
  14. declare -A symStats
  15. declare -A asymStats
  16. function getAlgo() { # getAlgo <asCSV> <mode> <line>
  17. if [ "$asCSV" = 1 ]; then
  18. declare -a fields
  19. IFS=',' read -ra fields <<< "$line"
  20. if [ "$mode" = 1 ]; then
  21. echo "${fields[0]}"
  22. else
  23. if [ "${fields[2]}" = "" ]; then
  24. echo "${fields[0]}"
  25. else
  26. echo "${fields[0]}-${fields[2]}"
  27. fi
  28. fi
  29. else
  30. if [ "$mode" = 1 ]; then
  31. echo "$line" | sed 's/ *[0-9]* MiB.*//g'
  32. else
  33. if [[ $line == "scrypt"* ]]; then
  34. echo "scrypt"
  35. else
  36. echo "$line" | sed 's/ *[0-9]* ops.*//g' | sed 's/ \+[0-9]\+ \+/-/g'
  37. fi
  38. fi
  39. fi
  40. }
  41. function getValue() { # getValue <asCSV> <mode> <line>
  42. if [ "$asCSV" = 1 ]; then
  43. declare -a fields
  44. IFS=',' read -ra fields <<< "$line"
  45. if [ "$mode" = 1 ]; then
  46. echo "${fields[1]}"
  47. else
  48. echo "${fields[4]}"
  49. fi
  50. else
  51. if [ "$mode" = 1 ]; then
  52. echo "$line" | sed 's/.*seconds, *//g' | sed 's/ *MiB\/s.*//g'
  53. else
  54. echo "$line" | sed 's/.* ms, *//g' | sed 's/ ops\/sec.*//g'
  55. fi
  56. fi
  57. }
  58. asCSV=0
  59. mode=0
  60. while IFS= read -r line; do
  61. if [[ $FIRST_FILE == *".csv" ]]; then
  62. asCSV=1
  63. if [[ $line == *"Symmetric Ciphers"* ]]; then
  64. mode=1
  65. read
  66. read
  67. elif [[ $line == *"Asymmetric Ciphers"* ]]; then
  68. mode=2
  69. read
  70. read
  71. elif [[ $line == "" ]]; then
  72. mode=0
  73. fi
  74. else
  75. asCSV=0
  76. if [[ $line == *"MiB/s"* ]]; then
  77. mode=1
  78. elif [[ $line == *"ops/sec"* ]]; then
  79. mode=2
  80. else
  81. mode=0
  82. fi
  83. fi
  84. if [ "$mode" -ne 0 ]; then
  85. ALGO=`getAlgo "$asCSV" "$mode" "$line"`
  86. VALUE=`getValue "$asCSV" "$mode" "$line"`
  87. if [ "$mode" = "1" ]; then
  88. symStats["${ALGO}"]=${VALUE}
  89. elif [ "$mode" = "2" ]; then
  90. asymStats["${ALGO}"]=${VALUE}
  91. fi
  92. fi
  93. done < ${FIRST_FILE}
  94. RED='\033[0;31m'
  95. GRN='\033[0;32m'
  96. NC='\033[0m' # No Color
  97. function printData() { # printData <ALGO> <val1> <val2>
  98. ALGO=$1
  99. VAL1=$2
  100. VAL2=$3
  101. if (( $(echo "sqrt( (${VAL1} - ${VAL2})^2 ) < ${THRESHOLD}" | bc -l) )); then
  102. # take absolute value and check if less than a threshold
  103. echo "${ALGO},${GRN}${VAL1}${NC},=,${GRN}${VAL2}${NC}\n"
  104. elif (( $(echo "${VAL1} > ${VAL2}" | bc -l) )); then
  105. echo "${ALGO},${GRN}${VAL1}${NC},>,${VAL2}\n"
  106. else
  107. echo "${ALGO},${VAL1},<,${GRN}${VAL2}${NC}\n"
  108. fi
  109. }
  110. asCSV=0
  111. mode=0
  112. while IFS= read -r line; do
  113. if [[ $SECOND_FILE == *".csv" ]]; then
  114. asCSV=1
  115. if [[ $line == *"Symmetric Ciphers"* ]]; then
  116. RES+="ALGO,${FIRST_FILE},diff(MB/s),${SECOND_FILE}\n"
  117. mode=1
  118. read
  119. read
  120. elif [[ $line == *"Asymmetric Ciphers"* ]]; then
  121. RES+="\nALGO,${FIRST_FILE},diff(ops/sec),${SECOND_FILE}\n"
  122. mode=2
  123. read
  124. read
  125. elif [[ $line == "" ]]; then
  126. mode=0
  127. fi
  128. else
  129. asCSV=0
  130. if [[ $line == *"MiB/s"* ]]; then
  131. mode=1
  132. elif [[ $line == *"ops/sec"* ]]; then
  133. mode=2
  134. else
  135. mode=0
  136. fi
  137. fi
  138. if [ "$mode" -ne 0 ]; then
  139. if [[ $line == *","* ]]; then
  140. ALGO=`getAlgo "$asCSV" "$mode" "$line"`
  141. VALUE=`getValue "$asCSV" "$mode" "$line"`
  142. if [ "$mode" = "1" ]; then
  143. RES+=`printData "${ALGO}" "${symStats["${ALGO}"]}" "${VALUE}"`
  144. elif [ "$mode" = "2" ]; then
  145. RES+=`printData "${ALGO}" "${asymStats["${ALGO}"]}" "${VALUE}"`
  146. fi
  147. fi
  148. fi
  149. done < ${SECOND_FILE}
  150. if [ "${OUTPUT_CSV}" = "1" ]; then
  151. echo -e "$RES"
  152. else
  153. echo -e "$RES" | column -t -s ',' -L
  154. fi