aboutsummaryrefslogtreecommitdiffstats
path: root/swarm/dev/scripts/random-uploads.sh
blob: db7887e3c0669ae6ff1a86eb1c9f0c98760c1d67 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/bash
#
# A script to upload random data to a swarm cluster.
#
# Example:
#
#   random-uploads.sh --addr 192.168.33.101:8500 --size 40k --count 1000

set -e

ROOT="$(cd "$(dirname "$0")/../../.." && pwd)"
source "${ROOT}/swarm/dev/scripts/util.sh"

DEFAULT_ADDR="localhost:8500"
DEFAULT_UPLOAD_SIZE="40k"
DEFAULT_UPLOAD_COUNT="1000"

usage() {
  cat >&2 <<USAGE
usage: $0 [options]

Upload random data to a Swarm cluster.

OPTIONS:
  -a, --addr ADDR     Swarm API address      [default: ${DEFAULT_ADDR}]
  -s, --size SIZE     Individual upload size [default: ${DEFAULT_UPLOAD_SIZE}]
  -c, --count COUNT   Number of uploads      [default: ${DEFAULT_UPLOAD_COUNT}]
  -h, --help          Show this message
USAGE
}

main() {
  local addr="${DEFAULT_ADDR}"
  local upload_size="${DEFAULT_UPLOAD_SIZE}"
  local upload_count="${DEFAULT_UPLOAD_COUNT}"

  parse_args "$@"

  info "uploading ${upload_count} ${upload_size} random files to ${addr}"

  for i in $(seq 1 ${upload_count}); do
    info "upload ${i} / ${upload_count}:"
    do_random_upload
    echo
  done
}

do_random_upload() {
  curl -fsSL -X POST --data-binary "$(random_data)" "http://${addr}/bzzr:/"
}

random_data() {
  dd if=/dev/urandom of=/dev/stdout bs="${upload_size}" count=1 2>/dev/null
}

parse_args() {
  while true; do
    case "$1" in
      -h | --help)
        usage
        exit 0
        ;;
      -a | --addr)
        if [[ -z "$2" ]]; then
          fail "--addr flag requires an argument"
        fi
        addr="$2"
        shift 2
        ;;
      -s | --size)
        if [[ -z "$2" ]]; then
          fail "--size flag requires an argument"
        fi
        upload_size="$2"
        shift 2
        ;;
      -c | --count)
        if [[ -z "$2" ]]; then
          fail "--count flag requires an argument"
        fi
        upload_count="$2"
        shift 2
        ;;
      *)
        break
        ;;
    esac
  done

  if [[ $# -ne 0 ]]; then
    usage
    fail "ERROR: invalid arguments: $@"
  fi
}

main "$@"