Skip to content

Commit

Permalink
pw_allocator: Add a benchmark test harness
Browse files Browse the repository at this point in the history
This CL enables collecting benchmarks that can be used to evaluate
improvements to performance, overhead, and/or fragmenetaion of
subsequent CLs.

The benchmarks can be collected by running the benchmark.py script,
which generates a CSV file that can be added to
go/pw_allocator-benchmarks.

Change-Id: I83e7311fc5a993f14d8d1180a718a530f54d5c56
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/231331
Lint: Lint 🤖 <[email protected]>
Commit-Queue: Aaron Green <[email protected]>
Reviewed-by: Taylor Cramer <[email protected]>
Docs-Not-Needed: Aaron Green <[email protected]>
  • Loading branch information
nopsledder authored and CQ Bot Account committed Oct 28, 2024
1 parent ef5f6bf commit 59e05fa
Show file tree
Hide file tree
Showing 24 changed files with 1,822 additions and 7 deletions.
8 changes: 8 additions & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,9 @@ if (current_toolchain != default_toolchain) {
"$dir_pw_rpc:test_rpc_server",
"$dir_pw_unit_test:test_rpc_server",
]

# Build the benchmarks, but don't run them by default.
deps += [ ":pw_benchmarks" ]
}
}

Expand Down Expand Up @@ -663,6 +666,11 @@ if (current_toolchain != default_toolchain) {
output_metadata = true
}

# TODO(b/369853273): Merge benchmarks and perf tests
group("pw_benchmarks") {
deps = [ "$dir_pw_allocator/benchmarks" ]
}

# Fuzzers not based on a fuzzing engine. Engine-based fuzzers should be
# included in `pw_module_tests`.
pw_test_group("pw_custom_fuzzers") {
Expand Down
5 changes: 4 additions & 1 deletion pw_allocator/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,10 @@ pw_test_group("tests") {
":unique_ptr_test",
":worst_fit_block_allocator_test",
]
group_deps = [ "examples" ]
group_deps = [
"benchmarks:tests",
"examples",
]
}

# Docs
Expand Down
1 change: 1 addition & 0 deletions pw_allocator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -727,4 +727,5 @@ pw_add_test(pw_allocator.worst_fit_block_allocator_test
pw_allocator
)

add_subdirectory(benchmarks)
add_subdirectory(examples)
151 changes: 151 additions & 0 deletions pw_allocator/benchmarks/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
# Copyright 2020 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

load(
"//pw_build:pigweed.bzl",
"pw_cc_test",
)

package(default_visibility = ["//visibility:public"])

licenses(["notice"])

# Libraries

cc_library(
name = "measurements",
testonly = True,
srcs = [
"measurements.cc",
],
hdrs = [
"public/pw_allocator/benchmarks/measurements.h",
],
includes = ["public"],
deps = [
"//pw_chrono:system_clock",
"//pw_containers:intrusive_map",
"//pw_metric:metric",
],
)

cc_library(
name = "benchmark",
testonly = True,
srcs = [
"benchmark.cc",
],
hdrs = [
"public/pw_allocator/benchmarks/benchmark.h",
"public/pw_allocator/benchmarks/config.h",
],
includes = ["public"],
target_compatible_with = select({
"@platforms//os:linux": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
deps = [
":measurements",
"//pw_allocator:block_allocator",
"//pw_allocator:fragmentation",
"//pw_allocator:test_harness",
"//pw_chrono:system_clock",
"//pw_metric:metric",
"//pw_tokenizer",
],
)

# Binaries

cc_binary(
name = "best_fit_benchmark",
testonly = True,
srcs = [
"best_fit_benchmark.cc",
],
deps = [
":benchmark",
"//pw_allocator:best_fit_block_allocator",
"//pw_random",
],
)

cc_binary(
name = "dual_first_fit_benchmark",
testonly = True,
srcs = [
"dual_first_fit_benchmark.cc",
],
deps = [
":benchmark",
"//pw_allocator:dual_first_fit_block_allocator",
"//pw_random",
],
)

cc_binary(
name = "first_fit_benchmark",
testonly = True,
srcs = [
"first_fit_benchmark.cc",
],
deps = [
":benchmark",
"//pw_allocator:first_fit_block_allocator",
"//pw_random",
],
)

cc_binary(
name = "last_fit_benchmark",
testonly = True,
srcs = [
"last_fit_benchmark.cc",
],
deps = [
":benchmark",
"//pw_allocator:last_fit_block_allocator",
"//pw_random",
],
)

cc_binary(
name = "worst_fit_benchmark",
testonly = True,
srcs = [
"worst_fit_benchmark.cc",
],
deps = [
":benchmark",
"//pw_allocator:worst_fit_block_allocator",
"//pw_random",
],
)

# Unit tests

pw_cc_test(
name = "measurements_test",
srcs = ["measurements_test.cc"],
deps = [":measurements"],
)

pw_cc_test(
name = "benchmark_test",
srcs = ["benchmark_test.cc"],
deps = [
":benchmark",
"//pw_allocator:testing",
],
)
136 changes: 136 additions & 0 deletions pw_allocator/benchmarks/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# Copyright 2024 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

import("//build_overrides/pigweed.gni")

import("$dir_pw_build/target_types.gni")
import("$dir_pw_chrono/backend.gni")
import("$dir_pw_unit_test/test.gni")

group("benchmarks") {
deps = [
":best_fit_benchmark",
":dual_first_fit_benchmark",
":first_fit_benchmark",
":last_fit_benchmark",
":worst_fit_benchmark",
]
}

config("public_include_path") {
include_dirs = [ "public" ]
visibility = [ ":*" ]
}

# Libraries

pw_source_set("measurements") {
public_configs = [ ":public_include_path" ]
public = [ "public/pw_allocator/benchmarks/measurements.h" ]
public_deps = [
"$dir_pw_chrono:system_clock",
"$dir_pw_containers:intrusive_map",
dir_pw_metric,
]
sources = [ "measurements.cc" ]
}

pw_source_set("benchmark") {
public_configs = [ ":public_include_path" ]
public = [
"public/pw_allocator/benchmarks/benchmark.h",
"public/pw_allocator/benchmarks/config.h",
]
public_deps = [
":measurements",
"$dir_pw_allocator:block_allocator",
"$dir_pw_allocator:fragmentation",
"$dir_pw_allocator:test_harness",
"$dir_pw_chrono:system_clock",
dir_pw_metric,
dir_pw_tokenizer,
]
sources = [ "benchmark.cc" ]
}

# Binaries

pw_executable("best_fit_benchmark") {
sources = [ "best_fit_benchmark.cc" ]
deps = [
":benchmark",
"$dir_pw_allocator:best_fit_block_allocator",
"$dir_pw_random",
]
}

pw_executable("dual_first_fit_benchmark") {
sources = [ "dual_first_fit_benchmark.cc" ]
deps = [
":benchmark",
"$dir_pw_allocator:dual_first_fit_block_allocator",
"$dir_pw_random",
]
}

pw_executable("first_fit_benchmark") {
sources = [ "first_fit_benchmark.cc" ]
deps = [
":benchmark",
"$dir_pw_allocator:first_fit_block_allocator",
"$dir_pw_random",
]
}

pw_executable("last_fit_benchmark") {
sources = [ "last_fit_benchmark.cc" ]
deps = [
":benchmark",
"$dir_pw_allocator:last_fit_block_allocator",
"$dir_pw_random",
]
}

pw_executable("worst_fit_benchmark") {
sources = [ "worst_fit_benchmark.cc" ]
deps = [
":benchmark",
"$dir_pw_allocator:worst_fit_block_allocator",
"$dir_pw_random",
]
}

# Unit tests

pw_test("measurements_test") {
enable_if = pw_chrono_SYSTEM_CLOCK_BACKEND != ""
deps = [ ":measurements" ]
sources = [ "measurements_test.cc" ]
}

pw_test("benchmark_test") {
enable_if = pw_chrono_SYSTEM_CLOCK_BACKEND != ""
deps = [
":benchmark",
"$dir_pw_allocator:testing",
]
sources = [ "benchmark_test.cc" ]
}

pw_test_group("tests") {
tests = [
":benchmark_test",
":measurements_test",
]
}
Loading

0 comments on commit 59e05fa

Please sign in to comment.