xref: /aosp_15_r20/external/cronet/base/test/perf_test_suite.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2010 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/test/perf_test_suite.h"
6 
7 #include "base/command_line.h"
8 #include "base/debug/debugger.h"
9 #include "base/files/file_path.h"
10 #include "base/path_service.h"
11 #include "base/process/launch.h"
12 #include "base/strings/string_util.h"
13 #include "base/test/perf_log.h"
14 #include "build/build_config.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/google_benchmark/src/include/benchmark/benchmark.h"
17 
18 #if BUILDFLAG(IS_FUCHSIA)
19 #include "base/fuchsia/file_utils.h"
20 #endif
21 
22 namespace base {
23 
PerfTestSuite(int argc,char ** argv)24 PerfTestSuite::PerfTestSuite(int argc, char** argv) : TestSuite(argc, argv) {}
25 
Initialize()26 void PerfTestSuite::Initialize() {
27   TestSuite::Initialize();
28 
29   // Initialize the perf timer log
30   FilePath log_path =
31       CommandLine::ForCurrentProcess()->GetSwitchValuePath("log-file");
32   if (log_path.empty()) {
33     PathService::Get(FILE_EXE, &log_path);
34 #if BUILDFLAG(IS_ANDROID)
35     FilePath tmp_dir;
36     PathService::Get(DIR_CACHE, &tmp_dir);
37     log_path = tmp_dir.Append(log_path.BaseName());
38 #elif BUILDFLAG(IS_FUCHSIA)
39     log_path =
40         FilePath(kPersistedDataDirectoryPath).Append(log_path.BaseName());
41 #endif
42     log_path = log_path.ReplaceExtension(FILE_PATH_LITERAL("log"));
43     log_path = log_path.InsertBeforeExtension(FILE_PATH_LITERAL("_perf"));
44   }
45   ASSERT_TRUE(InitPerfLog(log_path));
46 
47   // Raise to high priority to have more precise measurements. Since we don't
48   // aim at 1% precision, it is not necessary to run at realtime level.
49   if (!debug::BeingDebugged())
50     RaiseProcessToHighPriority();
51 }
52 
InitializeFromCommandLine(int * argc,char ** argv)53 void PerfTestSuite::InitializeFromCommandLine(int* argc, char** argv) {
54   TestSuite::InitializeFromCommandLine(argc, argv);
55   ::benchmark::Initialize(argc, argv);
56 }
57 
RunAllTests()58 int PerfTestSuite::RunAllTests() {
59   const int result = TestSuite::RunAllTests();
60   ::benchmark::RunSpecifiedBenchmarks();
61   return result;
62 }
63 
Shutdown()64 void PerfTestSuite::Shutdown() {
65   TestSuite::Shutdown();
66   ::benchmark::Shutdown();
67   FinalizePerfLog();
68 }
69 
70 }  // namespace base
71