Skip to content

Instantly share code, notes, and snippets.

@webr3
Last active October 24, 2024 12:55
Show Gist options
  • Save webr3/e62aa0606145e774462501b6b2ca54f6 to your computer and use it in GitHub Desktop.
Save webr3/e62aa0606145e774462501b6b2ca54f6 to your computer and use it in GitHub Desktop.

sh.it

Time your sh.it! A simple shell script to benchmark command execution times.

What the sh.it?

sh.it is a straightforward benchmarking tool that lets you time how long commands take to execute. Run any command or script multiple times and get execution statistics in milliseconds.

Getting your sh.it together

# requirements
apt-get install bc

# Make it executable
chmod +x sh.it

# Move it somewhere in your PATH (optional)
sudo cp sh.it /usr/local/bin/

Using your sh.it

# Basic usage
sh.it "command"           # Run once
sh.it "command" 5        # Run 5 times

# Some examples
sh.it "sleep 1"          # Time a simple command
sh.it "./mybuild.sh"     # Time a script
sh.it "ls -R /"         # Time a complex command
sh.it "curl api.com" 3   # Benchmark an API call

What you get

For each run, you'll see:

Run 1 of 5:
Time: 1002.34 ms
-------------------

And a final summary:

Summary:
Total runs: 5
Average time: 1003.21 ms
Minimum time: 1001.89 ms
Maximum time: 1004.56 ms
Total time: 5016.05 ms

The sh.it code

#!/bin/bash

if [ $# -eq 0 ] || [ $# -gt 2 ]; then
    echo "Usage: $0 <command_or_script> [number_of_runs]"
    echo "       number_of_runs defaults to 1 if not specified"
    exit 1
fi

command_to_run=$1
runs=${2:-1}  # Default to 1 if not specified

# Check if it's a script that exists directly or in PATH
if [[ "$command_to_run" == *"/"* ]]; then
    # Path was specified, check if executable
    if [ ! -x "$command_to_run" ]; then
        echo "Error: $command_to_run is not executable or doesn't exist"
        exit 1
    fi
else
    # No path specified, check if it's a command available in PATH
    if ! command -v "$command_to_run" >/dev/null 2>&1; then
        # Not found in PATH, check if it exists in current directory
        if [ -x "./$command_to_run" ]; then
            command_to_run="./$command_to_run"
        else
            echo "Error: $command_to_run not found in PATH or current directory"
            exit 1
        fi
    fi
fi

# Check if number of runs is a positive integer
if ! [[ "$runs" =~ ^[1-9][0-9]*$ ]]; then
    echo "Error: Number of runs must be a positive integer"
    exit 1
fi

total_time=0
min_time=""
max_time=""

for ((i=1; i<=$runs; i++)); do
    echo "Run $i of $runs:"
    
    # Time the execution
    start=$(date +%s.%N)
    $command_to_run
    end=$(date +%s.%N)
    
    # Calculate execution time in milliseconds
    execution_time=$(echo "($end - $start) * 1000" | bc)
    # Round to 2 decimal places
    execution_time=$(printf "%.2f" $execution_time)
    total_time=$(echo "$total_time + $execution_time" | bc)
    
    # Update min and max times
    if [ -z "$min_time" ] || [ "$(echo "$execution_time < $min_time" | bc)" -eq 1 ]; then
        min_time=$execution_time
    fi
    if [ -z "$max_time" ] || [ "$(echo "$execution_time > $max_time" | bc)" -eq 1 ]; then
        max_time=$execution_time
    fi
    
    echo "Time: $execution_time ms"
    echo "-------------------"
done

# Calculate and display statistics
average_time=$(echo "scale=2; $total_time / $runs" | bc)
echo "Summary:"
echo "Total runs: $runs"
echo "Average time: $average_time ms"
echo "Minimum time: $min_time ms"
echo "Maximum time: $max_time ms"
echo "Total time: $total_time ms"

Get your sh.it straight

  • Times are in milliseconds with 2 decimal places
  • Works with commands, scripts, and full paths
  • Handles scripts in current directory or PATH
  • Provides min, max, average, and total times
  • No external dependencies beyond standard Unix tools

Why this sh.it?

Because sometimes you just need to know how long your sh.it takes to run! 🚀

License

This is free sh.it - MIT License

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment