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