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/environment.h" | |
7 #include "base/file_path.h" | |
8 #include "base/file_util.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/path_service.h" | |
11 #include "base/threading/platform_thread.h" | |
12 #include "base/time.h" | |
13 #include "base/utf_string_conversions.h" | |
14 #include "chrome/app/chrome_command_ids.h" | |
15 #include "chrome/common/chrome_paths.h" | |
16 #include "chrome/common/chrome_switches.h" | |
17 #include "chrome/common/env_vars.h" | |
18 #include "chrome/test/automation/automation_proxy.h" | |
19 #include "chrome/test/automation/browser_proxy.h" | |
20 #include "chrome/test/automation/tab_proxy.h" | |
21 #include "chrome/test/ui/ui_perf_test.h" | |
22 #include "googleurl/src/gurl.h" | |
23 #include "net/base/net_util.h" | |
24 | |
25 using base::TimeDelta; | |
26 | |
27 namespace { | |
28 | |
29 // This Automated UI test opens static files in different tabs in a proxy | |
30 // browser. After all the tabs have opened, it switches between tabs, and notes | |
31 // time taken for each switch. It then prints out the times on the console, | |
32 // with the aim that the page cycler parser can interpret these numbers to | |
33 // draw graphs for page cycler Tab Switching Performance. | |
34 class TabSwitchingUITest : public UIPerfTest { | |
35 public: | |
36 TabSwitchingUITest() { | |
37 PathService::Get(base::DIR_SOURCE_ROOT, &path_prefix_); | |
38 path_prefix_ = path_prefix_.AppendASCII("data"); | |
39 path_prefix_ = path_prefix_.AppendASCII("tab_switching"); | |
40 | |
41 show_window_ = true; | |
42 } | |
43 | |
44 void SetUp() { | |
45 // Set the log_file_name_ path according to the selected browser_directory_. | |
46 log_file_name_ = browser_directory_.AppendASCII("chrome_debug.log"); | |
47 | |
48 // Set the log file path for the browser test. | |
49 scoped_ptr<base::Environment> env(base::Environment::Create()); | |
50 #if defined(OS_WIN) | |
51 env->SetVar(env_vars::kLogFileName, WideToUTF8(log_file_name_.value())); | |
52 #else | |
53 env->SetVar(env_vars::kLogFileName, log_file_name_.value()); | |
54 #endif | |
55 | |
56 // Add the necessary arguments to Chrome's launch command for these tests. | |
57 AddLaunchArguments(); | |
58 | |
59 // Run the rest of the UITest initialization. | |
60 UITest::SetUp(); | |
61 } | |
62 | |
63 static const int kNumCycles = 5; | |
64 | |
65 void PrintTimings(const char* label, TimeDelta timings[kNumCycles], | |
66 bool important) { | |
67 std::string times; | |
68 for (int i = 0; i < kNumCycles; ++i) | |
69 base::StringAppendF(×, "%.2f,", timings[i].InMillisecondsF()); | |
70 PrintResultList("times", "", label, times, "ms", important); | |
71 } | |
72 | |
73 void RunTabSwitchingUITest(const char* label, bool important) { | |
74 // Shut down from window UITest sets up automatically. | |
75 UITest::TearDown(); | |
76 | |
77 TimeDelta timings[kNumCycles]; | |
78 for (int i = 0; i < kNumCycles; ++i) { | |
79 // Prepare for this test run. | |
80 SetUp(); | |
81 | |
82 // Create a browser proxy. | |
83 browser_proxy_ = automation()->GetBrowserWindow(0); | |
84 | |
85 // Open all the tabs. | |
86 int initial_tab_count = 0; | |
87 ASSERT_TRUE(browser_proxy_->GetTabCount(&initial_tab_count)); | |
88 int new_tab_count = OpenTabs(); | |
89 int tab_count = -1; | |
90 ASSERT_TRUE(browser_proxy_->GetTabCount(&tab_count)); | |
91 ASSERT_EQ(initial_tab_count + new_tab_count, tab_count); | |
92 | |
93 // Switch linearly between tabs. | |
94 ASSERT_TRUE(browser_proxy_->ActivateTab(0)); | |
95 int final_tab_count = 0; | |
96 ASSERT_TRUE(browser_proxy_->GetTabCount(&final_tab_count)); | |
97 for (int j = initial_tab_count; j < final_tab_count; ++j) { | |
98 ASSERT_TRUE(browser_proxy_->ActivateTab(j)); | |
99 ASSERT_TRUE(browser_proxy_->WaitForTabToBecomeActive(j, 10000)); | |
100 } | |
101 | |
102 // Close the browser to force a dump of log. | |
103 bool application_closed = false; | |
104 EXPECT_TRUE(CloseBrowser(browser_proxy_.get(), &application_closed)); | |
105 | |
106 // Open the corresponding log file and collect average from the | |
107 // histogram stats generated for RenderWidgetHost_TabSwitchPaintDuration. | |
108 bool log_has_been_dumped = false; | |
109 std::string contents; | |
110 int max_tries = 20; | |
111 do { | |
112 log_has_been_dumped = file_util::ReadFileToString(log_file_name_, | |
113 &contents); | |
114 if (!log_has_been_dumped) | |
115 base::PlatformThread::Sleep(100); | |
116 } while (!log_has_been_dumped && max_tries--); | |
117 ASSERT_TRUE(log_has_been_dumped) << "Failed to read the log file"; | |
118 | |
119 // Parse the contents to get average. | |
120 int64 average = 0; | |
121 const std::string average_str("average = "); | |
122 std::string::size_type pos = contents.find( | |
123 "Histogram: MPArch.RWH_TabSwitchPaintDuration", 0); | |
124 std::string::size_type comma_pos; | |
125 std::string::size_type number_length; | |
126 | |
127 ASSERT_NE(pos, std::string::npos) << | |
128 "Histogram: MPArch.RWH_TabSwitchPaintDuration wasn't found\n" << | |
129 contents; | |
130 | |
131 // Get the average. | |
132 pos = contents.find(average_str, pos); | |
133 comma_pos = contents.find(",", pos); | |
134 pos += average_str.length(); | |
135 number_length = comma_pos - pos; | |
136 average = atoi(contents.substr(pos, number_length).c_str()); | |
137 | |
138 // Print the average and standard deviation. | |
139 timings[i] = TimeDelta::FromMilliseconds(average); | |
140 | |
141 // Clean up from the test run. | |
142 UITest::TearDown(); | |
143 } | |
144 PrintTimings(label, timings, important); | |
145 } | |
146 | |
147 protected: | |
148 // Opens new tabs. Returns the number of tabs opened. | |
149 int OpenTabs() { | |
150 // Add tabs. | |
151 static const char* files[] = { "espn.go.com", "bugzilla.mozilla.org", | |
152 "news.cnet.com", "www.amazon.com", | |
153 "kannada.chakradeo.net", "allegro.pl", | |
154 "ml.wikipedia.org", "www.bbc.co.uk", | |
155 "126.com", "www.altavista.com"}; | |
156 int number_of_new_tabs_opened = 0; | |
157 FilePath file_name; | |
158 for (size_t i = 0; i < arraysize(files); ++i) { | |
159 file_name = path_prefix_; | |
160 file_name = file_name.AppendASCII(files[i]); | |
161 file_name = file_name.AppendASCII("index.html"); | |
162 bool success = | |
163 browser_proxy_->AppendTab(net::FilePathToFileURL(file_name)); | |
164 EXPECT_TRUE(success); | |
165 if (success) | |
166 number_of_new_tabs_opened++; | |
167 } | |
168 | |
169 return number_of_new_tabs_opened; | |
170 } | |
171 | |
172 FilePath path_prefix_; | |
173 FilePath log_file_name_; | |
174 scoped_refptr<BrowserProxy> browser_proxy_; | |
175 | |
176 private: | |
177 void AddLaunchArguments() { | |
178 launch_arguments_.AppendSwitch(switches::kEnableLogging); | |
179 launch_arguments_.AppendSwitch(switches::kDumpHistogramsOnExit); | |
180 launch_arguments_.AppendSwitchASCII(switches::kLoggingLevel, "0"); | |
181 } | |
182 | |
183 DISALLOW_COPY_AND_ASSIGN(TabSwitchingUITest); | |
184 }; | |
185 | |
186 TEST_F(TabSwitchingUITest, TabSwitch) { | |
187 RunTabSwitchingUITest("t", true); | |
188 } | |
189 | |
190 TEST_F(TabSwitchingUITest, TabSwitchRef) { | |
191 UseReferenceBuild(); | |
192 RunTabSwitchingUITest("t_ref", true); | |
193 } | |
194 | |
195 } // namespace | |
OLD | NEW |