Skip to content

Instantly share code, notes, and snippets.

@armsultan
Created January 5, 2022 16:09
Show Gist options
  • Select an option

  • Save armsultan/19e8b585e0063a8cfffe1a3c5f3b3322 to your computer and use it in GitHub Desktop.

Select an option

Save armsultan/19e8b585e0063a8cfffe1a3c5f3b3322 to your computer and use it in GitHub Desktop.

Revisions

  1. armsultan created this gist Jan 5, 2022.
    81 changes: 81 additions & 0 deletions loadgen.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    #!/usr/bin/env bash
    #
    # HOW TO:
    #
    # 1. Create this script and place it in /bin/loadgen
    # 2. Make script executable: chmod +x /bin/loadgen
    # 3. Install wrk2 (https://github.com/giltene/wrk2) so we can define Rate -R, --rate:
    # See install instructions: https://github.com/giltene/wrk2/wiki/Installing-wrk2-on-Linux
    # 4. Install bc, the command-line calculator:
    # apt-get install bc
    # 5. Make sure your date and timezone is configured, e.g. on ubuntu sudo dpkg-reconfigure tzdata
    # Set cron job to run script EVERY 1 MINUTE
    # * * * * * eval $(/bin/loadgen) >/dev/null 2>&1
    #

    # Set URL target and wrk2
    TARGET=http://load.t3st.org/0kb.bin
    # Absolute path to wrk2
    WRK=/bin/wrk

    # Set Rate per second in a array where index is the hour in 24hr Time
    arr[0]=1000
    arr[1]=800
    arr[2]=600
    arr[3]=600
    arr[4]=800
    arr[5]=1000
    arr[6]=1500
    arr[7]=2000
    arr[8]=4000
    arr[9]=6000
    arr[10]=6500
    arr[11]=7000
    arr[12]=12000
    arr[13]=18000
    arr[14]=16000
    arr[15]=14000
    arr[16]=12000
    arr[17]=13000
    arr[18]=16500
    arr[19]=18000
    arr[20]=12000
    arr[21]=6000
    arr[22]=3000
    arr[23]=2000

    # Set the rate based on time (hr) of day
    HR=$(date +"%-H") # (hyphen) do not pad the field i.e. 09 becomes 9
    RATE=${arr[$HR]}
    # Calculate a random variable rate within a percentage range
    VAR_PERCENT=20
    VAR_DIFF=$(printf $(echo "scale=4; $VAR_PERCENT/100 * $RATE" | bc | cut -d . -f 1))
    VAR_RATE_HIGH=$(echo "$RATE + $VAR_DIFF" | bc)
    VAR_RATE_RANDOM=$[ ( $RANDOM % ( $[ $VAR_RATE_HIGH - $RATE ] + 1 ) ) + $RATE ]
    #echo $VAR_DIFF
    #echo $VAR_RATE_HIGH
    #echo $VAR_RATE_RANDOM

    # Horsepower
    # Get Number of Cores on this machine
    CORES=$(( $(lscpu | awk '/^Socket\(s\)/{ print $2 }') * $(lscpu | awk '/^Core\(s\) per socket/{ print $4 }') ))
    # The -c option specifies the number of TCP connections to create. For our testing, we set this to a fixed amount e.g. 50 connections.
    #CONNECTIONS=50
    CONNECTIONS=$(($RATE / 4))

    # The -d option specifies how long to generate traffic. We ran our tests for 61s each since we run a cron EVERY 1MIN
    DURATION=61s

    #
    # Attack single target url
    #
    # Uncomment one out, HTTP or HTTPS
    # HTTPS
    # The client opens and closes a connection for each request (the -H option sets the Connection: close HTTP header).
    # HTTPS
    # CMD="$WRK -t$CORES -c$CONNECTIONS -d$DURATION -R$RATE $TARGET -H 'Connection: close'"
    # HTTP
    CMD="$WRK -t$CORES -c$CONNECTIONS -d$DURATION -R$VAR_RATE_RANDOM $TARGET"
    echo $CMD

    # vim: syntax=sh