Skip to content
Draft
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
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ linker = "rust-lld"

[target.'cfg(target_env = "musl")']
rustflags = ["-C", "target-feature=-crt-static"]

[profile.bench]
debug = true
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ runtime = ["bindgen/runtime"]
static = ["bindgen/static"]

[workspace]
members = ["crates/macros", "crates/cli", "tests"]
members = ["crates/macros", "crates/cli", "tests", "benches"]

[package.metadata.docs.rs]
rustdoc-args = ["--cfg", "docs"]
Expand Down
24 changes: 24 additions & 0 deletions benches/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "benches"
version = "0.1.0"
edition = "2024"
publish = false

[dependencies]
ext-php-rs = { path = "../" }
gungraun = { version = "0.17.0", features = ["client_requests"] }

[features]
default = ["enum", "runtime", "closure"]
enum = ["ext-php-rs/enum"]
anyhow = ["ext-php-rs/anyhow"]
runtime = ["ext-php-rs/runtime"]
closure = ["ext-php-rs/closure"]
static = ["ext-php-rs/static"]

[lib]
crate-type = ["cdylib"]

[[bench]]
name = "binary_bench"
harness = false
84 changes: 84 additions & 0 deletions benches/benches/binary_bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use std::{
process::Command,
sync::{LazyLock, Once},
};

use gungraun::{
BinaryBenchmarkConfig, Callgrind, FlamegraphConfig, binary_benchmark, binary_benchmark_group,
main,
};

static BUILD: Once = Once::new();
static EXT_TARGET_DIR: LazyLock<String> = LazyLock::new(|| {
let mut dir = std::env::current_dir().expect("Could not get cwd");
dir.pop();
dir.push("target");
dir.push("release");
dir.display().to_string()
});

fn setup() {
BUILD.call_once(|| {
let mut command = Command::new("cargo");
command.arg("build");

command.arg("--release");

// Build features list dynamically based on compiled features
// Note: Using vec_init_then_push pattern here is intentional due to conditional compilation
#[allow(clippy::vec_init_then_push)]
{
let mut features = vec![];
#[cfg(feature = "enum")]
features.push("enum");
#[cfg(feature = "closure")]
features.push("closure");
#[cfg(feature = "anyhow")]
features.push("anyhow");
#[cfg(feature = "runtime")]
features.push("runtime");
#[cfg(feature = "static")]
features.push("static");

if !features.is_empty() {
command.arg("--no-default-features");
command.arg("--features").arg(features.join(","));
}
}

let result = command.output().expect("failed to execute cargo build");

assert!(
result.status.success(),
"Extension build failed:\nstdout: {}\nstderr: {}",
String::from_utf8_lossy(&result.stdout),
String::from_utf8_lossy(&result.stderr)
);
});
}

#[binary_benchmark]
#[bench::single_function_call(args = ("benches/function_call.php", 1))]
#[bench::multiple_function_calls(args = ("benches/function_call.php", 10))]
#[bench::lots_of_function_calls(args = ("benches/function_call.php", 100_000))]
fn function_calls(path: &str, cnt: usize) -> gungraun::Command {
setup();

gungraun::Command::new("php")
.arg(format!("-dextension={}/libbenches.so", *EXT_TARGET_DIR))
.arg(path)
.arg(cnt.to_string())
.build()
}

binary_benchmark_group!(
name = function;
benchmarks = function_calls
);

main!(
config = BinaryBenchmarkConfig::default()
.tool(Callgrind::with_args(["--instr-atstart=no", "--I1=32768,8,64", "--D1=32768,8,64", "--LL=67108864,16,64"])
.flamegraph(FlamegraphConfig::default()));
binary_benchmark_groups = function
);
11 changes: 11 additions & 0 deletions benches/benches/function_call.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

start_instrumentation();

foreach (range(1, $argv[1]) as $i) {
bench_function($i);
}

stop_instrumentation();
43 changes: 43 additions & 0 deletions benches/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#![cfg_attr(windows, feature(abi_vectorcall))]
#![allow(
clippy::must_use_candidate,
clippy::missing_panics_doc,
clippy::needless_pass_by_value,
clippy::implicit_hasher
)]

use ext_php_rs::{prelude::*, types::Zval};

#[php_function]
pub fn bench_function(n: u64) -> u64 {
// A simple function that does not do much work
n
}

#[php_function]
pub fn bench_callback_function(callback: ZendCallable) -> Zval {
// Call the provided PHP callable with a fixed argument
callback
.try_call(vec![&42])
.expect("Failed to call function")
}

#[php_function]
pub fn start_instrumentation() {
gungraun::client_requests::callgrind::start_instrumentation();
// gungraun::client_requests::callgrind::toggle_collect();
}

#[php_function]
pub fn stop_instrumentation() {
gungraun::client_requests::callgrind::stop_instrumentation();
}

#[php_module]
pub fn build_module(module: ModuleBuilder) -> ModuleBuilder {
module
.function(wrap_function!(bench_function))
.function(wrap_function!(bench_callback_function))
.function(wrap_function!(start_instrumentation))
.function(wrap_function!(stop_instrumentation))
}
12 changes: 6 additions & 6 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@
php-dev
libclang.lib
clang
valgrind
];

nativeBuildInputs = [ pkgs.rust-bin.stable.latest.default ];

shellHook = ''
export LIBCLANG_PATH="${pkgs.libclang.lib}/lib"
export GUNGRAUN_VALGRIND_INCLUDE="${pkgs.valgrind.dev}/include"
'';
};
};
Expand Down
Loading