• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/allow_check_is_test_for_testing.h"
14 #include "base/test/perf_log.h"
15 #include "build/build_config.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/google_benchmark/src/include/benchmark/benchmark.h"
18 
19 #if BUILDFLAG(IS_FUCHSIA)
20 #include "base/fuchsia/file_utils.h"
21 #endif
22 
23 namespace base {
24 
PerfTestSuite(int argc,char ** argv)25 PerfTestSuite::PerfTestSuite(int argc, char** argv) : TestSuite(argc, argv) {}
26 
Initialize()27 void PerfTestSuite::Initialize() {
28   TestSuite::Initialize();
29 
30   test::AllowCheckIsTestForTesting();
31 
32   // Initialize the perf timer log
33   FilePath log_path =
34       CommandLine::ForCurrentProcess()->GetSwitchValuePath("log-file");
35   if (log_path.empty()) {
36     PathService::Get(FILE_EXE, &log_path);
37 #if BUILDFLAG(IS_ANDROID)
38     FilePath tmp_dir;
39     PathService::Get(DIR_CACHE, &tmp_dir);
40     log_path = tmp_dir.Append(log_path.BaseName());
41 #elif BUILDFLAG(IS_FUCHSIA)
42     log_path =
43         FilePath(kPersistedDataDirectoryPath).Append(log_path.BaseName());
44 #endif
45     log_path = log_path.ReplaceExtension(FILE_PATH_LITERAL("log"));
46     log_path = log_path.InsertBeforeExtension(FILE_PATH_LITERAL("_perf"));
47   }
48   ASSERT_TRUE(InitPerfLog(log_path));
49 
50   // Raise to high priority to have more precise measurements. Since we don't
51   // aim at 1% precision, it is not necessary to run at realtime level.
52   if (!debug::BeingDebugged())
53     RaiseProcessToHighPriority();
54 }
55 
InitializeFromCommandLine(int * argc,char ** argv)56 void PerfTestSuite::InitializeFromCommandLine(int* argc, char** argv) {
57   TestSuite::InitializeFromCommandLine(argc, argv);
58   ::benchmark::Initialize(argc, argv);
59 }
60 
RunAllTests()61 int PerfTestSuite::RunAllTests() {
62   const int result = TestSuite::RunAllTests();
63   ::benchmark::RunSpecifiedBenchmarks();
64   return result;
65 }
66 
Shutdown()67 void PerfTestSuite::Shutdown() {
68   TestSuite::Shutdown();
69   ::benchmark::Shutdown();
70   FinalizePerfLog();
71 }
72 
73 }  // namespace base
74