Skip to content

Commit

Permalink
feat: add an auto benchmark script (#329)
Browse files Browse the repository at this point in the history
* feat: add an auto benchmark script

* feat(benchmark): add a help flag
  • Loading branch information
AntoineRR authored and sansyrox committed Dec 18, 2022
1 parent 77633ba commit ecb969e
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 5 deletions.
77 changes: 77 additions & 0 deletions benchmark.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/bin/sh

# Benchmark script to get info about Robyn's performances
# You can use this benchmark when developping on Robyn to test if your changes had a huge
# impact on performances. You cannot compare benchmarks from different machine and even
# several runs on the same machine can give very different results sometimes.
# Be aware of this when using this script!

Help() {
echo "Benchmark script to get info about Robyn's performances."
echo
echo "USAGE:"
echo " benchmark [-h|m|n|y]"
echo
echo "OPTIONS:"
echo " -h Print this help."
echo " -m Run 'maturin develop' to compile the Rust part of Robyn."
echo " -n <number> Set the number of requests that oha sends."
echo " -y Skip prompt"
exit 0
}

yes_flag=false
run_maturin=false
number=100000
while getopts hymn: opt; do
case $opt in
h)
Help
;;
y)
yes_flag=true
;;
m)
run_maturin=true
;;
n)
number=$OPTARG
;;
?)
echo 'Error in command line parsing' >&2
Help
exit 1
;;
esac
done

# Prompt user to check if he installed the requirements for running the benchmark
if [ "$yes_flag" = false ]; then
echo "Make sure you are running this in your venv and you installed 'oha' using 'cargo install oha'"
echo "Do you want to proceed?"
while true; do
read -p "" yn
case $yn in
[Yy]* ) break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
fi


# Compile Rust
if $run_maturin; then
maturin develop
fi

# Run the server in the background
python3 ./integration_tests/base_routes.py &
sleep 1

# oha will display benchmark results
oha -n "$number" http://localhost:5000/sync

# Kill subprocesses after exiting the script (python + robyn server)
# (see https://stackoverflow.com/questions/360201/how-do-i-kill-background-processes-jobs-when-my-shell-script-exits)
trap "trap - TERM && kill 0" INT TERM EXIT
14 changes: 9 additions & 5 deletions robyn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import multiprocessing as mp
import os
import signal
import sys
from typing import Callable, Optional

Expand Down Expand Up @@ -162,16 +163,19 @@ def init_processpool(socket):

process_pool = init_processpool(socket)

logger.info(f"{Colors.OKGREEN}Press Ctrl + C to stop \n{Colors.ENDC}")
try:
for process in process_pool:
process.join()
except KeyboardInterrupt:
def terminating_signal_handler(_sig, _frame):
logger.info(
f"{Colors.BOLD}{Colors.OKGREEN} Terminating server!! {Colors.ENDC}"
)
for process in process_pool:
process.kill()

signal.signal(signal.SIGINT, terminating_signal_handler)
signal.signal(signal.SIGTERM, terminating_signal_handler)

logger.info(f"{Colors.OKGREEN}Press Ctrl + C to stop \n{Colors.ENDC}")
for process in process_pool:
process.join()
else:
event_handler = EventHandler(self.file_path)
event_handler.start_server_first_time()
Expand Down

0 comments on commit ecb969e

Please sign in to comment.