• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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"
27 
28 using base::TimeDelta;
29 
30 namespace {
31 
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:
TabSwitchingUITest()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");
43 
44     show_window_ = true;
45   }
46 
SetUp()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");
50 
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, WideToUTF8(log_file_name_.value()));
55 #else
56     env->SetVar(env_vars::kLogFileName, log_file_name_.value());
57 #endif
58 
59     // Add the necessary arguments to Chrome's launch command for these tests.
60     AddLaunchArguments();
61 
62     // Run the rest of the UITest initialization.
63     UITest::SetUp();
64   }
65 
66   static const int kNumCycles = 5;
67 
PrintTimings(const char * label,TimeDelta timings[kNumCycles],bool important)68   void PrintTimings(const char* label, TimeDelta timings[kNumCycles],
69     bool important) {
70       std::string times;
71       for (int i = 0; i < kNumCycles; ++i)
72         base::StringAppendF(&times, "%.2f,", timings[i].InMillisecondsF());
73       perf_test::PrintResultList(
74           "times", std::string(), label, times, "ms", important);
75   }
76 
RunTabSwitchingUITest(const char * label,bool important)77   void RunTabSwitchingUITest(const char* label, bool important) {
78     // Shut down from window UITest sets up automatically.
79     UITest::TearDown();
80 
81     TimeDelta timings[kNumCycles];
82     for (int i = 0; i < kNumCycles; ++i) {
83       // Prepare for this test run.
84       SetUp();
85 
86       // Create a browser proxy.
87       browser_proxy_ = automation()->GetBrowserWindow(0);
88 
89       // Open all the tabs.
90       int initial_tab_count = 0;
91       ASSERT_TRUE(browser_proxy_->GetTabCount(&initial_tab_count));
92       int new_tab_count = OpenTabs();
93       int tab_count = -1;
94       ASSERT_TRUE(browser_proxy_->GetTabCount(&tab_count));
95       ASSERT_EQ(initial_tab_count + new_tab_count, tab_count);
96 
97       // Switch linearly between tabs.
98       ASSERT_TRUE(browser_proxy_->ActivateTab(0));
99       int final_tab_count = 0;
100       ASSERT_TRUE(browser_proxy_->GetTabCount(&final_tab_count));
101       for (int j = initial_tab_count; j < final_tab_count; ++j) {
102         ASSERT_TRUE(browser_proxy_->ActivateTab(j));
103         ASSERT_TRUE(browser_proxy_->WaitForTabToBecomeActive(
104             j, base::TimeDelta::FromSeconds(10)));
105       }
106 
107       // Close the browser to force a dump of log.
108       bool application_closed = false;
109       EXPECT_TRUE(CloseBrowser(browser_proxy_.get(), &application_closed));
110 
111       // Open the corresponding log file and collect average from the
112       // histogram stats generated for RenderWidgetHost_TabSwitchPaintDuration.
113       bool log_has_been_dumped = false;
114       std::string contents;
115       int max_tries = 20;
116       do {
117         log_has_been_dumped = base::ReadFileToString(log_file_name_, &contents);
118         if (!log_has_been_dumped)
119           base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100));
120       } while (!log_has_been_dumped && max_tries--);
121       ASSERT_TRUE(log_has_been_dumped) << "Failed to read the log file";
122 
123       // Parse the contents to get average.
124       int64 average = 0;
125       const std::string average_str("average = ");
126       std::string::size_type pos = contents.find(
127           "Histogram: MPArch.RWH_TabSwitchPaintDuration", 0);
128       std::string::size_type comma_pos;
129       std::string::size_type number_length;
130 
131       ASSERT_NE(pos, std::string::npos) <<
132         "Histogram: MPArch.RWH_TabSwitchPaintDuration wasn't found\n" <<
133         contents;
134 
135       // Get the average.
136       pos = contents.find(average_str, pos);
137       comma_pos = contents.find(",", pos);
138       pos += average_str.length();
139       number_length = comma_pos - pos;
140       average = atoi(contents.substr(pos, number_length).c_str());
141 
142       // Print the average and standard deviation.
143       timings[i] = TimeDelta::FromMilliseconds(average);
144 
145       // Clean up from the test run.
146       UITest::TearDown();
147     }
148     PrintTimings(label, timings, important);
149   }
150 
151  protected:
152   // Opens new tabs. Returns the number of tabs opened.
OpenTabs()153   int OpenTabs() {
154     // Add tabs.
155     static const char* files[] = { "espn.go.com", "bugzilla.mozilla.org",
156                                    "news.cnet.com", "www.amazon.com",
157                                    "kannada.chakradeo.net", "allegro.pl",
158                                    "ml.wikipedia.org", "www.bbc.co.uk",
159                                    "126.com", "www.altavista.com"};
160     int number_of_new_tabs_opened = 0;
161     base::FilePath file_name;
162     for (size_t i = 0; i < arraysize(files); ++i) {
163       file_name = path_prefix_;
164       file_name = file_name.AppendASCII(files[i]);
165       file_name = file_name.AppendASCII("index.html");
166       bool success =
167           browser_proxy_->AppendTab(net::FilePathToFileURL(file_name));
168       EXPECT_TRUE(success);
169       if (success)
170         number_of_new_tabs_opened++;
171     }
172 
173     return number_of_new_tabs_opened;
174   }
175 
176   base::FilePath path_prefix_;
177   base::FilePath log_file_name_;
178   scoped_refptr<BrowserProxy> browser_proxy_;
179 
180  private:
AddLaunchArguments()181   void AddLaunchArguments() {
182     launch_arguments_.AppendSwitch(switches::kEnableLogging);
183     launch_arguments_.AppendSwitchASCII(switches::kLoggingLevel, "0");
184   }
185 
186   DISALLOW_COPY_AND_ASSIGN(TabSwitchingUITest);
187 };
188 
189 // This is failing, and taking forever to finish when doing so.
190 // http://crbug.com/102162
191 
TEST_F(TabSwitchingUITest,DISABLED_TabSwitch)192 TEST_F(TabSwitchingUITest, DISABLED_TabSwitch) {
193   RunTabSwitchingUITest("t", true);
194 }
195 
196 // Started failing with a webkit roll in r49936. See http://crbug.com/46751
TEST_F(TabSwitchingUITest,DISABLED_TabSwitchRef)197 TEST_F(TabSwitchingUITest, DISABLED_TabSwitchRef) {
198   UseReferenceBuild();
199   RunTabSwitchingUITest("t_ref", true);
200 }
201 
202 }  // namespace
203