This project provides a simple framework for benchmarking the performance of various functions in PHP against their equivalents in C.
The following functions are included in this benchmark suite:
password_hash()(using Argon2i, mem=32768, time=2, threads=1)hash('sha256', ...)substr()gmp_init()gmp_mul()gmp_add()gmp_div()hexdec()time()- Date Math: Addition
- Date Math: Subtraction
microtime(true)
- Ubuntu 22.04 (or a compatible Debian-based distribution)
git
-
Clone the repository:
git clone <repository-url> cd <repository-directory>
-
Run the installer: The
install.shscript will update your package manager and install all the required dependencies (gcc,make,php,php-gmp,libgmp-dev,libargon2-dev,libssl-dev, andbc)../install.sh
The benchmarking process is broken into two main scripts: compile.sh and benchmark.sh.
-
Make the scripts executable: (This only needs to be done once.)
chmod +x compile.sh chmod +x benchmark.sh
-
Compile the C code: This script navigates to the
c/directory, cleans any old binaries, and compiles all the C source files using the optimized flags specified in theMakefile../compile.sh
-
Run the benchmark suite:
./benchmark.sh [crypto_iterations] [fast_func_iterations]
[crypto_iterations]is an optional argument for the number of iterations for slow cryptographic functions (e.g.,password_hash). Defaults to1000.[fast_func_iterations]is an optional argument for the number of iterations for all other, faster functions. Defaults to100000.
Example:
# Run with default iterations ./benchmark.sh # Run with 10 crypto iterations and 1,000,000 fast function iterations ./benchmark.sh 10 1000000
The benchmark.sh script performs the following actions:
- Executes Benchmarks: For each function, it runs both the compiled C executable and the corresponding PHP script, passing the same set of parameters (input data and iterations).
- Collects Results: It captures the execution time (in seconds) from the output of each script.
- Generates Report: The results are saved in a CSV file at
results/benchmark_results.csv. The script will also print the contents of this file to the console upon completion.
You can compile the C binaries at any time by running the ./compile.sh script or by running make inside the c/ directory. The compiled binaries are committed to the repository.