1
0

bench_functions.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/usr/bin/env bash
  2. #
  3. # SPDX-License-Identifier: GPL-2.0-or-later
  4. #
  5. # Parametrisation to this script is as follows:
  6. # * `my_path` MUST be set
  7. # * `WC_TTY` can be set to override the default '/dev/ttyUSB2'
  8. # * `csv_path_suffix` can be set to add a suffix to the output path
  9. # * `VERBOSE` can be set to '0' to suppress all output
  10. # or '1' to make the output more verbose
  11. ###
  12. # Preamble
  13. ###
  14. if (return 0 2>/dev/null); then
  15. [[ -v my_path ]] || { echo "\$my_path must not be empty"; return 1; }
  16. else
  17. echo "This script shall only be sourced"
  18. exit 1
  19. fi
  20. readonly tty="${WC_TTY:-/dev/ttyUSB2}"
  21. fifo="$(mktemp -u)" || exit $?
  22. readonly fifo
  23. readonly csv_path="${my_path}/data/results${csv_path_suffix:-}"
  24. function status_echo() {
  25. [ "$VERBOSE" = "0" ] || echo "$*"
  26. }
  27. function cleanup() {
  28. wait
  29. rm $fifo
  30. }
  31. mkfifo $fifo
  32. trap cleanup EXIT
  33. function error_out() {
  34. exit 1
  35. }
  36. trap error_out INT TERM
  37. mkdir -p $csv_path
  38. status_echo "Writing to folder: $csv_path"
  39. status_echo "Reading from TTY: $tty"
  40. ###
  41. # Functions
  42. ###
  43. function read_tty() {
  44. while true; do
  45. read -r l
  46. $1 "$l"
  47. $read_tty_ret
  48. done < $tty
  49. }
  50. function wait_until_finished() {
  51. while true; do
  52. read -r ret
  53. [ "$ret" == "finished" ] && break
  54. done < $fifo
  55. }
  56. function process_csv() {
  57. read_tty_ret=
  58. case "$csv_state" in
  59. "0")
  60. case "$1" in
  61. "Algorithm,MB/s,Cycles per byte," | \
  62. "Algorithm,key size,operation,avg ms,ops/sec,")
  63. echo "$1" > $csv
  64. csv_state=1
  65. ;;
  66. esac
  67. ;;
  68. "1")
  69. if [ "$1" != "Benchmark complete" ]; then
  70. echo "$1" >> $csv
  71. [ "$VERBOSE" = "1" ] && echo "$1"
  72. else
  73. echo "finished" > $fifo
  74. read_tty_ret='return'
  75. fi
  76. ;;
  77. esac
  78. }
  79. function csv_start() {
  80. csv_state=0
  81. csv=$csv_path/$1
  82. read_tty process_csv &
  83. }
  84. function bench() {
  85. status_echo "Benchmark ${1^^}$3"
  86. csv_start ${1}${3}.csv
  87. echo "b $2 -csv" > $tty
  88. wait_until_finished
  89. }
  90. ###
  91. # Implementation
  92. ###
  93. function small_block() {
  94. if [[ ! -v small_block_sizes ]]; then
  95. echo '$small_block_sizes is unset.' 1>&2
  96. return 1
  97. fi
  98. for blocksize in $small_block_sizes
  99. do
  100. status_echo "Benchmark with $blocksize bytes sized blocks"
  101. for mode in $1
  102. do
  103. local opts=${mode}_opts
  104. bench "${mode}" "-aes-${mode} $blocksize ${!opts}" "_${blocksize}"
  105. done
  106. done
  107. }
  108. function large_block() {
  109. if [[ ! -v large_block_ciphers ]]; then
  110. echo '$large_block_ciphers is unset.' 1>&2
  111. return 1
  112. fi
  113. if [[ ! -v large_max_blocksize ]]; then
  114. echo '$large_max_blocksize is unset.' 1>&2
  115. return 1
  116. fi
  117. if [[ ! -v large_num_bytes ]]; then
  118. echo '$large_num_bytes is unset.' 1>&2
  119. return 1
  120. fi
  121. # 1 MiB
  122. local blocksize=$((1024 * 1024))
  123. while [ $blocksize -lt $large_max_blocksize ]
  124. do
  125. local num_blocks=$(($large_num_bytes / $blocksize))
  126. status_echo "Benchmark with $blocksize bytes sized blocks"
  127. for mode in $large_block_ciphers
  128. do
  129. local opts=${mode}_fast_opts
  130. bench "${mode}" "-aes-${mode} ${!opts} $blocksize -blocks $num_blocks" "_${blocksize}"
  131. done
  132. blocksize=$(($blocksize * 2))
  133. done
  134. }
  135. #eof