| 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_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/ui/javascript_test_util.h" | |
| 15 #include "chrome/test/ui/ui_perf_test.h" | |
| 16 #include "content/common/json_value_serializer.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::huge_test_timeout_ms(), "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 PrintResultMeanAndError("total", "", trace_name, total, "ms", true); | |
| 109 | |
| 110 ResultsMap::const_iterator it = results.begin(); | |
| 111 for (; it != results.end(); ++it) | |
| 112 PrintResultList(it->first, "", trace_name, it->second, "ms", false); | |
| 113 } | |
| 114 | |
| 115 DISALLOW_COPY_AND_ASSIGN(SunSpiderTest); | |
| 116 }; | |
| 117 | |
| 118 class SunSpiderReferenceTest : public SunSpiderTest { | |
| 119 public: | |
| 120 SunSpiderReferenceTest() : SunSpiderTest() { | |
| 121 reference_ = true; | |
| 122 } | |
| 123 | |
| 124 void SetUp() { | |
| 125 UseReferenceBuild(); | |
| 126 SunSpiderTest::SetUp(); | |
| 127 } | |
| 128 }; | |
| 129 | |
| 130 TEST_F(SunSpiderTest, Perf) { | |
| 131 if (!CommandLine::ForCurrentProcess()->HasSwitch(kRunSunSpider)) | |
| 132 return; | |
| 133 | |
| 134 RunTest(); | |
| 135 } | |
| 136 | |
| 137 TEST_F(SunSpiderReferenceTest, Perf) { | |
| 138 if (!CommandLine::ForCurrentProcess()->HasSwitch(kRunSunSpider)) | |
| 139 return; | |
| 140 | |
| 141 RunTest(); | |
| 142 } | |
| 143 | |
| 144 } // namespace | |
| OLD | NEW |