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/environment.h" | |
6 #include "base/file_util.h" | |
7 #include "base/path_service.h" | |
8 #include "base/stringprintf.h" | |
9 #include "base/string_number_conversions.h" | |
10 #include "base/string_split.h" | |
11 #include "base/string_util.h" | |
12 #include "base/sys_info.h" | |
13 #include "base/test/test_file_util.h" | |
14 #include "base/time.h" | |
15 #include "base/utf_string_conversions.h" | |
16 #include "chrome/common/chrome_constants.h" | |
17 #include "chrome/common/chrome_paths.h" | |
18 #include "chrome/common/chrome_switches.h" | |
19 #include "chrome/common/env_vars.h" | |
20 #include "chrome/test/automation/automation_proxy.h" | |
21 #include "chrome/test/base/test_switches.h" | |
22 #include "chrome/test/base/ui_test_utils.h" | |
23 #include "chrome/test/ui/ui_perf_test.h" | |
24 #include "net/base/net_util.h" | |
25 | |
26 using base::TimeDelta; | |
27 using base::TimeTicks; | |
28 | |
29 namespace { | |
30 | |
31 class StartupTest : public UIPerfTest { | |
32 public: | |
33 StartupTest() { | |
34 show_window_ = true; | |
35 } | |
36 void SetUp() {} | |
37 void TearDown() {} | |
38 | |
39 enum TestColdness { | |
40 WARM, | |
41 COLD | |
42 }; | |
43 | |
44 enum TestImportance { | |
45 NOT_IMPORTANT, | |
46 IMPORTANT | |
47 }; | |
48 | |
49 // Load a file on startup rather than about:blank. This tests a longer | |
50 // startup path, including resource loading. | |
51 void SetUpWithFileURL() { | |
52 const FilePath file_url = ui_test_utils::GetTestFilePath( | |
53 FilePath(FilePath::kCurrentDirectory), | |
54 FilePath(FILE_PATH_LITERAL("simple.html"))); | |
55 ASSERT_TRUE(file_util::PathExists(file_url)); | |
56 launch_arguments_.AppendArgPath(file_url); | |
57 } | |
58 | |
59 // Load a complex html file on startup represented by |which_tab|. | |
60 void SetUpWithComplexFileURL(unsigned int which_tab) { | |
61 const char* const kTestPageCyclerDomains[] = { | |
62 "www.google.com", "www.nytimes.com", | |
63 "www.yahoo.com", "espn.go.com", "www.amazon.com" | |
64 }; | |
65 unsigned int which_el = which_tab % arraysize(kTestPageCyclerDomains); | |
66 const char* this_domain = kTestPageCyclerDomains[which_el]; | |
67 | |
68 FilePath page_cycler_path; | |
69 PathService::Get(base::DIR_SOURCE_ROOT, &page_cycler_path); | |
70 page_cycler_path = page_cycler_path.AppendASCII("data") | |
71 .AppendASCII("page_cycler").AppendASCII("moz") | |
72 .AppendASCII(this_domain).AppendASCII("index.html"); | |
73 GURL file_url = net::FilePathToFileURL(page_cycler_path).Resolve("?skip"); | |
74 launch_arguments_.AppendArg(file_url.spec()); | |
75 } | |
76 | |
77 // Use the given profile in the test data extensions/profiles dir. This tests | |
78 // startup with extensions installed. | |
79 void SetUpWithExtensionsProfile(const char* profile) { | |
80 FilePath data_dir; | |
81 PathService::Get(chrome::DIR_TEST_DATA, &data_dir); | |
82 data_dir = data_dir.AppendASCII("extensions").AppendASCII("profiles"). | |
83 AppendASCII(profile); | |
84 set_template_user_data(data_dir); | |
85 } | |
86 | |
87 // Rewrite the preferences file to point to the proper image directory. | |
88 virtual void SetUpProfile() { | |
89 UIPerfTest::SetUpProfile(); | |
90 if (profile_type_ != UITestBase::COMPLEX_THEME) | |
91 return; | |
92 | |
93 const FilePath pref_template_path(user_data_dir().AppendASCII("Default"). | |
94 AppendASCII("PreferencesTemplate")); | |
95 const FilePath pref_path(user_data_dir().AppendASCII("Default"). | |
96 AppendASCII("Preferences")); | |
97 | |
98 // Read in preferences template. | |
99 std::string pref_string; | |
100 EXPECT_TRUE(file_util::ReadFileToString(pref_template_path, &pref_string)); | |
101 string16 format_string = ASCIIToUTF16(pref_string); | |
102 | |
103 // Make sure temp directory has the proper format for writing to prefs file. | |
104 #if defined(OS_POSIX) | |
105 std::wstring user_data_dir_w(ASCIIToWide(user_data_dir().value())); | |
106 #elif defined(OS_WIN) | |
107 std::wstring user_data_dir_w(user_data_dir().value()); | |
108 // In Windows, the FilePath will write '\' for the path separators; change | |
109 // these to a separator that won't trigger escapes. | |
110 std::replace(user_data_dir_w.begin(), | |
111 user_data_dir_w.end(), '\\', '/'); | |
112 #endif | |
113 | |
114 // Rewrite prefs file. | |
115 std::vector<string16> subst; | |
116 subst.push_back(WideToUTF16(user_data_dir_w)); | |
117 const std::string prefs_string = | |
118 UTF16ToASCII(ReplaceStringPlaceholders(format_string, subst, NULL)); | |
119 EXPECT_TRUE(file_util::WriteFile(pref_path, prefs_string.c_str(), | |
120 prefs_string.size())); | |
121 file_util::EvictFileFromSystemCache(pref_path); | |
122 } | |
123 | |
124 // Runs a test which loads |tab_count| tabs on startup, either as command line | |
125 // arguments or, if |restore_session| is true, by using session restore. | |
126 // |nth_timed_tab|, if non-zero, will measure time to load the first n+1 tabs. | |
127 void RunPerfTestWithManyTabs(const char* graph, const char* trace, | |
128 int tab_count, int nth_timed_tab, | |
129 bool restore_session); | |
130 | |
131 void RunStartupTest(const char* graph, const char* trace, | |
132 TestColdness test_cold, TestImportance test_importance, | |
133 UITestBase::ProfileType profile_type, | |
134 int num_tabs, int nth_timed_tab) { | |
135 bool important = (test_importance == IMPORTANT); | |
136 profile_type_ = profile_type; | |
137 | |
138 // Sets the profile data for the run. For now, this is only used for | |
139 // the non-default themes test. | |
140 if (profile_type != UITestBase::DEFAULT_THEME) { | |
141 set_template_user_data(UITest::ComputeTypicalUserDataSource( | |
142 profile_type)); | |
143 } | |
144 | |
145 const int kNumCyclesMax = 20; | |
146 int numCycles = kNumCyclesMax; | |
147 scoped_ptr<base::Environment> env(base::Environment::Create()); | |
148 std::string numCyclesEnv; | |
149 if (env->GetVar(env_vars::kStartupTestsNumCycles, &numCyclesEnv) && | |
150 base::StringToInt(numCyclesEnv, &numCycles)) { | |
151 if (numCycles <= kNumCyclesMax) { | |
152 VLOG(1) << env_vars::kStartupTestsNumCycles | |
153 << " set in environment, so setting numCycles to " << numCycles; | |
154 } else { | |
155 VLOG(1) << env_vars::kStartupTestsNumCycles | |
156 << " is higher than the max, setting numCycles to " | |
157 << kNumCyclesMax; | |
158 numCycles = kNumCyclesMax; | |
159 } | |
160 } | |
161 | |
162 struct TimingInfo { | |
163 TimeDelta end_to_end; | |
164 float first_start_ms; | |
165 float last_stop_ms; | |
166 float first_stop_ms; | |
167 float nth_tab_stop_ms; | |
168 }; | |
169 TimingInfo timings[kNumCyclesMax]; | |
170 | |
171 for (int i = 0; i < numCycles; ++i) { | |
172 if (test_cold == COLD) { | |
173 FilePath dir_app; | |
174 ASSERT_TRUE(PathService::Get(chrome::DIR_APP, &dir_app)); | |
175 | |
176 FilePath chrome_exe(dir_app.Append(GetExecutablePath())); | |
177 ASSERT_TRUE(EvictFileFromSystemCacheWrapper(chrome_exe)); | |
178 #if defined(OS_WIN) | |
179 // chrome.dll is windows specific. | |
180 FilePath chrome_dll(dir_app.Append(FILE_PATH_LITERAL("chrome.dll"))); | |
181 ASSERT_TRUE(EvictFileFromSystemCacheWrapper(chrome_dll)); | |
182 #endif | |
183 } | |
184 UITest::SetUp(); | |
185 TimeTicks end_time = TimeTicks::Now(); | |
186 | |
187 // HACK: Chrome < 5.0.368.0 did not yet implement SendJSONRequest. | |
188 { | |
189 std::string server_version = automation()->server_version(); | |
190 std::vector<std::string> version_numbers; | |
191 base::SplitString(server_version, '.', &version_numbers); | |
192 int chrome_buildnum = 0; | |
193 ASSERT_TRUE(base::StringToInt(version_numbers[2], &chrome_buildnum)); | |
194 if (chrome_buildnum < 368) { | |
195 num_tabs = 0; | |
196 } | |
197 } | |
198 if (num_tabs > 0) { | |
199 float min_start; | |
200 float max_stop; | |
201 std::vector<float> times; | |
202 scoped_refptr<BrowserProxy> browser_proxy( | |
203 automation()->GetBrowserWindow(0)); | |
204 ASSERT_TRUE(browser_proxy.get()); | |
205 | |
206 if (browser_proxy->GetInitialLoadTimes(&min_start, &max_stop, ×) && | |
207 !times.empty()) { | |
208 ASSERT_LT(nth_timed_tab, num_tabs); | |
209 ASSERT_EQ(times.size(), static_cast<size_t>(num_tabs)); | |
210 timings[i].first_start_ms = min_start; | |
211 timings[i].last_stop_ms = max_stop; | |
212 timings[i].first_stop_ms = times[0]; | |
213 timings[i].nth_tab_stop_ms = times[nth_timed_tab]; | |
214 } else { | |
215 // Browser might not support initial load times. | |
216 // Only use end-to-end time for this test. | |
217 num_tabs = 0; | |
218 } | |
219 } | |
220 timings[i].end_to_end = end_time - browser_launch_time(); | |
221 UITest::TearDown(); | |
222 | |
223 if (i == 0) { | |
224 // Re-use the profile data after first run so that the noise from | |
225 // creating databases doesn't impact all the runs. | |
226 clear_profile_ = false; | |
227 // Clear template_user_data_ so we don't try to copy it over each time | |
228 // through. | |
229 set_template_user_data(FilePath()); | |
230 } | |
231 } | |
232 | |
233 std::string times; | |
234 for (int i = 0; i < numCycles; ++i) { | |
235 base::StringAppendF(×, | |
236 "%.2f,", | |
237 timings[i].end_to_end.InMillisecondsF()); | |
238 } | |
239 PrintResultList(graph, "", trace, times, "ms", important); | |
240 | |
241 if (num_tabs > 0) { | |
242 std::string name_base = trace; | |
243 std::string name; | |
244 | |
245 times.clear(); | |
246 name = name_base + "-start"; | |
247 for (int i = 0; i < numCycles; ++i) | |
248 base::StringAppendF(×, "%.2f,", timings[i].first_start_ms); | |
249 PrintResultList(graph, "", name.c_str(), times, "ms", important); | |
250 | |
251 times.clear(); | |
252 name = name_base + "-first"; | |
253 for (int i = 0; i < numCycles; ++i) | |
254 base::StringAppendF(×, "%.2f,", timings[i].first_stop_ms); | |
255 PrintResultList(graph, "", name.c_str(), times, "ms", important); | |
256 | |
257 if (nth_timed_tab > 0) { | |
258 // Display only the time necessary to load the first n tabs. | |
259 times.clear(); | |
260 name = name_base + "-" + base::IntToString(nth_timed_tab); | |
261 for (int i = 0; i < numCycles; ++i) | |
262 base::StringAppendF(×, "%.2f,", timings[i].nth_tab_stop_ms); | |
263 PrintResultList(graph, "", name.c_str(), times, "ms", important); | |
264 } | |
265 | |
266 if (num_tabs > 1) { | |
267 // Display the time necessary to load all of the tabs. | |
268 times.clear(); | |
269 name = name_base + "-all"; | |
270 for (int i = 0; i < numCycles; ++i) | |
271 base::StringAppendF(×, "%.2f,", timings[i].last_stop_ms); | |
272 PrintResultList(graph, "", name.c_str(), times, "ms", important); | |
273 } | |
274 } | |
275 } | |
276 }; | |
277 | |
278 TEST_F(StartupTest, PerfWarm) { | |
279 RunStartupTest("warm", "t", WARM, IMPORTANT, | |
280 UITestBase::DEFAULT_THEME, 0, 0); | |
281 } | |
282 | |
283 TEST_F(StartupTest, PerfReferenceWarm) { | |
284 UseReferenceBuild(); | |
285 RunStartupTest("warm", "t_ref", WARM, IMPORTANT, | |
286 UITestBase::DEFAULT_THEME, 0, 0); | |
287 } | |
288 | |
289 // TODO(mpcomplete): Should we have reference timings for all these? | |
290 | |
291 TEST_F(StartupTest, PerfCold) { | |
292 RunStartupTest("cold", "t", COLD, NOT_IMPORTANT, | |
293 UITestBase::DEFAULT_THEME, 0, 0); | |
294 } | |
295 | |
296 void StartupTest::RunPerfTestWithManyTabs(const char* graph, const char* trace, | |
297 int tab_count, int nth_timed_tab, | |
298 bool restore_session) { | |
299 // Initialize session with |tab_count| tabs. | |
300 for (int i = 0; i < tab_count; ++i) | |
301 SetUpWithComplexFileURL(i); | |
302 | |
303 if (restore_session) { | |
304 // Start the browser with these urls so we can save the session and exit. | |
305 UITest::SetUp(); | |
306 // Set flags to ensure profile is saved and can be restored. | |
307 #if defined(OS_MACOSX) | |
308 set_shutdown_type(ProxyLauncher::USER_QUIT); | |
309 #endif | |
310 clear_profile_ = false; | |
311 // Quit and set flags to restore session. | |
312 UITest::TearDown(); | |
313 | |
314 // Clear all arguments for session restore, or the number of open tabs | |
315 // will grow with each restore. | |
316 CommandLine new_launch_arguments = CommandLine(CommandLine::NO_PROGRAM); | |
317 // Keep the branding switch if using a reference build. | |
318 if (launch_arguments_.HasSwitch(switches::kEnableChromiumBranding)) { | |
319 new_launch_arguments.AppendSwitch(switches::kEnableChromiumBranding); | |
320 } | |
321 // The session will be restored once per cycle for numCycles test cycles, | |
322 // and each time, UITest::SetUp will wait for |tab_count| tabs to | |
323 // finish loading. | |
324 new_launch_arguments.AppendSwitchASCII(switches::kRestoreLastSession, | |
325 base::IntToString(tab_count)); | |
326 launch_arguments_ = new_launch_arguments; | |
327 } | |
328 RunStartupTest(graph, trace, WARM, NOT_IMPORTANT, | |
329 UITestBase::DEFAULT_THEME, tab_count, nth_timed_tab); | |
330 } | |
331 | |
332 TEST_F(StartupTest, PerfFewTabs) { | |
333 RunPerfTestWithManyTabs("few_tabs", "cmdline", 5, 2, false); | |
334 } | |
335 | |
336 TEST_F(StartupTest, PerfFewTabsReference) { | |
337 UseReferenceBuild(); | |
338 RunPerfTestWithManyTabs("few_tabs", "cmdline-ref", 5, 2, false); | |
339 } | |
340 | |
341 TEST_F(StartupTest, PerfRestoreFewTabs) { | |
342 RunPerfTestWithManyTabs("few_tabs", "restore", 5, 2, true); | |
343 } | |
344 | |
345 TEST_F(StartupTest, PerfRestoreFewTabsReference) { | |
346 UseReferenceBuild(); | |
347 RunPerfTestWithManyTabs("few_tabs", "restore-ref", 5, 2, true); | |
348 } | |
349 | |
350 #if defined(OS_MACOSX) | |
351 // http://crbug.com/46609 | |
352 #define MAYBE_PerfSeveralTabsReference FLAKY_PerfSeveralTabsReference | |
353 #define MAYBE_PerfSeveralTabs FLAKY_PerfSeveralTabs | |
354 // http://crbug.com/52858 | |
355 #define MAYBE_PerfRestoreSeveralTabs FLAKY_PerfRestoreSeveralTabs | |
356 #define MAYBE_PerfExtensionContentScript50 FLAKY_PerfExtensionContentScript50 | |
357 #elif defined(OS_WIN) | |
358 // http://crbug.com/46609 | |
359 #define MAYBE_PerfSeveralTabs FLAKY_PerfSeveralTabs | |
360 #define MAYBE_PerfSeveralTabsReference PerfSeveralTabsReference | |
361 #define MAYBE_PerfRestoreSeveralTabs PerfRestoreSeveralTabs | |
362 #define MAYBE_PerfExtensionContentScript50 PerfExtensionContentScript50 | |
363 #else | |
364 #define MAYBE_PerfSeveralTabsReference PerfSeveralTabsReference | |
365 #define MAYBE_PerfSeveralTabs PerfSeveralTabs | |
366 #define MAYBE_PerfRestoreSeveralTabs PerfRestoreSeveralTabs | |
367 #define MAYBE_PerfExtensionContentScript50 PerfExtensionContentScript50 | |
368 #endif | |
369 | |
370 TEST_F(StartupTest, MAYBE_PerfSeveralTabs) { | |
371 RunPerfTestWithManyTabs("several_tabs", "cmdline", 10, 4, false); | |
372 } | |
373 | |
374 TEST_F(StartupTest, MAYBE_PerfSeveralTabsReference) { | |
375 UseReferenceBuild(); | |
376 RunPerfTestWithManyTabs("several_tabs", "cmdline-ref", 10, 4, false); | |
377 } | |
378 | |
379 TEST_F(StartupTest, MAYBE_PerfRestoreSeveralTabs) { | |
380 RunPerfTestWithManyTabs("several_tabs", "restore", 10, 4, true); | |
381 } | |
382 | |
383 TEST_F(StartupTest, PerfRestoreSeveralTabsReference) { | |
384 UseReferenceBuild(); | |
385 RunPerfTestWithManyTabs("several_tabs", "restore-ref", 10, 4, true); | |
386 } | |
387 | |
388 TEST_F(StartupTest, PerfExtensionEmpty) { | |
389 SetUpWithFileURL(); | |
390 SetUpWithExtensionsProfile("empty"); | |
391 RunStartupTest("warm", "extension_empty", WARM, NOT_IMPORTANT, | |
392 UITestBase::DEFAULT_THEME, 1, 0); | |
393 } | |
394 | |
395 TEST_F(StartupTest, PerfExtensionContentScript1) { | |
396 SetUpWithFileURL(); | |
397 SetUpWithExtensionsProfile("content_scripts1"); | |
398 RunStartupTest("warm", "extension_content_scripts1", WARM, NOT_IMPORTANT, | |
399 UITestBase::DEFAULT_THEME, 1, 0); | |
400 } | |
401 | |
402 TEST_F(StartupTest, MAYBE_PerfExtensionContentScript50) { | |
403 SetUpWithFileURL(); | |
404 SetUpWithExtensionsProfile("content_scripts50"); | |
405 RunStartupTest("warm", "extension_content_scripts50", WARM, NOT_IMPORTANT, | |
406 UITestBase::DEFAULT_THEME, 1, 0); | |
407 } | |
408 | |
409 TEST_F(StartupTest, PerfComplexTheme) { | |
410 RunStartupTest("warm", "t-theme", WARM, NOT_IMPORTANT, | |
411 UITestBase::COMPLEX_THEME, 0, 0); | |
412 } | |
413 | |
414 #if defined(TOOLKIT_USES_GTK) | |
415 TEST_F(StartupTest, PerfGtkTheme) { | |
416 RunStartupTest("warm", "gtk-theme", WARM, NOT_IMPORTANT, | |
417 UITestBase::NATIVE_THEME, 0, 0); | |
418 } | |
419 | |
420 TEST_F(StartupTest, PrefNativeFrame) { | |
421 RunStartupTest("warm", "custom-frame", WARM, NOT_IMPORTANT, | |
422 UITestBase::CUSTOM_FRAME, 0, 0); | |
423 } | |
424 | |
425 TEST_F(StartupTest, PerfNativeFrameGtkTheme) { | |
426 RunStartupTest("warm", "custom-frame-gtk-theme", WARM, NOT_IMPORTANT, | |
427 UITestBase::CUSTOM_FRAME_NATIVE_THEME, 0, 0); | |
428 } | |
429 #endif | |
430 | |
431 } // namespace | |
OLD | NEW |