Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / test / perf / mach_ports_performancetest.cc
blob665fbcb274be1051119796d069d8c6895ade0836
1 // Copyright 2014 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 <mach/mach.h>
7 #include <vector>
9 #include "base/memory/ref_counted.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/stringprintf.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_window.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
15 #include "chrome/test/base/in_process_browser_test.h"
16 #include "net/test/spawned_test_server/spawned_test_server.h"
17 #include "testing/perf/perf_test.h"
19 namespace {
21 // This test spawns a new browser and counts the number of open Mach ports in
22 // the browser process. It navigates tabs and closes them, repeatedly measuring
23 // the number of open ports. This is used to protect against leaking Mach ports,
24 // which was the source of <http://crbug.com/105513>.
25 class MachPortsTest : public InProcessBrowserTest {
26 public:
27 MachPortsTest()
28 : server_(net::SpawnedTestServer::TYPE_HTTP,
29 net::SpawnedTestServer::kLocalhost,
30 base::FilePath(FILE_PATH_LITERAL("data/mach_ports/moz"))) {
33 void SetUp() override {
34 InProcessBrowserTest::SetUp();
36 ASSERT_TRUE(server_.Start());
39 void TearDown() override {
40 std::string ports;
41 for (std::vector<int>::iterator it = port_counts_.begin();
42 it != port_counts_.end(); ++it) {
43 base::StringAppendF(&ports, "%d,", *it);
45 perf_test::PrintResultList("mach_ports", "", "", ports, "ports", true);
47 InProcessBrowserTest::TearDown();
50 // Gets the browser's current number of Mach ports and records it.
51 void RecordPortCount() {
52 mach_port_name_array_t names;
53 mach_msg_type_number_t names_count = 0;
54 mach_port_type_array_t types;
55 mach_msg_type_number_t types_count = 0;
57 mach_port_t self = mach_task_self();
59 // A friendlier interface would allow NULL buffers to only get the counts.
60 kern_return_t kr = mach_port_names(self,
61 &names, &names_count,
62 &types, &types_count);
63 ASSERT_EQ(KERN_SUCCESS, kr) << "Failed to get mach_port_names(): "
64 << mach_error_string(kr);
65 ASSERT_EQ(names_count, types_count); // Documented kernel invariant.
67 port_counts_.push_back(names_count);
69 vm_deallocate(self, reinterpret_cast<vm_address_t>(names),
70 names_count * sizeof(mach_port_name_array_t));
71 vm_deallocate(self, reinterpret_cast<vm_address_t>(types),
72 types_count * sizeof(mach_port_type_array_t));
75 // Adds a tab from the page cycler data at the specified domain.
76 void AddTab(const std::string& domain) {
77 GURL url = server_.GetURL("files/" + domain + "/").Resolve("?skip");
78 AddTabAtIndex(0, url, ui::PAGE_TRANSITION_TYPED);
81 private:
82 net::SpawnedTestServer server_;
83 std::vector<int> port_counts_;
86 IN_PROC_BROWSER_TEST_F(MachPortsTest, GetCounts) {
87 browser()->window()->Show();
89 // Record startup number.
90 RecordPortCount();
92 // Create a browser and open a few tabs.
93 AddTab("www.google.com");
94 RecordPortCount();
96 AddTab("www.cnn.com");
97 RecordPortCount();
99 AddTab("www.nytimes.com");
100 RecordPortCount();
102 TabStripModel* tab_strip_model = browser()->tab_strip_model();
103 int tab_count = tab_strip_model->count();
104 ASSERT_EQ(4, tab_count); // Also count about:blank.
106 // Close each tab, recording the number of ports after each. Do not close the
107 // last tab, which is about:blank.
108 for (int i = 0; i < tab_count - 1; ++i) {
109 EXPECT_TRUE(
110 tab_strip_model->CloseWebContentsAt(0, TabStripModel::CLOSE_NONE));
111 RecordPortCount();
115 } // namespace