stress_test.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/bin/bash
  2. # fail if any commands fails
  3. set -e
  4. # debug log
  5. #set -x
  6. function show_usage() {
  7. printf "Usage: $0 [options [parameters]]\n"
  8. printf "Example: %s --system archlinux --runtime \"10 minutes\" --interval 60 --stress yes" "$0"
  9. printf "\n\n"
  10. printf "Mandatory options:\n"
  11. printf " --system (archlinux | artix | devuan | macos | openbsd) (Mandatory)\n"
  12. printf "\n"
  13. printf "Options:\n"
  14. printf " --runtime [script duration, expressed: 'N minutes', 'N seconds', etc.] (Optional, default: '5 minutes')\n"
  15. printf " --interval [script output interval in seconds: (Optional, default: '60')\n"
  16. printf " --stress [set stress or idle execution] (Optional, default: 'no')\n"
  17. printf "\n"
  18. printf " -h|--help, Print help\n"
  19. printf "\n"
  20. printf " It will automatically save a report to '\$HOME/[system]-report-[date].log'"
  21. printf "\n"
  22. exit
  23. }
  24. cpu0_cur_freq_linux(){
  25. cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
  26. }
  27. temp_command_archlinux() {
  28. sensors | grep "Tctl:" | awk '{print $2}'
  29. }
  30. temp_command_artix() {
  31. sensors | grep "Core 0" | awk '{print $3}'
  32. }
  33. temp_command_devuan() {
  34. sensors | grep "Core 3" | awk '{print $3}'
  35. }
  36. top_command_archlinux() {
  37. top -b -d2 -n2 | grep "Cpu" | tail -n 1 | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1"%"}'
  38. }
  39. top_command_artix() {
  40. top -b -d2 -n2 | grep "Cpu" | tail -n 1 | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print $1"%"}'
  41. }
  42. top_command_devuan() {
  43. top -b -d2 -n2 | grep "Cpu" | tail -n 1 | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1"%"}'
  44. }
  45. date_command() {
  46. date '+%Y-%m-%d_%H-%M'
  47. }
  48. if [[ "$1" == "--help" ]] || [[ "$1" == "-h" ]]; then
  49. show_usage
  50. fi
  51. if [[ -z $1 ]]; then
  52. show_usage
  53. fi
  54. while [ ! -z "$1" ]; do
  55. case "$1" in
  56. --system)
  57. shift
  58. echo "operating system: $1"
  59. system=$1
  60. ;;
  61. --runtime)
  62. shift
  63. echo "script duration: $1"
  64. runtime=$1
  65. ;;
  66. --interval)
  67. shift
  68. echo "script output interval: $1"
  69. interval=$1
  70. ;;
  71. --stress)
  72. shift
  73. echo "stress run: $1"
  74. stress_run=$1
  75. ;;
  76. *)
  77. show_usage
  78. ;;
  79. esac
  80. shift
  81. done
  82. ### Configuration
  83. if [[ -z $system ]]; then
  84. show_usage
  85. fi
  86. if [[ -z $runtime ]]; then
  87. runtime="300"
  88. fi
  89. if [[ -z $interval ]]; then
  90. interval="60"
  91. fi
  92. if [[ -z $stress_run ]]; then
  93. stress_run="no"
  94. fi
  95. end_time=$(date -ud "$runtime seconds" +%s)
  96. report_path=$HOME/$system-report-$(date_command).log
  97. ### check parameter values
  98. systems=(archlinux artix devuan macos openbsd)
  99. if [[ " "${systems[@]}" " != *" $system "* ]]; then
  100. echo "$system: not recognized. Valid systems are:"
  101. echo "${systems[@]/%/,}"
  102. exit 1
  103. fi
  104. options=(yes no)
  105. if [[ " "${options[@]}" " != *" $stress_run "* ]]; then
  106. echo "$stress_run: not recognized. Valid '--stress_run' options are:"
  107. echo "${options[@]/%/,}"
  108. exit 1
  109. fi
  110. ### Start stress test
  111. if [ "$stress_run" = "yes" ]; then
  112. #for i in $(seq "$(getconf _NPROCESSORS_ONLN)"); do yes > /dev/null & done
  113. stress --cpu `nproc` --vm `nproc` --vm-bytes 1GB --io `nproc` --hdd `nproc` --hdd-bytes 1GB --timeout $runtime > /dev/null &
  114. fi
  115. ### Monitoring
  116. echo "=============================="
  117. while [[ $(date -u +%s) -le $end_time ]]
  118. do
  119. sleep $interval
  120. case "$system" in
  121. archlinux)
  122. echo "CPU Usage: $(top_command_archlinux) | CPU Temp: $(temp_command_archlinux) | CPU Freq: $(cpu0_cur_freq_linux)" | tee -a "$report_path"
  123. ;;
  124. artix)
  125. echo "CPU Usage: $(top_command_artix) | CPU Temp: $(temp_command_artix) | CPU Freq: $(cpu0_cur_freq_linux)" | tee -a "$report_path"
  126. ;;
  127. devuan)
  128. echo "CPU Usage: $(top_command_devuan) | CPU Temp: $(temp_command_devuan) | CPU Freq: $(cpu0_cur_freq_linux)" | tee -a "$report_path"
  129. ;;
  130. macos)
  131. echo ""
  132. ;;
  133. openbsd)
  134. echo ""
  135. ;;
  136. esac
  137. done
  138. echo "=============================="
  139. ### Stop stress test
  140. #if [ "$stress_run" = "yes" ]; then
  141. # killall yes
  142. #fi
  143. ### Show report message
  144. echo "Saved report to file $report_path"
  145. ### Clean temporary files
  146. rm -rf stress.*