Nico Weber | f938755 | 2019-08-21 01:59:12 | [diff] [blame] | 1 | #include "GenerateInput.h" |
Louis Dionne | 5aa03b6 | 2023-06-16 13:49:04 | [diff] [blame] | 2 | #include "benchmark/benchmark.h" |
Nico Weber | cc89063 | 2019-08-21 00:14:12 | [diff] [blame] | 3 | #include "filesystem_include.h" |
Louis Dionne | 5aa03b6 | 2023-06-16 13:49:04 | [diff] [blame] | 4 | #include "test_iterators.h" |
Eric Fiselier | 3aa5478 | 2016-10-30 22:53:00 | [diff] [blame] | 5 | |
| 6 | static const size_t TestNumInputs = 1024; |
| 7 | |
Eric Fiselier | 3aa5478 | 2016-10-30 22:53:00 | [diff] [blame] | 8 | template <class GenInputs> |
Louis Dionne | 5aa03b6 | 2023-06-16 13:49:04 | [diff] [blame] | 9 | void BM_PathConstructString(benchmark::State& st, GenInputs gen) { |
Eric Fiselier | d7fae18 | 2018-04-02 23:03:41 | [diff] [blame] | 10 | using fs::path; |
Eric Fiselier | 3aa5478 | 2016-10-30 22:53:00 | [diff] [blame] | 11 | const auto in = gen(st.range(0)); |
| 12 | path PP; |
| 13 | for (auto& Part : in) |
| 14 | PP /= Part; |
| 15 | benchmark::DoNotOptimize(PP.native().data()); |
| 16 | while (st.KeepRunning()) { |
| 17 | const path P(PP.native()); |
| 18 | benchmark::DoNotOptimize(P.native().data()); |
| 19 | } |
Eric Fiselier | d7fae18 | 2018-04-02 23:03:41 | [diff] [blame] | 20 | st.SetComplexityN(st.range(0)); |
Eric Fiselier | 3aa5478 | 2016-10-30 22:53:00 | [diff] [blame] | 21 | } |
Louis Dionne | 5aa03b6 | 2023-06-16 13:49:04 | [diff] [blame] | 22 | BENCHMARK_CAPTURE(BM_PathConstructString, large_string, getRandomStringInputs)->Range(8, TestNumInputs)->Complexity(); |
Eric Fiselier | 3aa5478 | 2016-10-30 22:53:00 | [diff] [blame] | 23 | |
| 24 | template <class GenInputs> |
Louis Dionne | 5aa03b6 | 2023-06-16 13:49:04 | [diff] [blame] | 25 | void BM_PathConstructCStr(benchmark::State& st, GenInputs gen) { |
Eric Fiselier | d7fae18 | 2018-04-02 23:03:41 | [diff] [blame] | 26 | using fs::path; |
Eric Fiselier | ef915d3 | 2016-10-30 23:53:50 | [diff] [blame] | 27 | const auto in = gen(st.range(0)); |
| 28 | path PP; |
| 29 | for (auto& Part : in) |
| 30 | PP /= Part; |
| 31 | benchmark::DoNotOptimize(PP.native().data()); |
| 32 | while (st.KeepRunning()) { |
| 33 | const path P(PP.native().c_str()); |
| 34 | benchmark::DoNotOptimize(P.native().data()); |
| 35 | } |
| 36 | } |
Louis Dionne | 5aa03b6 | 2023-06-16 13:49:04 | [diff] [blame] | 37 | BENCHMARK_CAPTURE(BM_PathConstructCStr, large_string, getRandomStringInputs)->Arg(TestNumInputs); |
Eric Fiselier | a553330 | 2016-10-31 02:46:25 | [diff] [blame] | 38 | |
| 39 | template <template <class...> class ItType, class GenInputs> |
Louis Dionne | 5aa03b6 | 2023-06-16 13:49:04 | [diff] [blame] | 40 | void BM_PathConstructIter(benchmark::State& st, GenInputs gen) { |
Eric Fiselier | d7fae18 | 2018-04-02 23:03:41 | [diff] [blame] | 41 | using fs::path; |
Louis Dionne | 5aa03b6 | 2023-06-16 13:49:04 | [diff] [blame] | 42 | using Iter = ItType<std::string::const_iterator>; |
Eric Fiselier | a553330 | 2016-10-31 02:46:25 | [diff] [blame] | 43 | const auto in = gen(st.range(0)); |
| 44 | path PP; |
| 45 | for (auto& Part : in) |
| 46 | PP /= Part; |
| 47 | auto Start = Iter(PP.native().begin()); |
Louis Dionne | 5aa03b6 | 2023-06-16 13:49:04 | [diff] [blame] | 48 | auto End = Iter(PP.native().end()); |
Eric Fiselier | a553330 | 2016-10-31 02:46:25 | [diff] [blame] | 49 | benchmark::DoNotOptimize(PP.native().data()); |
| 50 | benchmark::DoNotOptimize(Start); |
| 51 | benchmark::DoNotOptimize(End); |
| 52 | while (st.KeepRunning()) { |
| 53 | const path P(Start, End); |
| 54 | benchmark::DoNotOptimize(P.native().data()); |
| 55 | } |
Eric Fiselier | d7fae18 | 2018-04-02 23:03:41 | [diff] [blame] | 56 | st.SetComplexityN(st.range(0)); |
Eric Fiselier | a553330 | 2016-10-31 02:46:25 | [diff] [blame] | 57 | } |
| 58 | template <class GenInputs> |
Louis Dionne | 5aa03b6 | 2023-06-16 13:49:04 | [diff] [blame] | 59 | void BM_PathConstructInputIter(benchmark::State& st, GenInputs gen) { |
Christopher Di Bella | 773ae44 | 2021-04-24 21:31:23 | [diff] [blame] | 60 | BM_PathConstructIter<cpp17_input_iterator>(st, gen); |
Eric Fiselier | a553330 | 2016-10-31 02:46:25 | [diff] [blame] | 61 | } |
| 62 | template <class GenInputs> |
Louis Dionne | 5aa03b6 | 2023-06-16 13:49:04 | [diff] [blame] | 63 | void BM_PathConstructForwardIter(benchmark::State& st, GenInputs gen) { |
Eric Fiselier | a553330 | 2016-10-31 02:46:25 | [diff] [blame] | 64 | BM_PathConstructIter<forward_iterator>(st, gen); |
| 65 | } |
Louis Dionne | 5aa03b6 | 2023-06-16 13:49:04 | [diff] [blame] | 66 | BENCHMARK_CAPTURE(BM_PathConstructInputIter, large_string, getRandomStringInputs) |
| 67 | ->Range(8, TestNumInputs) |
| 68 | ->Complexity(); |
| 69 | BENCHMARK_CAPTURE(BM_PathConstructForwardIter, large_string, getRandomStringInputs) |
| 70 | ->Range(8, TestNumInputs) |
| 71 | ->Complexity(); |
Eric Fiselier | a553330 | 2016-10-31 02:46:25 | [diff] [blame] | 72 | |
Eric Fiselier | ef915d3 | 2016-10-30 23:53:50 | [diff] [blame] | 73 | template <class GenInputs> |
Louis Dionne | 5aa03b6 | 2023-06-16 13:49:04 | [diff] [blame] | 74 | void BM_PathIterateMultipleTimes(benchmark::State& st, GenInputs gen) { |
Eric Fiselier | d7fae18 | 2018-04-02 23:03:41 | [diff] [blame] | 75 | using fs::path; |
Eric Fiselier | 3aa5478 | 2016-10-30 22:53:00 | [diff] [blame] | 76 | const auto in = gen(st.range(0)); |
| 77 | path PP; |
| 78 | for (auto& Part : in) |
| 79 | PP /= Part; |
| 80 | benchmark::DoNotOptimize(PP.native().data()); |
| 81 | while (st.KeepRunning()) { |
Louis Dionne | a2afc82 | 2022-01-24 17:05:09 | [diff] [blame] | 82 | for (auto const& E : PP) { |
Eric Fiselier | 3aa5478 | 2016-10-30 22:53:00 | [diff] [blame] | 83 | benchmark::DoNotOptimize(E.native().data()); |
| 84 | } |
| 85 | benchmark::ClobberMemory(); |
| 86 | } |
Eric Fiselier | d7fae18 | 2018-04-02 23:03:41 | [diff] [blame] | 87 | st.SetComplexityN(st.range(0)); |
Eric Fiselier | 3aa5478 | 2016-10-30 22:53:00 | [diff] [blame] | 88 | } |
Louis Dionne | 5aa03b6 | 2023-06-16 13:49:04 | [diff] [blame] | 89 | BENCHMARK_CAPTURE(BM_PathIterateMultipleTimes, iterate_elements, getRandomStringInputs) |
| 90 | ->Range(8, TestNumInputs) |
| 91 | ->Complexity(); |
Eric Fiselier | 3aa5478 | 2016-10-30 22:53:00 | [diff] [blame] | 92 | |
| 93 | template <class GenInputs> |
Louis Dionne | 5aa03b6 | 2023-06-16 13:49:04 | [diff] [blame] | 94 | void BM_PathIterateOnce(benchmark::State& st, GenInputs gen) { |
Eric Fiselier | d7fae18 | 2018-04-02 23:03:41 | [diff] [blame] | 95 | using fs::path; |
Eric Fiselier | 3aa5478 | 2016-10-30 22:53:00 | [diff] [blame] | 96 | const auto in = gen(st.range(0)); |
| 97 | path PP; |
| 98 | for (auto& Part : in) |
| 99 | PP /= Part; |
| 100 | benchmark::DoNotOptimize(PP.native().data()); |
| 101 | while (st.KeepRunning()) { |
| 102 | const path P = PP.native(); |
Louis Dionne | a2afc82 | 2022-01-24 17:05:09 | [diff] [blame] | 103 | for (auto const& E : P) { |
Eric Fiselier | 3aa5478 | 2016-10-30 22:53:00 | [diff] [blame] | 104 | benchmark::DoNotOptimize(E.native().data()); |
| 105 | } |
| 106 | benchmark::ClobberMemory(); |
| 107 | } |
Eric Fiselier | d7fae18 | 2018-04-02 23:03:41 | [diff] [blame] | 108 | st.SetComplexityN(st.range(0)); |
Eric Fiselier | 3aa5478 | 2016-10-30 22:53:00 | [diff] [blame] | 109 | } |
Louis Dionne | 5aa03b6 | 2023-06-16 13:49:04 | [diff] [blame] | 110 | BENCHMARK_CAPTURE(BM_PathIterateOnce, iterate_elements, getRandomStringInputs)->Range(8, TestNumInputs)->Complexity(); |
Eric Fiselier | 3aa5478 | 2016-10-30 22:53:00 | [diff] [blame] | 111 | |
| 112 | template <class GenInputs> |
Louis Dionne | 5aa03b6 | 2023-06-16 13:49:04 | [diff] [blame] | 113 | void BM_PathIterateOnceBackwards(benchmark::State& st, GenInputs gen) { |
Eric Fiselier | d7fae18 | 2018-04-02 23:03:41 | [diff] [blame] | 114 | using fs::path; |
Eric Fiselier | 3aa5478 | 2016-10-30 22:53:00 | [diff] [blame] | 115 | const auto in = gen(st.range(0)); |
| 116 | path PP; |
| 117 | for (auto& Part : in) |
| 118 | PP /= Part; |
| 119 | benchmark::DoNotOptimize(PP.native().data()); |
| 120 | while (st.KeepRunning()) { |
| 121 | const path P = PP.native(); |
| 122 | const auto B = P.begin(); |
Louis Dionne | 5aa03b6 | 2023-06-16 13:49:04 | [diff] [blame] | 123 | auto I = P.end(); |
Eric Fiselier | 3aa5478 | 2016-10-30 22:53:00 | [diff] [blame] | 124 | while (I != B) { |
| 125 | --I; |
| 126 | benchmark::DoNotOptimize(*I); |
| 127 | } |
| 128 | benchmark::DoNotOptimize(*I); |
| 129 | } |
| 130 | } |
Louis Dionne | 5aa03b6 | 2023-06-16 13:49:04 | [diff] [blame] | 131 | BENCHMARK_CAPTURE(BM_PathIterateOnceBackwards, iterate_elements, getRandomStringInputs)->Arg(TestNumInputs); |
Eric Fiselier | 3aa5478 | 2016-10-30 22:53:00 | [diff] [blame] | 132 | |
Eric Fiselier | d7fae18 | 2018-04-02 23:03:41 | [diff] [blame] | 133 | static fs::path getRandomPaths(int NumParts, int PathLen) { |
| 134 | fs::path Result; |
| 135 | while (NumParts--) { |
| 136 | std::string Part = getRandomString(PathLen); |
| 137 | Result /= Part; |
| 138 | } |
| 139 | return Result; |
| 140 | } |
| 141 | |
| 142 | template <class GenInput> |
Louis Dionne | 5aa03b6 | 2023-06-16 13:49:04 | [diff] [blame] | 143 | void BM_LexicallyNormal(benchmark::State& st, GenInput gen, size_t PathLen) { |
Eric Fiselier | d7fae18 | 2018-04-02 23:03:41 | [diff] [blame] | 144 | using fs::path; |
| 145 | auto In = gen(st.range(0), PathLen); |
| 146 | benchmark::DoNotOptimize(&In); |
| 147 | while (st.KeepRunning()) { |
| 148 | benchmark::DoNotOptimize(In.lexically_normal()); |
| 149 | } |
| 150 | st.SetComplexityN(st.range(0)); |
| 151 | } |
Louis Dionne | 5aa03b6 | 2023-06-16 13:49:04 | [diff] [blame] | 152 | BENCHMARK_CAPTURE(BM_LexicallyNormal, small_path, getRandomPaths, /*PathLen*/ 5) |
| 153 | ->RangeMultiplier(2) |
| 154 | ->Range(2, 256) |
| 155 | ->Complexity(); |
| 156 | BENCHMARK_CAPTURE(BM_LexicallyNormal, large_path, getRandomPaths, /*PathLen*/ 32) |
| 157 | ->RangeMultiplier(2) |
| 158 | ->Range(2, 256) |
| 159 | ->Complexity(); |
Eric Fiselier | d7fae18 | 2018-04-02 23:03:41 | [diff] [blame] | 160 | |
Eric Fiselier | 1903976 | 2018-01-18 04:23:01 | [diff] [blame] | 161 | BENCHMARK_MAIN(); |