Chromium Code Reviews| 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_STACK_TRACE = {'traceEvents': [ | |
|
Benoit L
2016/02/12 10:26:41
Stack trace?
gabadie
2016/02/12 10:39:11
Oups... Done.
| |
| 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 class PageTrackTest(unittest.TestCase): | |
| 37 def testGetBrowserPID(self): | |
| 38 def RunHelper(expected, trace): | |
| 39 self.assertEquals(expected, puller._GetBrowserPID(trace)) | |
| 40 | |
| 41 RunHelper(123, {'traceEvents': [ | |
| 42 {'pid': 354, 'cat': 'whatever0'}, | |
| 43 {'pid': 354, 'cat': 'whatever1'}, | |
| 44 {'pid': 354, 'cat': '__metadata', 'name': 'thread_name'}, | |
| 45 {'pid': 354, 'cat': '__metadata', 'name': 'process_name', 'args': { | |
| 46 'name': 'Renderer'}}, | |
| 47 {'pid': 123, 'cat': '__metadata', 'name': 'process_name', 'args': { | |
| 48 'name': 'Browser'}}, | |
| 49 {'pid': 354, 'cat': 'whatever0'}]}) | |
| 50 | |
| 51 try: | |
|
Benoit L
2016/02/12 10:26:42
self.assertRaises() is cleaner here.
gabadie
2016/02/12 10:39:11
Done.
| |
| 52 RunHelper(123, {'traceEvents': [ | |
| 53 {'pid': 354, 'cat': 'whatever0'}, | |
| 54 {'pid': 354, 'cat': 'whatever1'}]}) | |
| 55 except Exception as e: | |
| 56 self.assertTrue(str(e) == 'couldn\'t find browser\'s PID') | |
| 57 else: | |
| 58 self.fail() | |
| 59 | |
| 60 def testGetBrowserDumpEvents(self): | |
| 61 NAME = 'periodic_interval' | |
| 62 | |
| 63 def RunHelper(trace_events, browser_pid): | |
| 64 trace_events = copy.copy(trace_events) | |
| 65 trace_events.append({ | |
| 66 'pid': browser_pid, | |
| 67 'cat': '__metadata', | |
| 68 'name': 'process_name', | |
| 69 'args': {'name': 'Browser'}}) | |
| 70 return puller._GetBrowserDumpEvents({'traceEvents': trace_events}) | |
| 71 | |
| 72 TRACE_EVENTS = [ | |
| 73 {'pid': 354, 'ts': 1, 'cat': _MEM_CAT, 'ph': 'v', 'name': NAME}, | |
| 74 {'pid': 354, 'ts': 2, 'cat': _MEM_CAT, 'ph': 'V'}, | |
| 75 {'pid': 672, 'ts': 3, 'cat': _MEM_CAT, 'ph': 'v', 'name': NAME}, | |
| 76 {'pid': 123, 'ts': 4, 'cat': _MEM_CAT, 'ph': 'v', 'name': 'foo'}, | |
| 77 {'pid': 123, 'ts': 5, 'cat': _MEM_CAT, 'ph': 'v', 'name': NAME}, | |
| 78 {'pid': 123, 'ts': 6, 'cat': _MEM_CAT, 'ph': 'V'}, | |
| 79 {'pid': 672, 'ts': 7, 'cat': _MEM_CAT, 'ph': 'v', 'name': NAME}, | |
| 80 {'pid': 354, 'ts': 8, 'cat': _MEM_CAT, 'ph': 'v', 'name': 'foo'}, | |
| 81 {'pid': 123, 'ts': 9, 'cat': 'whatever1', 'ph': 'v', 'name': NAME}, | |
| 82 {'pid': 123, 'ts': 10, 'cat': _MEM_CAT, 'ph': 'v', 'name': NAME}, | |
| 83 {'pid': 354, 'ts': 11, 'cat': 'whatever0'}, | |
| 84 {'pid': 672, 'ts': 12, 'cat': _MEM_CAT, 'ph': 'v', 'name': NAME}] | |
| 85 | |
| 86 self.assertTrue(_MEM_CAT in puller.CATEGORIES) | |
| 87 | |
| 88 bump_events = RunHelper(TRACE_EVENTS, 123) | |
| 89 self.assertEquals(2, len(bump_events)) | |
| 90 self.assertEquals(5, bump_events[0]['ts']) | |
| 91 self.assertEquals(10, bump_events[1]['ts']) | |
| 92 | |
| 93 bump_events = RunHelper(TRACE_EVENTS, 354) | |
| 94 self.assertEquals(1, len(bump_events)) | |
| 95 self.assertEquals(1, bump_events[0]['ts']) | |
| 96 | |
| 97 bump_events = RunHelper(TRACE_EVENTS, 672) | |
| 98 self.assertEquals(3, len(bump_events)) | |
| 99 self.assertEquals(3, bump_events[0]['ts']) | |
| 100 self.assertEquals(7, bump_events[1]['ts']) | |
| 101 self.assertEquals(12, bump_events[2]['ts']) | |
| 102 | |
| 103 try: | |
|
Benoit L
2016/02/12 10:26:42
Ditto, assertRaises()
gabadie
2016/02/12 10:39:11
Done.
| |
| 104 RunHelper(TRACE_EVENTS, 895) | |
| 105 except Exception as e: | |
| 106 self.assertTrue(str(e) == 'No browser dump events found.') | |
| 107 else: | |
| 108 self.fail() | |
| 109 | |
| 110 def testGetWebPageTrackedEvents(self): | |
| 111 self.assertTrue(_BLINK_CAT in puller.CATEGORIES) | |
| 112 | |
| 113 trace_events = puller._GetWebPageTrackedEvents({'traceEvents': [ | |
| 114 {'ts': 0, 'args': {}, 'cat': 'whatever', 'name': _START}, | |
| 115 {'ts': 1, 'args': {'frame': '0'}, 'cat': 'whatever', 'name': _LOADS}, | |
| 116 {'ts': 2, 'args': {'frame': '0'}, 'cat': 'whatever', 'name': _LOADE}, | |
| 117 {'ts': 3, 'args': {}, 'cat': _BLINK_CAT, 'name': _START}, | |
| 118 {'ts': 4, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, 'name': _LOADS}, | |
| 119 {'ts': 5, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, 'name': _LOADE}, | |
| 120 {'ts': 6, 'args': {'frame': '0'}, 'cat': 'whatever', 'name': _UNLOAD}, | |
| 121 {'ts': 7, 'args': {}, 'cat': _BLINK_CAT, 'name': _START}, | |
| 122 {'ts': 8, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, 'name': _LOADS}, | |
| 123 {'ts': 9, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, 'name': _LOADE}, | |
| 124 {'ts': 10, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, 'name': _UNLOAD}, | |
| 125 {'ts': 11, 'args': {'frame': '0'}, 'cat': 'whatever', 'name': _START}, | |
| 126 {'ts': 12, 'args': {'frame': '0'}, 'cat': 'whatever', 'name': _LOADS}, | |
| 127 {'ts': 13, 'args': {'frame': '0'}, 'cat': 'whatever', 'name': _LOADE}, | |
| 128 {'ts': 14, 'args': {}, 'cat': _BLINK_CAT, 'name': _START}, | |
| 129 {'ts': 15, 'args': {}, 'cat': _BLINK_CAT, 'name': _START}, | |
| 130 {'ts': 16, 'args': {'frame': '1'}, 'cat': _BLINK_CAT, 'name': _LOADS}, | |
| 131 {'ts': 17, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, 'name': _LOADS}, | |
| 132 {'ts': 18, 'args': {'frame': '1'}, 'cat': _BLINK_CAT, 'name': _LOADE}, | |
| 133 {'ts': 19, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, 'name': _LOADE}, | |
| 134 {'ts': 20, 'args': {}, 'cat': 'whatever', 'name': _START}, | |
| 135 {'ts': 21, 'args': {'frame': '0'}, 'cat': 'whatever', 'name': _LOADS}, | |
| 136 {'ts': 22, 'args': {'frame': '0'}, 'cat': 'whatever', 'name': _LOADE}, | |
| 137 {'ts': 23, 'args': {}, 'cat': _BLINK_CAT, 'name': _START}, | |
| 138 {'ts': 24, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, 'name': _LOADS}, | |
| 139 {'ts': 25, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, 'name': _LOADE}]}) | |
| 140 | |
| 141 self.assertEquals(3, len(trace_events)) | |
| 142 self.assertEquals(14, trace_events['requestStart']['ts']) | |
| 143 self.assertEquals(17, trace_events['loadEventStart']['ts']) | |
| 144 self.assertEquals(19, trace_events['loadEventEnd']['ts']) | |
| 145 | |
| 146 def testPullMetricsFromTrace(self): | |
| 147 metrics = puller._PullMetricsFromTrace(_MINIMALIST_STACK_TRACE) | |
| 148 self.assertEquals(4, len(metrics)) | |
| 149 self.assertEquals(20, metrics['total_load']) | |
| 150 self.assertEquals(5, metrics['onload']) | |
| 151 self.assertEquals(30971, metrics['browser_malloc_avg']) | |
| 152 self.assertEquals(55044, metrics['browser_malloc_max']) | |
| 153 | |
| 154 def testCommandLine(self): | |
| 155 tmp_dir = tempfile.mkdtemp() | |
| 156 for dirname in ['1', '2', 'whatever']: | |
| 157 os.mkdir(os.path.join(tmp_dir, dirname)) | |
| 158 with open(os.path.join(tmp_dir, dirname, 'trace.json'), 'w') as out_file: | |
| 159 json.dump(_MINIMALIST_STACK_TRACE, out_file) | |
| 160 | |
| 161 process = subprocess.Popen(['python', puller.__file__, tmp_dir]) | |
| 162 process.wait() | |
| 163 shutil.rmtree(tmp_dir) | |
| 164 | |
| 165 self.assertEquals(0, process.returncode) | |
| 166 | |
| 167 if __name__ == '__main__': | |
|
Benoit L
2016/02/12 10:26:41
2 lines between top-level declarations.
gabadie
2016/02/12 10:39:11
Done.
| |
| 168 unittest.main() | |
| OLD | NEW |