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.
5 #include "base/basictypes.h"
6 #include "base/command_line.h"
8 #include "base/mac/mac_util.h"
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"
40 const char kExtensionId[] = "ddchlicdkolnonkihahngkmmmjnjlkkf";
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
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,
59 class TabCapturePerformanceTest
60 : public ExtensionApiTest,
61 public testing::WithParamInterface<int> {
63 TabCapturePerformanceTest() {}
65 bool HasFlag(TestFlags flag) const {
66 return (GetParam() & flag) == flag;
69 bool IsGpuAvailable() const {
70 return CommandLine::ForCurrentProcess()->HasSwitch("enable-gpu");
73 std::string ScalingMethod() const {
74 switch (GetParam() & kScaleQualityMask) {
75 case kScaleQualityFast:
77 case kScaleQualityGood:
79 case kScaleQualityBest:
86 std::string GetSuffixForTestFlags() {
88 if (HasFlag(kForceGpuComposited))
92 if (HasFlag(kDisableVsync))
94 if (HasFlag(kTestThroughWebRTC))
96 if (!ScalingMethod().empty())
97 suffix += "_scale" + ScalingMethod();
98 if (HasFlag(kSmallWindow))
103 virtual void SetUp() OVERRIDE {
105 ExtensionApiTest::SetUp();
108 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
109 if (!ScalingMethod().empty()) {
110 command_line->AppendSwitchASCII(switches::kTabCaptureUpscaleQuality,
112 command_line->AppendSwitchASCII(switches::kTabCaptureDownscaleQuality,
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
120 command_line->AppendSwitch(switches::kAllowFileAccessFromFiles);
122 if (HasFlag(kSmallWindow)) {
123 command_line->AppendSwitchASCII(switches::kWindowSize, "800,600");
125 command_line->AppendSwitchASCII(switches::kWindowSize, "2000,1500");
128 if (!HasFlag(kUseGpu))
129 command_line->AppendSwitch(switches::kDisableGpu);
131 if (HasFlag(kDisableVsync))
132 command_line->AppendSwitch(switches::kDisableGpuVsync);
134 command_line->AppendSwitchASCII(
135 extensions::switches::kWhitelistedExtensionID,
137 ExtensionApiTest::SetUpCommandLine(command_line);
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() << ").";
159 // Ignore some events for startup/setup/caching.
160 trace_analyzer::TraceEventVector rate_events(events.begin() + 3,
162 trace_analyzer::RateStats stats;
163 if (!GetRateStats(rate_events, &stats, NULL)) {
164 LOG(ERROR) << "GetRateStats failed";
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,
171 perf_test::PrintResultMeanAndError(test_name,
172 GetSuffixForTestFlags(),
180 void RunTest(const std::string& test_name) {
181 if (HasFlag(kUseGpu) && !IsGpuAvailable()) {
183 "Test skipped: requires gpu. Pass --enable-gpu on the command "
184 "line if use of GPU is desired.";
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";
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));
201 // The printed result will be the average time between frames in the
203 bool gpu_frames = PrintResults(
206 "RenderWidget::didCommitAndDrawCompositorFrame",
208 EXPECT_TRUE(gpu_frames);
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(),
222 IN_PROC_BROWSER_TEST_P(TabCapturePerformanceTest, Performance) {
223 RunTest("TabCapturePerformance");
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(
230 TabCapturePerformanceTest,
233 kUseGpu | kForceGpuComposited,
235 kDisableVsync | kUseGpu | kForceGpuComposited,
237 kTestThroughWebRTC | kUseGpu | kForceGpuComposited,
238 kTestThroughWebRTC | kDisableVsync,
239 kTestThroughWebRTC | kDisableVsync | kUseGpu | kForceGpuComposited));
241 #if defined(USE_AURA)
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.
247 const int kScalingTestBase =
248 kTestThroughWebRTC | kDisableVsync | kUseGpu | kForceGpuComposited;
250 INSTANTIATE_TEST_CASE_P(
252 TabCapturePerformanceTest,
254 kScalingTestBase | kScaleQualityFast,
255 kScalingTestBase | kScaleQualityGood,
256 kScalingTestBase | kScaleQualityBest,
257 kScalingTestBase | kScaleQualityFast | kSmallWindow,
258 kScalingTestBase | kScaleQualityGood | kSmallWindow,
259 kScalingTestBase | kScaleQualityBest | kSmallWindow));