1 // Copyright 2017 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/time/time.h"
6 #include "chrome/browser/extensions/extension_browsertest.h"
7 #include "chrome/browser/ui/tabs/tab_strip_model.h"
8 #include "chrome/test/base/ui_test_utils.h"
9 #include "content/public/test/browser_test.h"
10 #include "content/public/test/browser_test_utils.h"
11 #include "extensions/test/test_extension_dir.h"
12 
13 namespace extensions {
14 namespace {
15 
16 // TODO(jbroman, devlin): This should ultimately be replaced with some more
17 // sophisticated testing (e.g. in Telemetry) which is tracked on the perf bots.
18 
19 // These tests are designed to exercise the extension API bindings
20 // system and measure performance with and without native bindings.
21 // They are designed to be run locally, and there isn't much benefit to
22 // running them on the bots. For this reason, they are all disabled.
23 // To run them, append the --gtest_also_run_disabled_tests flag to the
24 // test executable.
25 #define LOCAL_TEST(TestName) DISABLED_ ## TestName
26 
27 class APIBindingPerfBrowserTest : public ExtensionBrowserTest {
28  protected:
APIBindingPerfBrowserTest()29   APIBindingPerfBrowserTest() {}
~APIBindingPerfBrowserTest()30   ~APIBindingPerfBrowserTest() override {}
31 
SetUpOnMainThread()32   void SetUpOnMainThread() override {
33     ExtensionBrowserTest::SetUpOnMainThread();
34     EXPECT_TRUE(embedded_test_server()->Start());
35   }
36 
RunTestAndReportTime()37   base::TimeDelta RunTestAndReportTime() {
38     double time_elapsed_ms = 0;
39     EXPECT_TRUE(content::ExecuteScriptAndExtractDouble(
40         browser()->tab_strip_model()->GetActiveWebContents(),
41         "runTest(time => window.domAutomationController.send(time))",
42         &time_elapsed_ms));
43     return base::TimeDelta::FromMillisecondsD(time_elapsed_ms);
44   }
45 
46  private:
47   DISALLOW_COPY_AND_ASSIGN(APIBindingPerfBrowserTest);
48 };
49 
50 constexpr char kSimpleContentScriptManifest[] =
51     R"({
52          "name": "Perf test extension",
53          "version": "0",
54          "manifest_version": 2,
55          "content_scripts": [ {
56            "all_frames": true,
57            "matches": [ "<all_urls>" ],
58            "run_at": "document_end",
59            "js": [ "content_script.js" ]
60          } ],
61          "permissions": [ "storage" ]
62        })";
63 
IN_PROC_BROWSER_TEST_F(APIBindingPerfBrowserTest,LOCAL_TEST (ManyFramesWithNoContentScript))64 IN_PROC_BROWSER_TEST_F(APIBindingPerfBrowserTest,
65                        LOCAL_TEST(ManyFramesWithNoContentScript)) {
66   ui_test_utils::NavigateToURL(browser(),
67                                embedded_test_server()->GetURL(
68                                    "/extensions/perf_tests/many_frames.html"));
69 
70   base::TimeDelta time_elapsed = RunTestAndReportTime();
71   LOG(INFO) << "Executed in " << time_elapsed.InMillisecondsF() << " ms";
72 }
73 
IN_PROC_BROWSER_TEST_F(APIBindingPerfBrowserTest,LOCAL_TEST (ManyFramesWithEmptyContentScript))74 IN_PROC_BROWSER_TEST_F(APIBindingPerfBrowserTest,
75                        LOCAL_TEST(ManyFramesWithEmptyContentScript)) {
76   TestExtensionDir extension_dir;
77   extension_dir.WriteManifest(kSimpleContentScriptManifest);
78   extension_dir.WriteFile(FILE_PATH_LITERAL("content_script.js"),
79                           "// This space intentionally left blank.");
80   ASSERT_TRUE(LoadExtension(extension_dir.UnpackedPath()));
81 
82   ui_test_utils::NavigateToURL(browser(),
83                                embedded_test_server()->GetURL(
84                                    "/extensions/perf_tests/many_frames.html"));
85 
86   base::TimeDelta time_elapsed = RunTestAndReportTime();
87   LOG(INFO) << "Executed in " << time_elapsed.InMillisecondsF() << " ms";
88 }
89 
IN_PROC_BROWSER_TEST_F(APIBindingPerfBrowserTest,LOCAL_TEST (ManyFramesWithStorageAndRuntime))90 IN_PROC_BROWSER_TEST_F(APIBindingPerfBrowserTest,
91                        LOCAL_TEST(ManyFramesWithStorageAndRuntime)) {
92   TestExtensionDir extension_dir;
93   extension_dir.WriteManifest(kSimpleContentScriptManifest);
94   extension_dir.WriteFile(FILE_PATH_LITERAL("content_script.js"),
95                           "chrome.storage.onChanged.addListener;"
96                           "chrome.runtime.onMessage.addListener;");
97   ASSERT_TRUE(LoadExtension(extension_dir.UnpackedPath()));
98 
99   ui_test_utils::NavigateToURL(browser(),
100                                embedded_test_server()->GetURL(
101                                    "/extensions/perf_tests/many_frames.html"));
102 
103   base::TimeDelta time_elapsed = RunTestAndReportTime();
104   LOG(INFO) << "Executed in " << time_elapsed.InMillisecondsF() << " ms";
105 }
106 
107 }  // namespace
108 }  // namespace extensions
109