-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Optimization to Skip this entire family #2105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
urmez
wants to merge
2
commits into
google:main
Choose a base branch
from
urmez:patch-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+140
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,115 @@ | ||
| #include <chrono> | ||
| #include <iostream> | ||
| #include <string> | ||
|
|
||
| #include "benchmark/benchmark.h" | ||
|
|
||
| namespace { | ||
|
|
||
| // Macro to create benchmark families with many arguments | ||
| #define CREATE_BENCHMARK_FAMILY(name) \ | ||
| void name(benchmark::State& state) { \ | ||
| for (auto _ : state) {} \ | ||
| } \ | ||
| BENCHMARK(name)->DenseRange(0, 999, 1); | ||
|
|
||
| // Create many benchmark families, each with 1000 instances (args 0-999) | ||
| CREATE_BENCHMARK_FAMILY(BM_Alpha) | ||
| CREATE_BENCHMARK_FAMILY(BM_Beta) | ||
| CREATE_BENCHMARK_FAMILY(BM_Gamma) | ||
| CREATE_BENCHMARK_FAMILY(BM_Delta) | ||
| CREATE_BENCHMARK_FAMILY(BM_Epsilon) | ||
| CREATE_BENCHMARK_FAMILY(BM_Zeta) | ||
| CREATE_BENCHMARK_FAMILY(BM_Eta) | ||
| CREATE_BENCHMARK_FAMILY(BM_Theta) | ||
| CREATE_BENCHMARK_FAMILY(BM_Iota) | ||
| CREATE_BENCHMARK_FAMILY(BM_Kappa) | ||
| CREATE_BENCHMARK_FAMILY(BM_Lambda) | ||
| CREATE_BENCHMARK_FAMILY(BM_Mu) | ||
| CREATE_BENCHMARK_FAMILY(BM_Nu) | ||
| CREATE_BENCHMARK_FAMILY(BM_Xi) | ||
| CREATE_BENCHMARK_FAMILY(BM_Omicron) | ||
|
|
||
| // The target benchmark we're looking for (also with 1000 instances) | ||
| CREATE_BENCHMARK_FAMILY(BM_TargetBenchmark) | ||
|
|
||
| class NullReporter : public benchmark::BenchmarkReporter { | ||
| public: | ||
| bool ReportContext(const Context&) override { return true; } | ||
| void ReportRuns(const std::vector<Run>&) override {} | ||
| void Finalize() override {} | ||
| }; | ||
|
|
||
| } // namespace | ||
|
|
||
| int main(int argc, char** argv) { | ||
| benchmark::MaybeReenterWithoutASLR(argc, argv); | ||
|
|
||
| std::cout << "\n=== Filter Optimization Performance Test ===\n"; | ||
| std::cout << "Total families: 16 (15 non-matching + 1 target)\n"; | ||
| std::cout << "Total instances: 16000 (16 families × 1000 args each)\n\n"; | ||
|
|
||
| // Measure time to filter with literal string (optimization applies) | ||
| NullReporter null_reporter; | ||
|
|
||
| std::cout << "Testing literal filter \"TargetBenchmark\"...\n"; | ||
| int argc1 = 3; | ||
| const char* argv1[] = {"test", "--benchmark_filter=TargetBenchmark", "--benchmark_list_tests"}; | ||
| benchmark::Initialize(&argc1, const_cast<char**>(argv1)); | ||
| auto start_literal = std::chrono::high_resolution_clock::now(); | ||
| size_t count_literal = benchmark::RunSpecifiedBenchmarks(&null_reporter); | ||
| auto end_literal = std::chrono::high_resolution_clock::now(); | ||
| auto duration_literal = std::chrono::duration_cast<std::chrono::microseconds>(end_literal - start_literal); | ||
|
|
||
| std::cout << "Testing regex filter \".*TargetBenchmark.*\"...\n"; | ||
| int argc2 = 3; | ||
| const char* argv2[] = {"test", "--benchmark_filter=.*TargetBenchmark.*", "--benchmark_list_tests"}; | ||
| benchmark::Initialize(&argc2, const_cast<char**>(argv2)); | ||
| auto start_regex = std::chrono::high_resolution_clock::now(); | ||
| size_t count_regex = benchmark::RunSpecifiedBenchmarks(&null_reporter); | ||
| auto end_regex = std::chrono::high_resolution_clock::now(); | ||
| auto duration_regex = std::chrono::duration_cast<std::chrono::microseconds>(end_regex - start_regex); | ||
|
|
||
| // Verify both found exactly 1000 benchmarks (all instances of BM_TargetBenchmark) | ||
| if (count_literal != 1000) { | ||
| std::cerr << "ERROR: Literal filter expected 1000 matches, got " << count_literal << "\n"; | ||
| return -1; | ||
| } | ||
| if (count_regex != 1000) { | ||
| std::cerr << "ERROR: Regex filter expected 1000 matches, got " << count_regex << "\n"; | ||
| return -1; | ||
| } | ||
|
|
||
| std::cout << "\n=== RESULTS ===\n"; | ||
| std::cout << "Literal filter \"TargetBenchmark\": " << duration_literal.count() << " μs\n"; | ||
| std::cout << "Regex filter \".*TargetBenchmark.*\": " << duration_regex.count() << " μs\n\n"; | ||
|
|
||
| if (duration_literal.count() > 0) { | ||
| double speedup = static_cast<double>(duration_regex.count()) / duration_literal.count(); | ||
| std::cout << "Speedup with optimization: " << speedup << "x faster\n\n"; | ||
|
|
||
| // Verify optimization provides at least 5x speedup | ||
| if (speedup < 5.0) { | ||
| std::cerr << "ERROR: Expected at least 5x speedup, got " << speedup << "x\n"; | ||
| std::cerr << "Optimization may not be working correctly!\n"; | ||
| return -1; | ||
| } | ||
| } else { | ||
| std::cerr << "ERROR: Literal filter completed too fast to measure accurately\n"; | ||
| return -1; | ||
| } | ||
|
Comment on lines
+91
to
+100
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is not a good idea to make tests depend on host machie performance. |
||
|
|
||
| std::cout << "=== WHY THE DIFFERENCE? ===\n"; | ||
| std::cout << "Literal filter (\"TargetBenchmark\"):\n"; | ||
| std::cout << " - Detects no regex metacharacters\n"; | ||
| std::cout << " - Uses family->name_.find(\"TargetBenchmark\")\n"; | ||
| std::cout << " - Skips 15 families immediately (15000 instances not generated)\n"; | ||
| std::cout << " - Only processes BM_TargetBenchmark family (1000 instances generated)\n\n"; | ||
|
|
||
| std::cout << "Regex filter (\".*TargetBenchmark.*\"):\n"; | ||
| std::cout << " - Detects metacharacters (. and *)\n"; | ||
| std::cout << " - Must process ALL 16 families\n"; | ||
| std::cout << " - Generates all 16000 instances and regex-matches each name\n\n"; | ||
|
|
||
| return 0; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't this errneously skip
BM_Family/arg1/arg2given filterBM_Family/arg?