~tardypad/dotfiles

ref: 00c0c349a1b8960152233f11caa9ea1a40e395a3 dotfiles/files/scripts/shell/http-stats -rwxr-xr-x 2.7 KiB
00c0c349 — Damien Tardy-Panis Add presentation mode to mako to hide notifications 4 months ago
                                                                                
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/bin/sh

init_variables() {
  COMMAND=${0##*/}

  CURL_OPTIONS=

  CURL_FORMAT='
time_namelookup: %{time_namelookup}
time_connect: %{time_connect}
time_appconnect: %{time_appconnect}
time_pretransfer: %{time_pretransfer}
time_starttransfer: %{time_starttransfer}
time_total: %{time_total}
speed_download: %{speed_download}
speed_upload: %{speed_upload}
size_download: %{size_download}
size_upload: %{size_upload}
'

  REQUEST_DATA=
}

parse_command_line() {
  CURL_OPTIONS="$*"
}

validate_command_line() {
  if [ -z "${CURL_OPTIONS}" ]; then
    exit_error 'missing curl options'
  fi
}

exit_error() {
  [ -z "$1" ] || echo "${COMMAND}: $1"
  exit 1
} >&2

perform_request() {
  # shellcheck disable=SC2086
  REQUEST_DATA=$(
    curl -sS \
      -w "${CURL_FORMAT}" \
      -o /dev/null \
      ${CURL_OPTIONS}
  )
}

get_value() {
  TYPE="$1"
  NAME="$2"

  case "${TYPE}" in
    time)  CALC='* 1000' ;; # s to ms
    speed) CALC='/ 1000' ;; # B/s to kB/s
    size)  CALC='/ 1000' ;; # B to kB
  esac

  echo "$(
    echo "${REQUEST_DATA}" \
      | sed -n "s/^${TYPE}_${NAME}: \(.*\)$/\1/p"
  ) ${CALC}" | bc -l
}

print_range() {
  RANGE_NAME="$1"
  START_TIME="$2"
  END_TIME="$3"

  printf '%s\t%0.f ms\t(%0.f ms)\n' \
    "${RANGE_NAME}" \
    "$( echo "${END_TIME} - ${START_TIME}" | bc -l )" \
    "${END_TIME}"
}

print_speed() {
  printf '%s\t%0.2f kB/s\n' "$1" "$2"
}

print_size() {
  printf '%s\t%0.2f kB\n' "$1" "$2"
}

print_stats() {
  TIME_NAMELOOKUP=$( get_value time namelookup )
  TIME_CONNECT=$( get_value time connect )
  TIME_APPCONNECT=$( get_value time appconnect )
  TIME_PRETRANSFER=$( get_value time pretransfer )
  TIME_STARTTRANSFER=$( get_value time starttransfer )
  TIME_TOTAL=$( get_value time total )
  SPEED_DOWNLOADS=$( get_value speed download )
  SPEED_UPLOADS=$( get_value speed upload )
  SIZE_DOWNLOAD=$( get_value size download )
  SIZE_UPLOAD=$( get_value size upload )

  {
    print_range 'DNS lookup' 0 "${TIME_NAMELOOKUP}"
    print_range 'TCP connection' "${TIME_NAMELOOKUP}" "${TIME_CONNECT}"

    case "${TIME_APPCONNECT}" in
      *[!0.]*)
          print_range 'SSL handshake' "${TIME_CONNECT}" "${TIME_APPCONNECT}" ;;
    esac

    print_range 'Server processing' "${TIME_PRETRANSFER}" "${TIME_STARTTRANSFER}"
    print_range 'Content transfer' "${TIME_STARTTRANSFER}" "${TIME_TOTAL}"

    printf '\n'
    print_speed 'Speed upload' "${SPEED_UPLOADS}"
    print_speed 'Speed download' "${SPEED_DOWNLOADS}"

    printf '\n'
    print_size 'Size upload' "${SIZE_UPLOAD}"
    print_size 'Size download' "${SIZE_DOWNLOAD}"
  } \
    | column -t -s '	' -R2 -L
}

init_variables
parse_command_line "$@"
validate_command_line

if ! perform_request; then
  exit 1
fi

print_stats