1 // Copyright (C) 2022 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <getopt.h>
16 #include <unistd.h>
17 
18 #include <cstring>
19 #include <filesystem>
20 #include <string>
21 
22 #include <benchmark/benchmark.h>
23 #include <ditto/arg_parser.h>
24 #include <ditto/logger.h>
25 #include <ditto/parser.h>
26 
27 template <class... Args>
BM_DittoBench(benchmark::State & state,Args &&...args)28 void BM_DittoBench(benchmark::State& state, Args&&... args) {
29   for (auto _ : state) {
30     auto args_tuple = std::make_tuple(std::move(args)...);
31 
32     std::error_code errorCode;
33     auto myPath = std::filesystem::path("/proc/self/exe");
34     auto executable_path = std::filesystem::canonical(myPath, errorCode);
35     auto project_path = executable_path.parent_path();
36     std::string ditto_path = project_path.string() + "/" + std::get<0>(args_tuple);
37 
38     // LOGW("Opening ditto file: " + ditto_path);
39     //  TODO error code?
40 
41     dittosuite::CmdArguments arguments = {.results_output = dittosuite::ResultsOutput::kNull,
42                                           .file_path = ditto_path};
43 
44     dittosuite::Parser::GetParser().ParseFile(arguments.file_path, arguments.parameters);
45 
46     auto init = dittosuite::Parser::GetParser().GetInit();
47     if (init) {
48       init->SetUp();
49       init->Run();
50       init->TearDown();
51     }
52 
53     auto main = dittosuite::Parser::GetParser().GetMain();
54     main->SetUp();
55     main->Run();
56     main->TearDown();
57 
58     auto result = main->CollectResults("");
59     double duration_ns = result->GetSamples("duration")[0];
60     state.SetIterationTime(duration_ns / 1000 / 1000 / 1000);
61 
62     auto clean_up = dittosuite::Parser::GetParser().GetCleanUp();
63     if (clean_up) {
64       clean_up->SetUp();
65       clean_up->Run();
66       clean_up->TearDown();
67     }
68   }
69 }
70 
71 BENCHMARK_CAPTURE(BM_DittoBench, read_random_native, "example/android/read_random_native.ditto")->UseManualTime();
72 BENCHMARK_CAPTURE(BM_DittoBench, read_random_fuse, "example/android/read_random_fuse.ditto")->UseManualTime();
73 BENCHMARK_CAPTURE(BM_DittoBench, write_random_native, "example/android/write_random_native.ditto")->UseManualTime();
74 BENCHMARK_CAPTURE(BM_DittoBench, write_random_fuse, "example/android/write_random_fuse.ditto")->UseManualTime();
75 
76 BENCHMARK_MAIN();
77