Skip to content

Commit b84d783

Browse files
committed
test: cover process isolation end to end
Build a fixture in simulation mode and check that: - each benchmark runs in its own process by default, including one that uses a thread started in main; - CODSPEED_PROCESS_ISOLATION=false runs them all in one process; - a failing benchmark fails the run with its exit code; - the child ignores the command line filter; - an invalid setting is rejected. Register it with ctest and as a Bazel sh_test, and run it in CI.
1 parent 4ed2aee commit b84d783

5 files changed

Lines changed: 292 additions & 0 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,22 @@ jobs:
102102
mode: ${{ matrix.codspeed-mode }}
103103
run: examples/google_benchmark_cmake/build/benchmark_example
104104

105+
process-isolation-tests:
106+
runs-on: ubuntu-latest
107+
steps:
108+
- name: Checkout code
109+
uses: actions/checkout@v3
110+
with:
111+
submodules: "recursive"
112+
113+
- name: Build and run the process isolation test
114+
run: |
115+
mkdir -p tests/process_isolation/build
116+
cd tests/process_isolation/build
117+
cmake -DCODSPEED_MODE=simulation -DCODSPEED_STRICT_WARNINGS=ON ..
118+
make -j
119+
ctest --output-on-failure
120+
105121
bazel-integration-tests:
106122
strategy:
107123
matrix:
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_binary")
2+
3+
cc_binary(
4+
name = "process_isolation_fixture",
5+
srcs = ["main.cpp"],
6+
copts = ["-Wall", "-Wextra", "-Werror"],
7+
linkopts = ["-pthread"],
8+
deps = ["@codspeed_google_benchmark_compat//:benchmark"],
9+
)
10+
11+
# Run with --@codspeed_core//:codspeed_mode=simulation (or another analysis
12+
# mode), process isolation does not exist in the other modes.
13+
sh_test(
14+
name = "process_isolation_test",
15+
srcs = ["run_test.sh"],
16+
args = ["$(location :process_isolation_fixture)"],
17+
data = [":process_isolation_fixture"],
18+
)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
cmake_minimum_required(VERSION 3.12)
2+
include(FetchContent)
3+
4+
project(codspeed_process_isolation_test VERSION 0.0.0 LANGUAGES CXX)
5+
6+
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
7+
add_compile_options(-Wall -Wextra -Werror)
8+
endif()
9+
10+
set(CMAKE_BUILD_TYPE RelWithDebInfo)
11+
12+
# Keep ctest limited to the isolation test.
13+
set(BENCHMARK_ENABLE_TESTING
14+
OFF
15+
CACHE BOOL
16+
"Enable testing of the benchmark library."
17+
FORCE
18+
)
19+
set(BENCHMARK_ENABLE_GTEST_TESTS
20+
OFF
21+
CACHE BOOL
22+
"Enable testing of the benchmark library."
23+
FORCE
24+
)
25+
26+
FetchContent_Declare(
27+
google_benchmark
28+
SOURCE_DIR ${CMAKE_SOURCE_DIR}/../../google_benchmark
29+
)
30+
FetchContent_MakeAvailable(google_benchmark)
31+
32+
find_package(Threads REQUIRED)
33+
add_executable(process_isolation_fixture main.cpp)
34+
target_link_libraries(
35+
process_isolation_fixture
36+
PRIVATE benchmark::benchmark Threads::Threads
37+
)
38+
39+
# Process isolation only exists in the analysis modes.
40+
enable_testing()
41+
add_test(
42+
NAME process_isolation
43+
COMMAND
44+
${CMAKE_CURRENT_SOURCE_DIR}/run_test.sh
45+
$<TARGET_FILE:process_isolation_fixture>
46+
)

‎tests/process_isolation/main.cpp‎

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#include <benchmark/benchmark.h>
2+
#include <unistd.h>
3+
4+
#include <condition_variable>
5+
#include <cstdio>
6+
#include <cstdlib>
7+
#include <functional>
8+
#include <future>
9+
#include <mutex>
10+
#include <queue>
11+
#include <string>
12+
#include <thread>
13+
14+
// Set to make `BM_Failing` exit with `FAILING_EXIT_CODE`.
15+
static const char* const FAIL_ENV = "FIXTURE_FAIL";
16+
static constexpr int FAILING_EXIT_CODE = 3;
17+
18+
// Started in `main`, before any benchmark, so its thread belongs to the
19+
// process that runs the harness. A forked child would not have it.
20+
class MainThreadWorker {
21+
public:
22+
MainThreadWorker() : thread_([this] { loop(); }) {}
23+
24+
~MainThreadWorker() {
25+
{
26+
std::lock_guard<std::mutex> lock(mutex_);
27+
done_ = true;
28+
}
29+
wakeup_.notify_one();
30+
thread_.join();
31+
}
32+
33+
int submit(int value) {
34+
std::promise<int> result;
35+
{
36+
std::lock_guard<std::mutex> lock(mutex_);
37+
jobs_.push([&result, value] { result.set_value(value + 1); });
38+
}
39+
wakeup_.notify_one();
40+
return result.get_future().get();
41+
}
42+
43+
private:
44+
void loop() {
45+
std::unique_lock<std::mutex> lock(mutex_);
46+
while (true) {
47+
wakeup_.wait(lock, [this] { return done_ || !jobs_.empty(); });
48+
if (jobs_.empty()) {
49+
return;
50+
}
51+
auto job = std::move(jobs_.front());
52+
jobs_.pop();
53+
lock.unlock();
54+
job();
55+
lock.lock();
56+
}
57+
}
58+
59+
std::mutex mutex_;
60+
std::condition_variable wakeup_;
61+
std::queue<std::function<void()>> jobs_;
62+
bool done_ = false;
63+
std::thread thread_;
64+
};
65+
66+
static MainThreadWorker* worker = nullptr;
67+
68+
// The test reads these lines to count the processes.
69+
static void print_pid_once(const char* name) {
70+
static std::mutex print_mutex;
71+
std::lock_guard<std::mutex> lock(print_mutex);
72+
static std::string printed;
73+
if (printed.find(name) != std::string::npos) {
74+
return;
75+
}
76+
printed += name;
77+
std::printf("PID %s %d\n", name, static_cast<int>(getpid()));
78+
std::fflush(stdout);
79+
}
80+
81+
static void BM_First(benchmark::State& state) {
82+
print_pid_once("first");
83+
for (auto _ : state) {
84+
benchmark::DoNotOptimize(1);
85+
}
86+
}
87+
BENCHMARK(BM_First);
88+
89+
static void BM_Second(benchmark::State& state) {
90+
print_pid_once("second");
91+
for (auto _ : state) {
92+
benchmark::DoNotOptimize(2);
93+
}
94+
}
95+
BENCHMARK(BM_Second);
96+
97+
static void BM_OnMainWorker(benchmark::State& state) {
98+
print_pid_once("worker");
99+
for (auto _ : state) {
100+
benchmark::DoNotOptimize(worker->submit(3));
101+
}
102+
}
103+
BENCHMARK(BM_OnMainWorker);
104+
105+
static void BM_Failing(benchmark::State& state) {
106+
print_pid_once("failing");
107+
for (auto _ : state) {
108+
std::exit(FAILING_EXIT_CODE);
109+
}
110+
}
111+
112+
int main(int argc, char** argv) {
113+
MainThreadWorker main_worker;
114+
worker = &main_worker;
115+
116+
if (std::getenv(FAIL_ENV) != nullptr) {
117+
benchmark::RegisterBenchmark("BM_Failing", BM_Failing);
118+
}
119+
120+
benchmark::Initialize(&argc, argv);
121+
benchmark::RunSpecifiedBenchmarks();
122+
benchmark::Shutdown();
123+
return 0;
124+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env bash
2+
# End to end test of process isolation: runs the fixture binary, built in an
3+
# analysis mode, and checks the processes it used and its exit code.
4+
set -u
5+
6+
FIXTURE="$1"
7+
EXPECTED_BENCHMARKS=(BM_First BM_Second BM_OnMainWorker)
8+
FAILING_EXIT_CODE=3
9+
TIMEOUT_SECONDS=120
10+
11+
failures=0
12+
fail() {
13+
echo "FAIL: $*" >&2
14+
failures=$((failures + 1))
15+
}
16+
17+
run_fixture() {
18+
timeout "$TIMEOUT_SECONDS" "$FIXTURE" "$@" >"$OUTPUT_FILE" 2>"$ERROR_FILE"
19+
return $?
20+
}
21+
22+
OUTPUT_FILE="$(mktemp)"
23+
ERROR_FILE="$(mktemp)"
24+
trap 'rm -f "$OUTPUT_FILE" "$ERROR_FILE"' EXIT
25+
26+
distinct_pids() {
27+
grep '^PID ' "$OUTPUT_FILE" | awk '{print $3}' | sort -u | wc -l
28+
}
29+
30+
check_all_benchmarks_ran() {
31+
local label="$1"
32+
for name in "${EXPECTED_BENCHMARKS[@]}"; do
33+
grep -q "::${name}" "$ERROR_FILE" || fail "$label: no 'Measured/Checked' line for $name"
34+
done
35+
}
36+
37+
# 1. Isolation on (default and explicit): one process per benchmark.
38+
for setting in "" "true" "1"; do
39+
label="isolation=${setting:-<unset>}"
40+
if [ -z "$setting" ]; then
41+
env -u CODSPEED_PROCESS_ISOLATION -u FIXTURE_FAIL \
42+
timeout "$TIMEOUT_SECONDS" "$FIXTURE" >"$OUTPUT_FILE" 2>"$ERROR_FILE"
43+
else
44+
CODSPEED_PROCESS_ISOLATION="$setting" run_fixture
45+
fi
46+
[ $? -eq 0 ] || fail "$label: expected exit code 0"
47+
check_all_benchmarks_ran "$label"
48+
pids=$(distinct_pids)
49+
[ "$pids" -eq "${#EXPECTED_BENCHMARKS[@]}" ] ||
50+
fail "$label: expected ${#EXPECTED_BENCHMARKS[@]} distinct PIDs, got $pids"
51+
# The parent prints the context once, children must stay quiet.
52+
[ "$(grep -c 'Run on' "$ERROR_FILE")" -le 1 ] || fail "$label: context printed more than once"
53+
done
54+
55+
# 2. Isolation off: all benchmarks share one process.
56+
for setting in "false" "0"; do
57+
label="isolation=$setting"
58+
CODSPEED_PROCESS_ISOLATION="$setting" run_fixture
59+
[ $? -eq 0 ] || fail "$label: expected exit code 0"
60+
check_all_benchmarks_ran "$label"
61+
pids=$(distinct_pids)
62+
[ "$pids" -eq 1 ] || fail "$label: expected 1 PID, got $pids"
63+
done
64+
65+
# 3. A failing benchmark fails the whole run, with or without isolation.
66+
for setting in "true" "false"; do
67+
label="failing, isolation=$setting"
68+
FIXTURE_FAIL=1 CODSPEED_PROCESS_ISOLATION="$setting" run_fixture
69+
code=$?
70+
[ "$code" -eq "$FAILING_EXIT_CODE" ] ||
71+
fail "$label: expected exit code $FAILING_EXIT_CODE, got $code"
72+
done
73+
74+
# 4. The child ignores the command line filter.
75+
CODSPEED_PROCESS_ISOLATION=true run_fixture --benchmark_filter=BM_First
76+
[ $? -eq 0 ] || fail "filter: expected exit code 0"
77+
grep -q "::BM_First" "$ERROR_FILE" || fail "filter: BM_First did not run"
78+
grep -q "::BM_Second" "$ERROR_FILE" && fail "filter: BM_Second ran despite the filter"
79+
80+
# 5. An invalid value is a hard error.
81+
CODSPEED_PROCESS_ISOLATION=maybe run_fixture
82+
[ $? -ne 0 ] || fail "invalid value: expected a non-zero exit code"
83+
grep -q "CODSPEED_PROCESS_ISOLATION" "$ERROR_FILE" || fail "invalid value: no error message"
84+
85+
if [ "$failures" -ne 0 ]; then
86+
exit 1
87+
fi
88+
echo "process isolation test passed"

0 commit comments

Comments
 (0)