-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[libc++][format] Adds print benchmarks. #129765
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,79 @@ | ||||||||||
| //===----------------------------------------------------------------------===// | ||||||||||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||||||||
| // See https://llvm.org/LICENSE.txt for license information. | ||||||||||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||||||||
| // | ||||||||||
| //===----------------------------------------------------------------------===// | ||||||||||
|
|
||||||||||
| // REQUIRES: std-at-least-c++23 | ||||||||||
|
|
||||||||||
| // This benchmark writes the print and benchmark data to stdout. In order to | ||||||||||
| // preserve the benchmark output it needs to be stored in a file (using the | ||||||||||
| // console format). Another issue with the benchmark is the time it takes to | ||||||||||
| // write output to the real terminal. In order to avoid that overhead write the | ||||||||||
| // output to a fast "terminal", like /dev/null. For example, the printf | ||||||||||
| // console 1546 ns | ||||||||||
| // /dev/null 70.9 ns | ||||||||||
| // An example of a good test invocation. | ||||||||||
| // BENCHMARK_OUT=benchmark.txt BENCHMARK_OUT_FORMAT=console <exe> >/dev/null | ||||||||||
|
Comment on lines
+17
to
+18
Member
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.
Suggested change
What do you mean here? The canonical way of running benchmarks is Is this not how this benchmark should be run? If we should be adding additional environment variables to the benchmark when running it, we should find a way for a vanilla
Member
Author
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. That's indeed the case. This tests This patch was originally written a while ago for our non-lit benchmarking so I haven't investigated the lit invocation.
Member
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. One thing we could potentially do is ensure that any
Member
Author
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. I think we could add a new lit keyword. The issue with this test is the same for other potential benchmarks that write to the standard output facilities. So something along the lines of 'BENCHMARK_USES_TERMINAL (Obviously Changing the output to a Another solution is to use IMO the cleanest solution is to always write the benchmark output to " |
||||||||||
|
|
||||||||||
| #include <print> | ||||||||||
|
|
||||||||||
| #include <cstdio> | ||||||||||
| #include <string> | ||||||||||
| #include <vector> | ||||||||||
|
|
||||||||||
| #include "benchmark/benchmark.h" | ||||||||||
|
|
||||||||||
| void printf(benchmark::State& s) { | ||||||||||
| while (s.KeepRunning()) | ||||||||||
| std::printf("The answer to life, the universe, and everything is %d.\n", 42); | ||||||||||
| } | ||||||||||
| BENCHMARK(printf); | ||||||||||
|
|
||||||||||
| void vprint_string(std::string_view fmt, std::format_args args) { | ||||||||||
| auto s = std::vformat(fmt, args); | ||||||||||
| std::size_t result = fwrite(s.data(), 1, s.size(), stdout); | ||||||||||
|
Member
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.
Suggested change
Also below. |
||||||||||
| if (result < s.size()) | ||||||||||
| throw std::format_error("fwrite error"); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| template <typename... T> | ||||||||||
| void print_string(std::format_string<T...> fmt, T&&... args) { | ||||||||||
| vprint_string(fmt.get(), std::make_format_args(args...)); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| void print_string(benchmark::State& s) { | ||||||||||
| while (s.KeepRunning()) { | ||||||||||
|
Member
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. I think the preferred way of using GoogleBenchmark is to use a range-based for-loop: https://github.com/google/benchmark/blob/main/docs/user_guide.md#a-faster-keep-running-loop This can be re-written as |
||||||||||
| print_string("The answer to life, the universe, and everything is {}.\n", 42); | ||||||||||
| } | ||||||||||
| } | ||||||||||
| BENCHMARK(print_string); | ||||||||||
|
|
||||||||||
| void vprint_stack(std::string_view fmt, std::format_args args) { | ||||||||||
| auto buf = std::vector<char>{}; | ||||||||||
| std::vformat_to(std::back_inserter(buf), fmt, args); | ||||||||||
| std::size_t result = fwrite(buf.data(), 1, buf.size(), stdout); | ||||||||||
| if (result < buf.size()) | ||||||||||
| throw std::format_error("fwrite error"); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| template <typename... T> | ||||||||||
| void print_stack(std::format_string<T...> fmt, T&&... args) { | ||||||||||
| vprint_stack(fmt.get(), std::make_format_args(args...)); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| void print_stack(benchmark::State& s) { | ||||||||||
| while (s.KeepRunning()) { | ||||||||||
| print_stack("The answer to life, the universe, and everything is {}.\n", 42); | ||||||||||
| } | ||||||||||
| } | ||||||||||
| BENCHMARK(print_stack); | ||||||||||
|
|
||||||||||
| void print_direct(benchmark::State& s) { | ||||||||||
| while (s.KeepRunning()) | ||||||||||
| std::print("The answer to life, the universe, and everything is {}.\n", 42); | ||||||||||
| } | ||||||||||
| BENCHMARK(print_direct); | ||||||||||
|
|
||||||||||
| BENCHMARK_MAIN(); | ||||||||||
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.
This seems like an unterminated sentence?