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