OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
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/command_line.h" | |
6 #include "base/file_path.h" | |
7 #include "base/file_util.h" | |
8 #include "base/path_service.h" | |
9 #include "base/string_util.h" | |
10 #include "base/test/test_timeouts.h" | |
11 #include "base/utf_string_conversions.h" | |
12 #include "base/values.h" | |
13 #include "chrome/common/chrome_paths.h" | |
14 #include "chrome/test/automation/tab_proxy.h" | |
15 #include "chrome/test/ui/javascript_test_util.h" | |
16 #include "chrome/test/ui/ui_perf_test.h" | |
17 #include "content/common/json_value_serializer.h" | |
18 #include "googleurl/src/gurl.h" | |
19 #include "net/base/net_util.h" | |
20 | |
21 namespace { | |
22 | |
23 static const FilePath::CharType kStartFile[] = | |
24 FILE_PATH_LITERAL("run.html"); | |
25 | |
26 const char kRunV8Benchmark[] = "run-v8-benchmark"; | |
27 | |
28 class V8BenchmarkTest : public UIPerfTest { | |
29 public: | |
30 typedef std::map<std::string, std::string> ResultsMap; | |
31 | |
32 V8BenchmarkTest() : reference_(false) { | |
33 dom_automation_enabled_ = true; | |
34 show_window_ = true; | |
35 } | |
36 | |
37 void RunTest() { | |
38 FilePath::StringType start_file(kStartFile); | |
39 FilePath test_path = GetV8BenchmarkDir(); | |
40 test_path = test_path.Append(start_file); | |
41 GURL test_url(net::FilePathToFileURL(test_path)); | |
42 | |
43 scoped_refptr<TabProxy> tab(GetActiveTab()); | |
44 ASSERT_TRUE(tab.get()); | |
45 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, tab->NavigateToURL(test_url)); | |
46 | |
47 // Wait for the test to finish. | |
48 ASSERT_TRUE(WaitUntilTestCompletes(tab.get(), test_url)); | |
49 | |
50 PrintResults(tab.get()); | |
51 } | |
52 | |
53 protected: | |
54 bool reference_; // True if this is a reference build. | |
55 | |
56 private: | |
57 // Return the path to the V8 benchmark directory on the local filesystem. | |
58 FilePath GetV8BenchmarkDir() { | |
59 FilePath test_dir; | |
60 PathService::Get(chrome::DIR_TEST_DATA, &test_dir); | |
61 return test_dir.AppendASCII("v8_benchmark"); | |
62 } | |
63 | |
64 bool WaitUntilTestCompletes(TabProxy* tab, const GURL& test_url) { | |
65 return WaitUntilCookieValue(tab, test_url, "__done", | |
66 TestTimeouts::huge_test_timeout_ms(), "1"); | |
67 } | |
68 | |
69 bool GetScore(TabProxy* tab, std::string* score) { | |
70 std::wstring score_wide; | |
71 bool succeeded = tab->ExecuteAndExtractString(L"", | |
72 L"window.domAutomationController.send(automation.GetScore());", | |
73 &score_wide); | |
74 | |
75 // Note that we don't use ASSERT_TRUE here (and in some other places) as it | |
76 // doesn't work inside a function with a return type other than void. | |
77 EXPECT_TRUE(succeeded); | |
78 if (!succeeded) | |
79 return false; | |
80 | |
81 score->assign(WideToUTF8(score_wide)); | |
82 return true; | |
83 } | |
84 | |
85 bool GetResults(TabProxy* tab, ResultsMap* results) { | |
86 std::wstring json_wide; | |
87 bool succeeded = tab->ExecuteAndExtractString(L"", | |
88 L"window.domAutomationController.send(" | |
89 L" JSON.stringify(automation.GetResults()));", | |
90 &json_wide); | |
91 | |
92 EXPECT_TRUE(succeeded); | |
93 if (!succeeded) | |
94 return false; | |
95 | |
96 std::string json = WideToUTF8(json_wide); | |
97 return JsonDictionaryToMap(json, results); | |
98 } | |
99 | |
100 void PrintResults(TabProxy* tab) { | |
101 std::string score; | |
102 ASSERT_TRUE(GetScore(tab, &score)); | |
103 | |
104 ResultsMap results; | |
105 ASSERT_TRUE(GetResults(tab, &results)); | |
106 | |
107 std::string trace_name = reference_ ? "score_ref" : "score"; | |
108 std::string unit_name = "score (bigger is better)"; | |
109 | |
110 PrintResult("score", "", trace_name, score, unit_name, true); | |
111 | |
112 ResultsMap::const_iterator it = results.begin(); | |
113 for (; it != results.end(); ++it) | |
114 PrintResult(it->first, "", trace_name, it->second, unit_name, false); | |
115 } | |
116 | |
117 DISALLOW_COPY_AND_ASSIGN(V8BenchmarkTest); | |
118 }; | |
119 | |
120 class V8BenchmarkReferenceTest : public V8BenchmarkTest { | |
121 public: | |
122 V8BenchmarkReferenceTest() : V8BenchmarkTest() { | |
123 reference_ = true; | |
124 } | |
125 | |
126 void SetUp() { | |
127 UseReferenceBuild(); | |
128 V8BenchmarkTest::SetUp(); | |
129 } | |
130 }; | |
131 | |
132 TEST_F(V8BenchmarkTest, Perf) { | |
133 if (!CommandLine::ForCurrentProcess()->HasSwitch(kRunV8Benchmark)) | |
134 return; | |
135 | |
136 RunTest(); | |
137 } | |
138 | |
139 // Bug 87162: Disable Perf for ChromeOS | |
140 #if defined(OS_CHROMEOS) | |
141 #define MAYBE_Perf DISABLED_Perf | |
142 #else | |
143 #define MAYBE_Perf Perf | |
144 #endif | |
145 | |
146 TEST_F(V8BenchmarkReferenceTest, MAYBE_Perf) { | |
147 if (!CommandLine::ForCurrentProcess()->HasSwitch(kRunV8Benchmark)) | |
148 return; | |
149 | |
150 RunTest(); | |
151 } | |
152 | |
153 } // namespace | |
OLD | NEW |