OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 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 import copy |
| 6 import json |
| 7 import os |
| 8 import shutil |
| 9 import subprocess |
| 10 import tempfile |
| 11 import unittest |
| 12 |
| 13 import pull_sandwich_metrics as puller |
| 14 |
| 15 _BLINK_CAT = 'blink.user_timing' |
| 16 _MEM_CAT = 'disabled-by-default-memory-infra' |
| 17 _START='requestStart' |
| 18 _LOADS='loadEventStart' |
| 19 _LOADE='loadEventEnd' |
| 20 _UNLOAD='unloadEventEnd' |
| 21 |
| 22 _MINIMALIST_TRACE = {'traceEvents': [ |
| 23 {'cat': _BLINK_CAT, 'name': _UNLOAD, 'ts': 10, 'args': {'frame': '0'}}, |
| 24 {'cat': _BLINK_CAT, 'name': _START, 'ts': 20, 'args': {}, }, |
| 25 {'cat': _MEM_CAT, 'name': 'periodic_interval', 'pid': 1, 'ph': 'v', |
| 26 'args': {'dumps': {'allocators': {'malloc': {'attrs': {'size':{ |
| 27 'units': 'bytes', 'value': '1af2', }}}}}}}, |
| 28 {'cat': _BLINK_CAT, 'name': _LOADS, 'ts': 35, 'args': {'frame': '0'}}, |
| 29 {'cat': _BLINK_CAT, 'name': _LOADE, 'ts': 40, 'args': {'frame': '0'}}, |
| 30 {'cat': _MEM_CAT, 'name': 'periodic_interval', 'pid': 1, 'ph': 'v', |
| 31 'args': {'dumps': {'allocators': {'malloc': {'attrs': {'size':{ |
| 32 'units': 'bytes', 'value': 'd704', }}}}}}}, |
| 33 {'cat': '__metadata', 'pid': 1, 'name': 'process_name', 'args': { |
| 34 'name': 'Browser'}}]} |
| 35 |
| 36 |
| 37 class PageTrackTest(unittest.TestCase): |
| 38 def testGetBrowserPID(self): |
| 39 def RunHelper(expected, trace): |
| 40 self.assertEquals(expected, puller._GetBrowserPID(trace)) |
| 41 |
| 42 RunHelper(123, {'traceEvents': [ |
| 43 {'pid': 354, 'cat': 'whatever0'}, |
| 44 {'pid': 354, 'cat': 'whatever1'}, |
| 45 {'pid': 354, 'cat': '__metadata', 'name': 'thread_name'}, |
| 46 {'pid': 354, 'cat': '__metadata', 'name': 'process_name', 'args': { |
| 47 'name': 'Renderer'}}, |
| 48 {'pid': 123, 'cat': '__metadata', 'name': 'process_name', 'args': { |
| 49 'name': 'Browser'}}, |
| 50 {'pid': 354, 'cat': 'whatever0'}]}) |
| 51 |
| 52 with self.assertRaises(ValueError): |
| 53 RunHelper(123, {'traceEvents': [ |
| 54 {'pid': 354, 'cat': 'whatever0'}, |
| 55 {'pid': 354, 'cat': 'whatever1'}]}) |
| 56 |
| 57 def testGetBrowserDumpEvents(self): |
| 58 NAME = 'periodic_interval' |
| 59 |
| 60 def RunHelper(trace_events, browser_pid): |
| 61 trace_events = copy.copy(trace_events) |
| 62 trace_events.append({ |
| 63 'pid': browser_pid, |
| 64 'cat': '__metadata', |
| 65 'name': 'process_name', |
| 66 'args': {'name': 'Browser'}}) |
| 67 return puller._GetBrowserDumpEvents({'traceEvents': trace_events}) |
| 68 |
| 69 TRACE_EVENTS = [ |
| 70 {'pid': 354, 'ts': 1, 'cat': _MEM_CAT, 'ph': 'v', 'name': NAME}, |
| 71 {'pid': 354, 'ts': 2, 'cat': _MEM_CAT, 'ph': 'V'}, |
| 72 {'pid': 672, 'ts': 3, 'cat': _MEM_CAT, 'ph': 'v', 'name': NAME}, |
| 73 {'pid': 123, 'ts': 4, 'cat': _MEM_CAT, 'ph': 'v', 'name': 'foo'}, |
| 74 {'pid': 123, 'ts': 5, 'cat': _MEM_CAT, 'ph': 'v', 'name': NAME}, |
| 75 {'pid': 123, 'ts': 6, 'cat': _MEM_CAT, 'ph': 'V'}, |
| 76 {'pid': 672, 'ts': 7, 'cat': _MEM_CAT, 'ph': 'v', 'name': NAME}, |
| 77 {'pid': 354, 'ts': 8, 'cat': _MEM_CAT, 'ph': 'v', 'name': 'foo'}, |
| 78 {'pid': 123, 'ts': 9, 'cat': 'whatever1', 'ph': 'v', 'name': NAME}, |
| 79 {'pid': 123, 'ts': 10, 'cat': _MEM_CAT, 'ph': 'v', 'name': NAME}, |
| 80 {'pid': 354, 'ts': 11, 'cat': 'whatever0'}, |
| 81 {'pid': 672, 'ts': 12, 'cat': _MEM_CAT, 'ph': 'v', 'name': NAME}] |
| 82 |
| 83 self.assertTrue(_MEM_CAT in puller.CATEGORIES) |
| 84 |
| 85 bump_events = RunHelper(TRACE_EVENTS, 123) |
| 86 self.assertEquals(2, len(bump_events)) |
| 87 self.assertEquals(5, bump_events[0]['ts']) |
| 88 self.assertEquals(10, bump_events[1]['ts']) |
| 89 |
| 90 bump_events = RunHelper(TRACE_EVENTS, 354) |
| 91 self.assertEquals(1, len(bump_events)) |
| 92 self.assertEquals(1, bump_events[0]['ts']) |
| 93 |
| 94 bump_events = RunHelper(TRACE_EVENTS, 672) |
| 95 self.assertEquals(3, len(bump_events)) |
| 96 self.assertEquals(3, bump_events[0]['ts']) |
| 97 self.assertEquals(7, bump_events[1]['ts']) |
| 98 self.assertEquals(12, bump_events[2]['ts']) |
| 99 |
| 100 with self.assertRaises(ValueError): |
| 101 RunHelper(TRACE_EVENTS, 895) |
| 102 |
| 103 def testGetWebPageTrackedEvents(self): |
| 104 self.assertTrue(_BLINK_CAT in puller.CATEGORIES) |
| 105 |
| 106 trace_events = puller._GetWebPageTrackedEvents({'traceEvents': [ |
| 107 {'ts': 0, 'args': {}, 'cat': 'whatever', 'name': _START}, |
| 108 {'ts': 1, 'args': {'frame': '0'}, 'cat': 'whatever', 'name': _LOADS}, |
| 109 {'ts': 2, 'args': {'frame': '0'}, 'cat': 'whatever', 'name': _LOADE}, |
| 110 {'ts': 3, 'args': {}, 'cat': _BLINK_CAT, 'name': _START}, |
| 111 {'ts': 4, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, 'name': _LOADS}, |
| 112 {'ts': 5, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, 'name': _LOADE}, |
| 113 {'ts': 6, 'args': {'frame': '0'}, 'cat': 'whatever', 'name': _UNLOAD}, |
| 114 {'ts': 7, 'args': {}, 'cat': _BLINK_CAT, 'name': _START}, |
| 115 {'ts': 8, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, 'name': _LOADS}, |
| 116 {'ts': 9, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, 'name': _LOADE}, |
| 117 {'ts': 10, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, 'name': _UNLOAD}, |
| 118 {'ts': 11, 'args': {'frame': '0'}, 'cat': 'whatever', 'name': _START}, |
| 119 {'ts': 12, 'args': {'frame': '0'}, 'cat': 'whatever', 'name': _LOADS}, |
| 120 {'ts': 13, 'args': {'frame': '0'}, 'cat': 'whatever', 'name': _LOADE}, |
| 121 {'ts': 14, 'args': {}, 'cat': _BLINK_CAT, 'name': _START}, |
| 122 {'ts': 15, 'args': {}, 'cat': _BLINK_CAT, 'name': _START}, |
| 123 {'ts': 16, 'args': {'frame': '1'}, 'cat': _BLINK_CAT, 'name': _LOADS}, |
| 124 {'ts': 17, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, 'name': _LOADS}, |
| 125 {'ts': 18, 'args': {'frame': '1'}, 'cat': _BLINK_CAT, 'name': _LOADE}, |
| 126 {'ts': 19, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, 'name': _LOADE}, |
| 127 {'ts': 20, 'args': {}, 'cat': 'whatever', 'name': _START}, |
| 128 {'ts': 21, 'args': {'frame': '0'}, 'cat': 'whatever', 'name': _LOADS}, |
| 129 {'ts': 22, 'args': {'frame': '0'}, 'cat': 'whatever', 'name': _LOADE}, |
| 130 {'ts': 23, 'args': {}, 'cat': _BLINK_CAT, 'name': _START}, |
| 131 {'ts': 24, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, 'name': _LOADS}, |
| 132 {'ts': 25, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, 'name': _LOADE}]}) |
| 133 |
| 134 self.assertEquals(3, len(trace_events)) |
| 135 self.assertEquals(14, trace_events['requestStart']['ts']) |
| 136 self.assertEquals(17, trace_events['loadEventStart']['ts']) |
| 137 self.assertEquals(19, trace_events['loadEventEnd']['ts']) |
| 138 |
| 139 def testPullMetricsFromTrace(self): |
| 140 metrics = puller._PullMetricsFromTrace(_MINIMALIST_TRACE) |
| 141 self.assertEquals(4, len(metrics)) |
| 142 self.assertEquals(20, metrics['total_load']) |
| 143 self.assertEquals(5, metrics['onload']) |
| 144 self.assertEquals(30971, metrics['browser_malloc_avg']) |
| 145 self.assertEquals(55044, metrics['browser_malloc_max']) |
| 146 |
| 147 def testCommandLine(self): |
| 148 tmp_dir = tempfile.mkdtemp() |
| 149 for dirname in ['1', '2', 'whatever']: |
| 150 os.mkdir(os.path.join(tmp_dir, dirname)) |
| 151 with open(os.path.join(tmp_dir, dirname, 'trace.json'), 'w') as out_file: |
| 152 json.dump(_MINIMALIST_TRACE, out_file) |
| 153 |
| 154 process = subprocess.Popen(['python', puller.__file__, tmp_dir]) |
| 155 process.wait() |
| 156 shutil.rmtree(tmp_dir) |
| 157 |
| 158 self.assertEquals(0, process.returncode) |
| 159 |
| 160 |
| 161 if __name__ == '__main__': |
| 162 unittest.main() |
OLD | NEW |