Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add an auto benchmark script #329

Merged
merged 2 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions benchmark.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/bin/sh
sansyrox marked this conversation as resolved.
Show resolved Hide resolved

# 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)
sansyrox marked this conversation as resolved.
Show resolved Hide resolved

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