summaryrefslogtreecommitdiffstats
path: root/chrome/test/perf/mach_ports_performancetest.cc
blob: e80b9439ac6743aff744e7a20b7dec4887560ed7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <mach/mach.h>

#include <vector>

#include "base/memory/ref_counted.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "net/test/spawned_test_server/spawned_test_server.h"
#include "testing/perf/perf_test.h"

namespace {

// This test spawns a new browser and counts the number of open Mach ports in
// the browser process. It navigates tabs and closes them, repeatedly measuring
// the number of open ports. This is used to protect against leaking Mach ports,
// which was the source of <http://crbug.com/105513>.
class MachPortsTest : public InProcessBrowserTest {
 public:
  MachPortsTest()
      : server_(net::SpawnedTestServer::TYPE_HTTP,
                net::SpawnedTestServer::kLocalhost,
                base::FilePath(FILE_PATH_LITERAL("data/mach_ports/moz"))) {
  }

  virtual void SetUp() OVERRIDE {
    InProcessBrowserTest::SetUp();

    ASSERT_TRUE(server_.Start());
  }

  virtual void TearDown() OVERRIDE {
    std::string ports;
    for (std::vector<int>::iterator it = port_counts_.begin();
         it != port_counts_.end(); ++it) {
      base::StringAppendF(&ports, "%d,", *it);
    }
    perf_test::PrintResultList("mach_ports", "", "", ports, "ports", true);

    InProcessBrowserTest::TearDown();
  }

  // Gets the browser's current number of Mach ports and records it.
  void RecordPortCount() {
    mach_port_name_array_t names;
    mach_msg_type_number_t names_count = 0;
    mach_port_type_array_t types;
    mach_msg_type_number_t types_count = 0;

    mach_port_t self = mach_task_self();

    // A friendlier interface would allow NULL buffers to only get the counts.
    kern_return_t kr = mach_port_names(self,
                                       &names, &names_count,
                                       &types, &types_count);
    ASSERT_EQ(KERN_SUCCESS, kr) << "Failed to get mach_port_names(): "
                                << mach_error_string(kr);
    ASSERT_EQ(names_count, types_count);  // Documented kernel invariant.

    port_counts_.push_back(names_count);

    vm_deallocate(self, reinterpret_cast<vm_address_t>(names),
        names_count * sizeof(mach_port_name_array_t));
    vm_deallocate(self, reinterpret_cast<vm_address_t>(types),
        types_count * sizeof(mach_port_type_array_t));
  }

  // Adds a tab from the page cycler data at the specified domain.
  void AddTab(const std::string& domain) {
    GURL url = server_.GetURL("files/" + domain + "/").Resolve("?skip");
    AddTabAtIndex(0, url, ui::PAGE_TRANSITION_TYPED);
  }

 private:
  net::SpawnedTestServer server_;
  std::vector<int> port_counts_;
};

IN_PROC_BROWSER_TEST_F(MachPortsTest, GetCounts) {
  browser()->window()->Show();

  // Record startup number.
  RecordPortCount();

  // Create a browser and open a few tabs.
  AddTab("www.google.com");
  RecordPortCount();

  AddTab("www.cnn.com");
  RecordPortCount();

  AddTab("www.nytimes.com");
  RecordPortCount();

  TabStripModel* tab_strip_model = browser()->tab_strip_model();
  int tab_count = tab_strip_model->count();
  ASSERT_EQ(4, tab_count);  // Also count about:blank.

  // Close each tab, recording the number of ports after each. Do not close the
  // last tab, which is about:blank.
  for (int i = 0; i < tab_count - 1; ++i) {
    EXPECT_TRUE(
        tab_strip_model->CloseWebContentsAt(0, TabStripModel::CLOSE_NONE));
    RecordPortCount();
  }
}

}  // namespace