1 // Copyright 2013 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/basictypes.h"
6 #include "base/command_line.h"
7 #if defined(OS_MACOSX)
8 #include "base/mac/mac_util.h"
9 #endif
10 #include "base/strings/stringprintf.h"
11 #include "base/test/trace_event_analyzer.h"
12 #include "base/win/windows_version.h"
13 #include "chrome/browser/extensions/extension_apitest.h"
14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/extensions/extension_test_message_listener.h"
16 #include "chrome/browser/extensions/tab_helper.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/ui/fullscreen/fullscreen_controller.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/chrome_version_info.h"
21 #include "chrome/test/base/test_launcher_utils.h"
22 #include "chrome/test/base/test_switches.h"
23 #include "chrome/test/base/tracing.h"
24 #include "content/public/browser/render_process_host.h"
25 #include "content/public/browser/render_view_host.h"
26 #include "content/public/common/content_switches.h"
27 #include "extensions/common/feature_switch.h"
28 #include "extensions/common/features/base_feature_provider.h"
29 #include "extensions/common/features/complex_feature.h"
30 #include "extensions/common/features/feature.h"
31 #include "extensions/common/features/simple_feature.h"
32 #include "extensions/common/switches.h"
33 #include "testing/gtest/include/gtest/gtest.h"
34 #include "testing/perf/perf_test.h"
35 #include "ui/compositor/compositor_switches.h"
36 #include "ui/gl/gl_switches.h"
37
38 namespace {
39
40 const char kExtensionId[] = "ddchlicdkolnonkihahngkmmmjnjlkkf";
41
42 enum TestFlags {
43 kUseGpu = 1 << 0, // Only execute test if --enable-gpu was given
44 // on the command line. This is required for
45 // tests that run on GPU.
46 kForceGpuComposited = 1 << 1, // Force the test to use the compositor.
47 kDisableVsync = 1 << 2, // Do not limit framerate to vertical refresh.
48 // when on GPU, nor to 60hz when not on GPU.
49 kTestThroughWebRTC = 1 << 3, // Send captured frames through webrtc
50 kSmallWindow = 1 << 4, // 1 = 800x600, 0 = 2000x1000
51
52 kScaleQualityMask = 3 << 5, // two bits select which scaling quality
53 kScaleQualityDefault = 0 << 5, // to use on aura.
54 kScaleQualityFast = 1 << 5,
55 kScaleQualityGood = 2 << 5,
56 kScaleQualityBest = 3 << 5,
57 };
58
59 class TabCapturePerformanceTest
60 : public ExtensionApiTest,
61 public testing::WithParamInterface<int> {
62 public:
TabCapturePerformanceTest()63 TabCapturePerformanceTest() {}
64
HasFlag(TestFlags flag) const65 bool HasFlag(TestFlags flag) const {
66 return (GetParam() & flag) == flag;
67 }
68
IsGpuAvailable() const69 bool IsGpuAvailable() const {
70 return CommandLine::ForCurrentProcess()->HasSwitch("enable-gpu");
71 }
72
ScalingMethod() const73 std::string ScalingMethod() const {
74 switch (GetParam() & kScaleQualityMask) {
75 case kScaleQualityFast:
76 return "fast";
77 case kScaleQualityGood:
78 return "good";
79 case kScaleQualityBest:
80 return "best";
81 default:
82 return "";
83 }
84 }
85
GetSuffixForTestFlags()86 std::string GetSuffixForTestFlags() {
87 std::string suffix;
88 if (HasFlag(kForceGpuComposited))
89 suffix += "_comp";
90 if (HasFlag(kUseGpu))
91 suffix += "_gpu";
92 if (HasFlag(kDisableVsync))
93 suffix += "_novsync";
94 if (HasFlag(kTestThroughWebRTC))
95 suffix += "_webrtc";
96 if (!ScalingMethod().empty())
97 suffix += "_scale" + ScalingMethod();
98 if (HasFlag(kSmallWindow))
99 suffix += "_small";
100 return suffix;
101 }
102
SetUp()103 virtual void SetUp() OVERRIDE {
104 EnablePixelOutput();
105 ExtensionApiTest::SetUp();
106 }
107
SetUpCommandLine(CommandLine * command_line)108 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
109 if (!ScalingMethod().empty()) {
110 command_line->AppendSwitchASCII(switches::kTabCaptureUpscaleQuality,
111 ScalingMethod());
112 command_line->AppendSwitchASCII(switches::kTabCaptureDownscaleQuality,
113 ScalingMethod());
114 }
115
116 // Some of the tests may launch http requests through JSON or AJAX
117 // which causes a security error (cross domain request) when the page
118 // is loaded from the local file system ( file:// ). The following switch
119 // fixes that error.
120 command_line->AppendSwitch(switches::kAllowFileAccessFromFiles);
121
122 if (HasFlag(kSmallWindow)) {
123 command_line->AppendSwitchASCII(switches::kWindowSize, "800,600");
124 } else {
125 command_line->AppendSwitchASCII(switches::kWindowSize, "2000,1500");
126 }
127
128 if (!HasFlag(kUseGpu))
129 command_line->AppendSwitch(switches::kDisableGpu);
130
131 if (HasFlag(kDisableVsync))
132 command_line->AppendSwitch(switches::kDisableGpuVsync);
133
134 command_line->AppendSwitchASCII(
135 extensions::switches::kWhitelistedExtensionID,
136 kExtensionId);
137 ExtensionApiTest::SetUpCommandLine(command_line);
138 }
139
PrintResults(trace_analyzer::TraceAnalyzer * analyzer,const std::string & test_name,const std::string & event_name,const std::string & unit)140 bool PrintResults(trace_analyzer::TraceAnalyzer *analyzer,
141 const std::string& test_name,
142 const std::string& event_name,
143 const std::string& unit) {
144 trace_analyzer::TraceEventVector events;
145 trace_analyzer::Query query =
146 trace_analyzer::Query::EventNameIs(event_name) &&
147 (trace_analyzer::Query::EventPhaseIs(TRACE_EVENT_PHASE_BEGIN) ||
148 trace_analyzer::Query::EventPhaseIs(TRACE_EVENT_PHASE_COMPLETE) ||
149 trace_analyzer::Query::EventPhaseIs(TRACE_EVENT_PHASE_ASYNC_BEGIN) ||
150 trace_analyzer::Query::EventPhaseIs(TRACE_EVENT_PHASE_FLOW_BEGIN) ||
151 trace_analyzer::Query::EventPhaseIs(TRACE_EVENT_PHASE_INSTANT));
152 analyzer->FindEvents(query, &events);
153 if (events.size() < 20) {
154 LOG(ERROR) << "Not enough events of type " << event_name << " found ("
155 << events.size() << ").";
156 return false;
157 }
158
159 // Ignore some events for startup/setup/caching.
160 trace_analyzer::TraceEventVector rate_events(events.begin() + 3,
161 events.end() - 3);
162 trace_analyzer::RateStats stats;
163 if (!GetRateStats(rate_events, &stats, NULL)) {
164 LOG(ERROR) << "GetRateStats failed";
165 return false;
166 }
167 double mean_ms = stats.mean_us / 1000.0;
168 double std_dev_ms = stats.standard_deviation_us / 1000.0;
169 std::string mean_and_error = base::StringPrintf("%f,%f", mean_ms,
170 std_dev_ms);
171 perf_test::PrintResultMeanAndError(test_name,
172 GetSuffixForTestFlags(),
173 event_name,
174 mean_and_error,
175 unit,
176 true);
177 return true;
178 }
179
RunTest(const std::string & test_name)180 void RunTest(const std::string& test_name) {
181 if (HasFlag(kUseGpu) && !IsGpuAvailable()) {
182 LOG(WARNING) <<
183 "Test skipped: requires gpu. Pass --enable-gpu on the command "
184 "line if use of GPU is desired.";
185 return;
186 }
187
188 std::string json_events;
189 ASSERT_TRUE(tracing::BeginTracing("gpu,mirroring"));
190 std::string page = "performance.html";
191 page += HasFlag(kTestThroughWebRTC) ? "?WebRTC=1" : "?WebRTC=0";
192 // Ideally we'd like to run a higher capture rate when vsync is disabled,
193 // but libjingle currently doesn't allow that.
194 // page += HasFlag(kDisableVsync) ? "&fps=300" : "&fps=30";
195 page += "&fps=30";
196 ASSERT_TRUE(RunExtensionSubtest("tab_capture", page)) << message_;
197 ASSERT_TRUE(tracing::EndTracing(&json_events));
198 scoped_ptr<trace_analyzer::TraceAnalyzer> analyzer;
199 analyzer.reset(trace_analyzer::TraceAnalyzer::Create(json_events));
200
201 // The printed result will be the average time between frames in the
202 // browser window.
203 bool gpu_frames = PrintResults(
204 analyzer.get(),
205 test_name,
206 "RenderWidget::didCommitAndDrawCompositorFrame",
207 "ms");
208 EXPECT_TRUE(gpu_frames);
209
210 // This prints out the average time between capture events.
211 // As the capture frame rate is capped at 30fps, this score
212 // cannot get any better than (lower) 33.33 ms.
213 EXPECT_TRUE(PrintResults(analyzer.get(),
214 test_name,
215 "Capture",
216 "ms"));
217 }
218 };
219
220 } // namespace
221
IN_PROC_BROWSER_TEST_P(TabCapturePerformanceTest,Performance)222 IN_PROC_BROWSER_TEST_P(TabCapturePerformanceTest, Performance) {
223 RunTest("TabCapturePerformance");
224 }
225
226 // Note: First argument is optional and intentionally left blank.
227 // (it's a prefix for the generated test cases)
228 INSTANTIATE_TEST_CASE_P(
229 ,
230 TabCapturePerformanceTest,
231 testing::Values(
232 0,
233 kUseGpu | kForceGpuComposited,
234 kDisableVsync,
235 kDisableVsync | kUseGpu | kForceGpuComposited,
236 kTestThroughWebRTC,
237 kTestThroughWebRTC | kUseGpu | kForceGpuComposited,
238 kTestThroughWebRTC | kDisableVsync,
239 kTestThroughWebRTC | kDisableVsync | kUseGpu | kForceGpuComposited));
240
241 #if defined(USE_AURA)
242 // TODO(hubbe):
243 // These are temporary tests for the purpose of determining what the
244 // appropriate scaling quality is. Once that has been determined,
245 // these tests will be removed.
246
247 const int kScalingTestBase =
248 kTestThroughWebRTC | kDisableVsync | kUseGpu | kForceGpuComposited;
249
250 INSTANTIATE_TEST_CASE_P(
251 ScalingTests,
252 TabCapturePerformanceTest,
253 testing::Values(
254 kScalingTestBase | kScaleQualityFast,
255 kScalingTestBase | kScaleQualityGood,
256 kScalingTestBase | kScaleQualityBest,
257 kScalingTestBase | kScaleQualityFast | kSmallWindow,
258 kScalingTestBase | kScaleQualityGood | kSmallWindow,
259 kScalingTestBase | kScaleQualityBest | kSmallWindow));
260
261 #endif // USE_AURA
262