random-uploads.sh 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/bin/bash
  2. #
  3. # A script to upload random data to a swarm cluster.
  4. #
  5. # Example:
  6. #
  7. # random-uploads.sh --addr 192.168.33.101:8500 --size 40k --count 1000
  8. set -e
  9. ROOT="$(cd "$(dirname "$0")/../../.." && pwd)"
  10. source "${ROOT}/swarm/dev/scripts/util.sh"
  11. DEFAULT_ADDR="localhost:8500"
  12. DEFAULT_UPLOAD_SIZE="40k"
  13. DEFAULT_UPLOAD_COUNT="1000"
  14. usage() {
  15. cat >&2 <<USAGE
  16. usage: $0 [options]
  17. Upload random data to a Swarm cluster.
  18. OPTIONS:
  19. -a, --addr ADDR Swarm API address [default: ${DEFAULT_ADDR}]
  20. -s, --size SIZE Individual upload size [default: ${DEFAULT_UPLOAD_SIZE}]
  21. -c, --count COUNT Number of uploads [default: ${DEFAULT_UPLOAD_COUNT}]
  22. -h, --help Show this message
  23. USAGE
  24. }
  25. main() {
  26. local addr="${DEFAULT_ADDR}"
  27. local upload_size="${DEFAULT_UPLOAD_SIZE}"
  28. local upload_count="${DEFAULT_UPLOAD_COUNT}"
  29. parse_args "$@"
  30. info "uploading ${upload_count} ${upload_size} random files to ${addr}"
  31. for i in $(seq 1 ${upload_count}); do
  32. info "upload ${i} / ${upload_count}:"
  33. do_random_upload
  34. echo
  35. done
  36. }
  37. do_random_upload() {
  38. curl -fsSL -X POST --data-binary "$(random_data)" "http://${addr}/bzz-raw:/"
  39. }
  40. random_data() {
  41. dd if=/dev/urandom of=/dev/stdout bs="${upload_size}" count=1 2>/dev/null
  42. }
  43. parse_args() {
  44. while true; do
  45. case "$1" in
  46. -h | --help)
  47. usage
  48. exit 0
  49. ;;
  50. -a | --addr)
  51. if [[ -z "$2" ]]; then
  52. fail "--addr flag requires an argument"
  53. fi
  54. addr="$2"
  55. shift 2
  56. ;;
  57. -s | --size)
  58. if [[ -z "$2" ]]; then
  59. fail "--size flag requires an argument"
  60. fi
  61. upload_size="$2"
  62. shift 2
  63. ;;
  64. -c | --count)
  65. if [[ -z "$2" ]]; then
  66. fail "--count flag requires an argument"
  67. fi
  68. upload_count="$2"
  69. shift 2
  70. ;;
  71. *)
  72. break
  73. ;;
  74. esac
  75. done
  76. if [[ $# -ne 0 ]]; then
  77. usage
  78. fail "ERROR: invalid arguments: $@"
  79. fi
  80. }
  81. main "$@"