stress.sh 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-3.0-or-later
  3. if ! hash curl 2>/dev/null
  4. then
  5. 1>&2 echo "'curl' not found on system. Please install 'curl'."
  6. exit 1
  7. fi
  8. # set the host to connect to
  9. if [ -n "$1" ]
  10. then
  11. host="$1"
  12. else
  13. host="http://127.0.0.1:19999"
  14. fi
  15. echo "using netdata server at: $host"
  16. # shellcheck disable=SC2207 disable=SC1117
  17. charts=($(curl -k "$host/netdata.conf" 2>/dev/null | grep "^\[" | cut -d '[' -f 2 | cut -d ']' -f 1 | grep -v ^global$ | grep -v "^plugin" | sort -u))
  18. if [ "${#charts[@]}" -eq 0 ]
  19. then
  20. echo "Cannot download charts from server: $host"
  21. exit 1
  22. fi
  23. update_every="$(curl -k "$host/netdata.conf" 2>/dev/null | grep "update every = " | head -n 1 | cut -d '=' -f 2)"
  24. [ $(( update_every + 1 - 1)) -eq 0 ] && update_every=1
  25. entries="$(curl -k "$host/netdata.conf" 2>/dev/null | grep "history = " | head -n 1 | cut -d '=' -f 2)"
  26. [ $(( entries + 1 - 1)) -eq 0 ] && entries=3600
  27. # to compare equal things, set the entries to 3600 max
  28. [ $entries -gt 3600 ] && entries=3600
  29. if [ $entries -ne 3600 ]
  30. then
  31. echo >&2 "You are running a test for a history of $entries entries."
  32. fi
  33. modes=("average" "max")
  34. formats=("jsonp" "json" "ssv" "csv" "datatable" "datasource" "tsv" "ssvcomma" "html" "array")
  35. options="flip|jsonwrap"
  36. now=$(date +%s)
  37. first=$((now - (entries * update_every)))
  38. duration=$((now - first))
  39. file="$(mktemp /tmp/netdata-stress-XXXXXXXX)"
  40. cleanup() {
  41. echo "cleanup"
  42. [ -f "$file" ] && rm "$file"
  43. }
  44. trap cleanup EXIT
  45. while true
  46. do
  47. echo "curl -k --compressed --keepalive-time 120 --header \"Connection: keep-alive\" \\" >"$file"
  48. # shellcheck disable=SC2034
  49. for x in {1..100}
  50. do
  51. dt=$((RANDOM * duration / 32767))
  52. st=$((RANDOM * duration / 32767))
  53. et=$(( st + dt ))
  54. [ $et -gt "$now" ] && st=$(( now - dt ))
  55. points=$((RANDOM * 2000 / 32767 + 2))
  56. st=$((first + st))
  57. et=$((first + et))
  58. mode=$((RANDOM * ${#modes[@]} / 32767))
  59. mode="${modes[$mode]}"
  60. chart=$((RANDOM * ${#charts[@]} / 32767))
  61. chart="${charts[$chart]}"
  62. format=$((RANDOM * ${#formats[@]} / 32767))
  63. format="${formats[$format]}"
  64. echo "--url \"$host/api/v1/data?chart=$chart&mode=$mode&format=$format&options=$options&after=$st&before=$et&points=$points\" \\"
  65. done >>"$file"
  66. bash "$file" >/dev/null
  67. done