-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: benchmark macros * feat: biguint divmod benchmark * chore: makefile targets for running benchmarks * chore: script to prettify benchmark result into markdown table * ci: upload benchmark result to pr comment * chore: fix type in build deps definition * ci: fix run_benchmarks checkout * ci: fix run_benchmarks prettify * ci: allow write permission * ci: fixed comment message
- Loading branch information
1 parent
1e3f23c
commit d9f1790
Showing
10 changed files
with
156 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
name: "Benchmarks" | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
branches: ["*"] | ||
paths: | ||
- "libs/**" | ||
|
||
jobs: | ||
run_benchmarks: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
pull-requests: write | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: "Compile" | ||
run: make build | ||
- name: "Run benchmarks" | ||
run: | | ||
make benchmark > benchmark_results.txt | ||
make benchmark_prettify BENCHMARK_FILE=benchmark_results.txt > benchmark_table.md | ||
- name: "Post benchmark results in pr comment" | ||
uses: mshick/add-pr-comment@v2 | ||
with: | ||
message-path: benchmark_table.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
DEPS := utils primitive-types math | ||
BUILD_DEPS := utils primitive-types math | ||
TESTS_DEPS := utils primitive-types math | ||
BENCHMARKS_DEPS := utils primitive-types math |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
DEPS := utils primitive-types | ||
BUILD_DEPS := utils primitive-types | ||
TESTS_DEPS := utils primitive-types | ||
BENCHMARKS_DEPS := utils primitive-types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
DEPS := utils primitive-types | ||
BUILD_DEPS := utils primitive-types | ||
TESTS_DEPS := utils primitive-types | ||
BENCHMARKS_DEPS := utils primitive-types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <math/random.h> | ||
#include <primitive-types/biguint.h> | ||
#include <utils/benchmark.h> | ||
|
||
void benchmark_divmod() { | ||
BigUint a = biguint_new(4); | ||
BigUint b = biguint_new(4); | ||
biguint_random(&a); | ||
biguint_random(&b); | ||
biguint_divmod(a, b, &a, &b); | ||
} | ||
|
||
int main() { | ||
BEGIN_BENCHMARK(); | ||
benchmark(benchmark_divmod, 1000000); | ||
END_BENCHMARK(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
DEPS := utils | ||
BUILD_DEPS := utils | ||
TESTS_DEPS := utils | ||
BENCHMARKS_DEPS := utils math |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
DEPS := | ||
BUILD_DEPS := | ||
TESTS_DEPS := | ||
BENCHMARKS_DEPS := |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#ifndef TEST_H | ||
#define TEST_H | ||
#include <time.h> | ||
#include <utils/macros.h> | ||
|
||
#define benchmark(benchmark_fn, iterations, ...) \ | ||
do { \ | ||
printf("\n=============== %s (%d iterations) ===============\n", #benchmark_fn, iterations); \ | ||
int ANONYMOUS_VARIABLE(benchmark_fn) = 0; \ | ||
double measures[iterations]; \ | ||
for (; ANONYMOUS_VARIABLE(benchmark_fn) < iterations; ANONYMOUS_VARIABLE(benchmark_fn)++) { \ | ||
clock_t start_time = clock(); \ | ||
benchmark_fn(__VA_ARGS__); \ | ||
clock_t end_time = clock(); \ | ||
double diff = (double)(end_time - start_time) / CLOCKS_PER_SEC; \ | ||
measures[ANONYMOUS_VARIABLE(benchmark_fn)] = diff; \ | ||
} \ | ||
double sum = 0; \ | ||
double avg = 0; \ | ||
for (int ANONYMOUS_VARIABLE(benchmark_fn_i) = 0; ANONYMOUS_VARIABLE(benchmark_fn_i) < iterations; \ | ||
ANONYMOUS_VARIABLE(benchmark_fn_i)++) { \ | ||
sum += measures[ANONYMOUS_VARIABLE(benchmark_fn_i)]; \ | ||
} \ | ||
avg = sum / iterations; \ | ||
printf("Total execution took: %f seconds\n", sum); \ | ||
printf("Average execution took: %f seconds\n", avg); \ | ||
printf("=============================\n"); \ | ||
} while (0) | ||
|
||
#define BEGIN_BENCHMARK() \ | ||
printf("\n==============================\n"); \ | ||
printf("Benchmark suite %s at %s\n", __FILE__, __func__); | ||
|
||
#define END_BENCHMARK() \ | ||
printf("\nBenchmark suite %s at %s", __FILE__, __func__); \ | ||
printf("\n==============================\n"); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/bin/bash | ||
|
||
FILE=$1 | ||
RESULTS=$(cat $FILE) | ||
|
||
# Initialize the Markdown table header | ||
echo "|Library| Benchmark Name | Iterations | Total Execution Time | Average Execution Time |" | ||
echo "|-------|----------------|------------|----------------------|------------------------|" | ||
|
||
current_library="" | ||
while IFS= read -r line; do | ||
if [[ "$line" =~ Benchmark\ suite\ libs/([^/]+)/benchmarks ]]; then | ||
current_library="${BASH_REMATCH[1]}" | ||
elif [[ "$line" =~ ^\=\=\=\=\=\=\=\=+\ (.*)\ \(([0-9]+)\ iterations\)\ =\=\=\=\=\=\=\=\=+ ]]; then | ||
benchmark_name="${BASH_REMATCH[1]}" | ||
iterations="${BASH_REMATCH[2]}" | ||
benchmark_name=$(echo "$benchmark_name" | sed 's/^benchmark_//') | ||
elif [[ "$line" =~ Total\ execution\ took:\ ([0-9.]+)\ seconds ]]; then | ||
total_time="${BASH_REMATCH[1]}" | ||
elif [[ "$line" =~ Average\ execution\ took:\ ([0-9.]+)\ seconds ]]; then | ||
avg_time="${BASH_REMATCH[1]}" | ||
echo "| $current_library | $benchmark_name | $iterations | $total_time seconds | $avg_time seconds |" | ||
fi | ||
done <<< "$RESULTS" | ||
|