OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/test/perf_log.h" | 5 #include "base/test/perf_log.h" |
6 | 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/files/file_path.h" |
7 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
8 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/path_service.h" |
| 12 #include "build/build_config.h" |
9 | 13 |
10 namespace base { | 14 namespace base { |
11 | 15 |
12 static FILE* perf_log_file = NULL; | 16 static FILE* perf_log_file = NULL; |
13 | 17 |
14 bool InitPerfLog(const FilePath& log_file) { | 18 bool InitPerfLog() { |
15 if (perf_log_file) { | 19 if (perf_log_file) { |
16 // trying to initialize twice | 20 // trying to initialize twice |
17 NOTREACHED(); | 21 NOTREACHED(); |
18 return false; | 22 return false; |
19 } | 23 } |
20 | 24 |
21 perf_log_file = OpenFile(log_file, "w"); | 25 FilePath log_path = |
| 26 CommandLine::ForCurrentProcess()->GetSwitchValuePath("log-file"); |
| 27 if (log_path.empty()) { |
| 28 PathService::Get(FILE_EXE, &log_path); |
| 29 #if defined(OS_ANDROID) |
| 30 base::FilePath tmp_dir; |
| 31 PathService::Get(base::DIR_CACHE, &tmp_dir); |
| 32 log_path = tmp_dir.Append(log_path.BaseName()); |
| 33 #endif |
| 34 log_path = log_path.ReplaceExtension(FILE_PATH_LITERAL("log")); |
| 35 log_path = log_path.InsertBeforeExtension(FILE_PATH_LITERAL("_perf")); |
| 36 } |
| 37 |
| 38 perf_log_file = OpenFile(log_path, "w"); |
22 return perf_log_file != NULL; | 39 return perf_log_file != NULL; |
23 } | 40 } |
24 | 41 |
25 void FinalizePerfLog() { | 42 void FinalizePerfLog() { |
26 if (!perf_log_file) { | 43 if (!perf_log_file) { |
27 // trying to cleanup without initializing | 44 // trying to cleanup without initializing |
28 NOTREACHED(); | 45 NOTREACHED(); |
29 return; | 46 return; |
30 } | 47 } |
31 base::CloseFile(perf_log_file); | 48 base::CloseFile(perf_log_file); |
32 } | 49 } |
33 | 50 |
34 void LogPerfResult(const char* test_name, double value, const char* units) { | 51 void LogPerfResult(const char* test_name, double value, const char* units) { |
35 if (!perf_log_file) { | 52 if (!perf_log_file) { |
36 NOTREACHED(); | 53 NOTREACHED(); |
37 return; | 54 return; |
38 } | 55 } |
39 | 56 |
40 fprintf(perf_log_file, "%s\t%g\t%s\n", test_name, value, units); | 57 fprintf(perf_log_file, "%s\t%g\t%s\n", test_name, value, units); |
41 printf("%s\t%g\t%s\n", test_name, value, units); | 58 printf("%s\t%g\t%s\n", test_name, value, units); |
42 fflush(stdout); | 59 fflush(stdout); |
43 } | 60 } |
44 | 61 |
45 } // namespace base | 62 } // namespace base |
OLD | NEW |