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.
4import logging
5import os
6import unittest
7
8from telemetry.core import util
9from telemetry.core.platform.profiler import perf_profiler
10from telemetry.unittest import options_for_unittests
11from telemetry.unittest import simple_mock
12
13
14class TestPerfProfiler(unittest.TestCase):
15  def testPerfProfiler(self):
16    options = options_for_unittests.GetCopy()
17    if not perf_profiler.PerfProfiler.is_supported(options.browser_type):
18      logging.warning('PerfProfiler is not supported. Skipping test')
19      return
20
21    profile_file = os.path.join(
22        util.GetUnittestDataDir(), 'perf_report_output.txt')
23    with open(profile_file) as f:
24      perf_report_output = f.read()
25
26    mock_popen = simple_mock.MockObject()
27    mock_popen.ExpectCall('communicate').WillReturn([perf_report_output])
28
29    mock_subprocess = simple_mock.MockObject()
30    mock_subprocess.ExpectCall(
31        'Popen').WithArgs(simple_mock.DONT_CARE).WillReturn(mock_popen)
32    mock_subprocess.SetAttribute('PIPE', simple_mock.MockObject())
33
34    real_subprocess = perf_profiler.subprocess
35    perf_profiler.subprocess = mock_subprocess
36    try:
37      self.assertEqual(
38          perf_profiler.PerfProfiler.GetTopSamples(profile_file, 10),
39          { 'v8::internal::StaticMarkingVisitor::MarkMapContents': 63615201,
40            'v8::internal::RelocIterator::next': 38271931,
41            'v8::internal::LAllocator::MeetConstraintsBetween': 42913933,
42            'v8::internal::FlexibleBodyVisitor::Visit': 31909537,
43            'v8::internal::LiveRange::CreateAssignedOperand': 42913933,
44            'void v8::internal::RelocInfo::Visit': 96878864,
45            'WebCore::HTMLTokenizer::nextToken': 48240439,
46            'v8::internal::Scanner::ScanIdentifierOrKeyword': 46054550,
47            'sk_memset32_SSE2': 45121317,
48            'v8::internal::HeapObject::Size': 39786862
49            })
50    finally:
51      perf_profiler.subprocess = real_subprocess
52