Chromium Code Reviews| Index: tools/android/loading/pull_sandwich_metrics_unittest.py |
| diff --git a/tools/android/loading/pull_sandwich_metrics_unittest.py b/tools/android/loading/pull_sandwich_metrics_unittest.py |
| index 3c985087161c61288fb71b8869be4334d3375413..10918b5fa09dc17f5e1cbccfa7a7a996f2f7f19b 100644 |
| --- a/tools/android/loading/pull_sandwich_metrics_unittest.py |
| +++ b/tools/android/loading/pull_sandwich_metrics_unittest.py |
| @@ -10,7 +10,12 @@ import subprocess |
| import tempfile |
| import unittest |
| +import loading_trace |
| +import page_track |
| import pull_sandwich_metrics as puller |
| +import request_track |
| +import tracing |
| + |
| _BLINK_CAT = 'blink.user_timing' |
| _MEM_CAT = 'disabled-by-default-memory-infra' |
| @@ -19,40 +24,56 @@ _LOADS='loadEventStart' |
| _LOADE='loadEventEnd' |
| _UNLOAD='unloadEventEnd' |
| -_MINIMALIST_TRACE = {'traceEvents': [ |
| - {'cat': _BLINK_CAT, 'name': _UNLOAD, 'ts': 10, 'args': {'frame': '0'}}, |
| - {'cat': _BLINK_CAT, 'name': _START, 'ts': 20, 'args': {}, }, |
| +_MINIMALIST_TRACE_EVENTS = [ |
| + {'ph': 'R', 'cat': _BLINK_CAT, 'name': _UNLOAD, 'ts': 10000, |
| + 'args': {'frame': '0'}}, |
| + {'ph': 'R', 'cat': _BLINK_CAT, 'name': _START, 'ts': 20000, |
| + 'args': {}}, |
| {'cat': _MEM_CAT, 'name': 'periodic_interval', 'pid': 1, 'ph': 'v', |
| - 'args': {'dumps': {'allocators': {'malloc': {'attrs': {'size':{ |
| + 'ts': 1, 'args': {'dumps': {'allocators': {'malloc': {'attrs': {'size':{ |
| 'units': 'bytes', 'value': '1af2', }}}}}}}, |
| - {'cat': _BLINK_CAT, 'name': _LOADS, 'ts': 35, 'args': {'frame': '0'}}, |
| - {'cat': _BLINK_CAT, 'name': _LOADE, 'ts': 40, 'args': {'frame': '0'}}, |
| + {'ph': 'R', 'cat': _BLINK_CAT, 'name': _LOADS, 'ts': 35000, |
| + 'args': {'frame': '0'}}, |
| + {'ph': 'R', 'cat': _BLINK_CAT, 'name': _LOADE, 'ts': 40000, |
| + 'args': {'frame': '0'}}, |
| {'cat': _MEM_CAT, 'name': 'periodic_interval', 'pid': 1, 'ph': 'v', |
| - 'args': {'dumps': {'allocators': {'malloc': {'attrs': {'size':{ |
| + 'ts': 1, 'args': {'dumps': {'allocators': {'malloc': {'attrs': {'size':{ |
| 'units': 'bytes', 'value': 'd704', }}}}}}}, |
| - {'cat': '__metadata', 'pid': 1, 'name': 'process_name', 'args': { |
| - 'name': 'Browser'}}]} |
| + {'ph': 'M', 'cat': '__metadata', 'pid': 1, 'name': 'process_name', 'ts': 1, |
| + 'args': {'name': 'Browser'}}] |
| + |
| + |
| +def TracingTrack(events): |
| + return tracing.TracingTrack.FromJsonDict({'events': events}) |
| + |
| + |
| +def LoadingTrace(events): |
| + return loading_trace.LoadingTrace('http://a.com/', {}, |
| + page_track.PageTrack(None), |
|
mattcary
2016/02/19 14:41:05
Can just pass in None instead of a *Track created
gabadie
2016/02/19 15:04:35
Already tried and I can't otherwise the SaveToJson
|
| + request_track.RequestTrack(None), |
| + TracingTrack(events)) |
| class PageTrackTest(unittest.TestCase): |
| def testGetBrowserPID(self): |
| - def RunHelper(expected, trace): |
| - self.assertEquals(expected, puller._GetBrowserPID(trace)) |
| - |
| - RunHelper(123, {'traceEvents': [ |
| - {'pid': 354, 'cat': 'whatever0'}, |
| - {'pid': 354, 'cat': 'whatever1'}, |
| - {'pid': 354, 'cat': '__metadata', 'name': 'thread_name'}, |
| - {'pid': 354, 'cat': '__metadata', 'name': 'process_name', 'args': { |
| - 'name': 'Renderer'}}, |
| - {'pid': 123, 'cat': '__metadata', 'name': 'process_name', 'args': { |
| - 'name': 'Browser'}}, |
| - {'pid': 354, 'cat': 'whatever0'}]}) |
| + def RunHelper(expected, events): |
| + self.assertEquals(expected, puller._GetBrowserPID(TracingTrack(events))) |
| + |
| + RunHelper(123, [ |
| + {'ph': 'M', 'ts': 0, 'pid': 354, 'cat': 'whatever0'}, |
| + {'ph': 'M', 'ts': 0, 'pid': 354, 'cat': 'whatever1'}, |
| + {'ph': 'M', 'ts': 0, 'pid': 354, 'cat': '__metadata', |
| + 'name': 'thread_name'}, |
| + {'ph': 'M', 'ts': 0, 'pid': 354, 'cat': '__metadata', |
| + 'name': 'process_name', 'args': {'name': 'Renderer'}}, |
| + {'ph': 'M', 'ts': 0, 'pid': 123, 'cat': '__metadata', |
| + 'name': 'process_name', 'args': {'name': 'Browser'}}, |
| + {'ph': 'M', 'ts': 0, 'pid': 354, 'cat': 'whatever0'}]) |
| with self.assertRaises(ValueError): |
| - RunHelper(123, {'traceEvents': [ |
| - {'pid': 354, 'cat': 'whatever0'}, |
| - {'pid': 354, 'cat': 'whatever1'}]}) |
| + RunHelper(123, [ |
| + {'ph': 'M', 'ts': 0, 'pid': 354, 'cat': 'whatever0'}, |
| + {'ph': 'M', 'ts': 0, 'pid': 354, 'cat': 'whatever1'}]) |
| def testGetBrowserDumpEvents(self): |
| NAME = 'periodic_interval' |
| @@ -63,39 +84,41 @@ class PageTrackTest(unittest.TestCase): |
| 'pid': browser_pid, |
| 'cat': '__metadata', |
| 'name': 'process_name', |
| + 'ph': 'M', |
| + 'ts': 0, |
| 'args': {'name': 'Browser'}}) |
| - return puller._GetBrowserDumpEvents({'traceEvents': trace_events}) |
| + return puller._GetBrowserDumpEvents(TracingTrack(trace_events)) |
| TRACE_EVENTS = [ |
| - {'pid': 354, 'ts': 1, 'cat': _MEM_CAT, 'ph': 'v', 'name': NAME}, |
| - {'pid': 354, 'ts': 2, 'cat': _MEM_CAT, 'ph': 'V'}, |
| - {'pid': 672, 'ts': 3, 'cat': _MEM_CAT, 'ph': 'v', 'name': NAME}, |
| - {'pid': 123, 'ts': 4, 'cat': _MEM_CAT, 'ph': 'v', 'name': 'foo'}, |
| - {'pid': 123, 'ts': 5, 'cat': _MEM_CAT, 'ph': 'v', 'name': NAME}, |
| - {'pid': 123, 'ts': 6, 'cat': _MEM_CAT, 'ph': 'V'}, |
| - {'pid': 672, 'ts': 7, 'cat': _MEM_CAT, 'ph': 'v', 'name': NAME}, |
| - {'pid': 354, 'ts': 8, 'cat': _MEM_CAT, 'ph': 'v', 'name': 'foo'}, |
| - {'pid': 123, 'ts': 9, 'cat': 'whatever1', 'ph': 'v', 'name': NAME}, |
| - {'pid': 123, 'ts': 10, 'cat': _MEM_CAT, 'ph': 'v', 'name': NAME}, |
| - {'pid': 354, 'ts': 11, 'cat': 'whatever0'}, |
| - {'pid': 672, 'ts': 12, 'cat': _MEM_CAT, 'ph': 'v', 'name': NAME}] |
| + {'pid': 354, 'ts': 1000, 'cat': _MEM_CAT, 'ph': 'v', 'name': NAME}, |
| + {'pid': 354, 'ts': 2000, 'cat': _MEM_CAT, 'ph': 'V'}, |
| + {'pid': 672, 'ts': 3000, 'cat': _MEM_CAT, 'ph': 'v', 'name': NAME}, |
| + {'pid': 123, 'ts': 4000, 'cat': _MEM_CAT, 'ph': 'v', 'name': 'foo'}, |
| + {'pid': 123, 'ts': 5000, 'cat': _MEM_CAT, 'ph': 'v', 'name': NAME}, |
| + {'pid': 123, 'ts': 6000, 'cat': _MEM_CAT, 'ph': 'V'}, |
| + {'pid': 672, 'ts': 7000, 'cat': _MEM_CAT, 'ph': 'v', 'name': NAME}, |
| + {'pid': 354, 'ts': 8000, 'cat': _MEM_CAT, 'ph': 'v', 'name': 'foo'}, |
| + {'pid': 123, 'ts': 9000, 'cat': 'whatever1', 'ph': 'v', 'name': NAME}, |
| + {'pid': 123, 'ts': 10000, 'cat': _MEM_CAT, 'ph': 'v', 'name': NAME}, |
| + {'pid': 354, 'ts': 11000, 'cat': 'whatever0', 'ph': 'R'}, |
| + {'pid': 672, 'ts': 12000, 'cat': _MEM_CAT, 'ph': 'v', 'name': NAME}] |
| self.assertTrue(_MEM_CAT in puller.CATEGORIES) |
| bump_events = RunHelper(TRACE_EVENTS, 123) |
| self.assertEquals(2, len(bump_events)) |
| - self.assertEquals(5, bump_events[0]['ts']) |
| - self.assertEquals(10, bump_events[1]['ts']) |
| + self.assertEquals(5, bump_events[0].start_msec) |
| + self.assertEquals(10, bump_events[1].start_msec) |
| bump_events = RunHelper(TRACE_EVENTS, 354) |
| self.assertEquals(1, len(bump_events)) |
| - self.assertEquals(1, bump_events[0]['ts']) |
| + self.assertEquals(1, bump_events[0].start_msec) |
| bump_events = RunHelper(TRACE_EVENTS, 672) |
| self.assertEquals(3, len(bump_events)) |
| - self.assertEquals(3, bump_events[0]['ts']) |
| - self.assertEquals(7, bump_events[1]['ts']) |
| - self.assertEquals(12, bump_events[2]['ts']) |
| + self.assertEquals(3, bump_events[0].start_msec) |
| + self.assertEquals(7, bump_events[1].start_msec) |
| + self.assertEquals(12, bump_events[2].start_msec) |
| with self.assertRaises(ValueError): |
| RunHelper(TRACE_EVENTS, 895) |
| @@ -103,41 +126,68 @@ class PageTrackTest(unittest.TestCase): |
| def testGetWebPageTrackedEvents(self): |
| self.assertTrue(_BLINK_CAT in puller.CATEGORIES) |
| - trace_events = puller._GetWebPageTrackedEvents({'traceEvents': [ |
| - {'ts': 0, 'args': {}, 'cat': 'whatever', 'name': _START}, |
| - {'ts': 1, 'args': {'frame': '0'}, 'cat': 'whatever', 'name': _LOADS}, |
| - {'ts': 2, 'args': {'frame': '0'}, 'cat': 'whatever', 'name': _LOADE}, |
| - {'ts': 3, 'args': {}, 'cat': _BLINK_CAT, 'name': _START}, |
| - {'ts': 4, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, 'name': _LOADS}, |
| - {'ts': 5, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, 'name': _LOADE}, |
| - {'ts': 6, 'args': {'frame': '0'}, 'cat': 'whatever', 'name': _UNLOAD}, |
| - {'ts': 7, 'args': {}, 'cat': _BLINK_CAT, 'name': _START}, |
| - {'ts': 8, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, 'name': _LOADS}, |
| - {'ts': 9, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, 'name': _LOADE}, |
| - {'ts': 10, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, 'name': _UNLOAD}, |
| - {'ts': 11, 'args': {'frame': '0'}, 'cat': 'whatever', 'name': _START}, |
| - {'ts': 12, 'args': {'frame': '0'}, 'cat': 'whatever', 'name': _LOADS}, |
| - {'ts': 13, 'args': {'frame': '0'}, 'cat': 'whatever', 'name': _LOADE}, |
| - {'ts': 14, 'args': {}, 'cat': _BLINK_CAT, 'name': _START}, |
| - {'ts': 15, 'args': {}, 'cat': _BLINK_CAT, 'name': _START}, |
| - {'ts': 16, 'args': {'frame': '1'}, 'cat': _BLINK_CAT, 'name': _LOADS}, |
| - {'ts': 17, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, 'name': _LOADS}, |
| - {'ts': 18, 'args': {'frame': '1'}, 'cat': _BLINK_CAT, 'name': _LOADE}, |
| - {'ts': 19, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, 'name': _LOADE}, |
| - {'ts': 20, 'args': {}, 'cat': 'whatever', 'name': _START}, |
| - {'ts': 21, 'args': {'frame': '0'}, 'cat': 'whatever', 'name': _LOADS}, |
| - {'ts': 22, 'args': {'frame': '0'}, 'cat': 'whatever', 'name': _LOADE}, |
| - {'ts': 23, 'args': {}, 'cat': _BLINK_CAT, 'name': _START}, |
| - {'ts': 24, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, 'name': _LOADS}, |
| - {'ts': 25, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, 'name': _LOADE}]}) |
| + trace_events = puller._GetWebPageTrackedEvents(TracingTrack([ |
| + {'ph': 'R', 'ts': 0000, 'args': {}, 'cat': 'whatever', |
| + 'name': _START}, |
| + {'ph': 'R', 'ts': 1000, 'args': {'frame': '0'}, 'cat': 'whatever', |
| + 'name': _LOADS}, |
| + {'ph': 'R', 'ts': 2000, 'args': {'frame': '0'}, 'cat': 'whatever', |
| + 'name': _LOADE}, |
| + {'ph': 'R', 'ts': 3000, 'args': {}, 'cat': _BLINK_CAT, |
| + 'name': _START}, |
| + {'ph': 'R', 'ts': 4000, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, |
| + 'name': _LOADS}, |
| + {'ph': 'R', 'ts': 5000, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, |
| + 'name': _LOADE}, |
| + {'ph': 'R', 'ts': 6000, 'args': {'frame': '0'}, 'cat': 'whatever', |
| + 'name': _UNLOAD}, |
| + {'ph': 'R', 'ts': 7000, 'args': {}, 'cat': _BLINK_CAT, |
| + 'name': _START}, |
| + {'ph': 'R', 'ts': 8000, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, |
| + 'name': _LOADS}, |
| + {'ph': 'R', 'ts': 9000, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, |
| + 'name': _LOADE}, |
| + {'ph': 'R', 'ts': 10000, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, |
| + 'name': _UNLOAD}, |
| + {'ph': 'R', 'ts': 11000, 'args': {'frame': '0'}, 'cat': 'whatever', |
| + 'name': _START}, |
| + {'ph': 'R', 'ts': 12000, 'args': {'frame': '0'}, 'cat': 'whatever', |
| + 'name': _LOADS}, |
| + {'ph': 'R', 'ts': 13000, 'args': {'frame': '0'}, 'cat': 'whatever', |
| + 'name': _LOADE}, |
| + {'ph': 'R', 'ts': 14000, 'args': {}, 'cat': _BLINK_CAT, |
| + 'name': _START}, |
| + {'ph': 'R', 'ts': 15000, 'args': {}, 'cat': _BLINK_CAT, |
| + 'name': _START}, |
| + {'ph': 'R', 'ts': 16000, 'args': {'frame': '1'}, 'cat': _BLINK_CAT, |
| + 'name': _LOADS}, |
| + {'ph': 'R', 'ts': 17000, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, |
| + 'name': _LOADS}, |
| + {'ph': 'R', 'ts': 18000, 'args': {'frame': '1'}, 'cat': _BLINK_CAT, |
| + 'name': _LOADE}, |
| + {'ph': 'R', 'ts': 19000, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, |
| + 'name': _LOADE}, |
| + {'ph': 'R', 'ts': 20000, 'args': {}, 'cat': 'whatever', |
| + 'name': _START}, |
| + {'ph': 'R', 'ts': 21000, 'args': {'frame': '0'}, 'cat': 'whatever', |
| + 'name': _LOADS}, |
| + {'ph': 'R', 'ts': 22000, 'args': {'frame': '0'}, 'cat': 'whatever', |
| + 'name': _LOADE}, |
| + {'ph': 'R', 'ts': 23000, 'args': {}, 'cat': _BLINK_CAT, |
| + 'name': _START}, |
| + {'ph': 'R', 'ts': 24000, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, |
| + 'name': _LOADS}, |
| + {'ph': 'R', 'ts': 25000, 'args': {'frame': '0'}, 'cat': _BLINK_CAT, |
| + 'name': _LOADE}])) |
| self.assertEquals(3, len(trace_events)) |
| - self.assertEquals(14, trace_events['requestStart']['ts']) |
| - self.assertEquals(17, trace_events['loadEventStart']['ts']) |
| - self.assertEquals(19, trace_events['loadEventEnd']['ts']) |
| + self.assertEquals(14, trace_events['requestStart'].start_msec) |
| + self.assertEquals(17, trace_events['loadEventStart'].start_msec) |
| + self.assertEquals(19, trace_events['loadEventEnd'].start_msec) |
| - def testPullMetricsFromTrace(self): |
| - metrics = puller._PullMetricsFromTrace(_MINIMALIST_TRACE) |
| + def testPullMetricsFromLoadingTrace(self): |
| + metrics = puller._PullMetricsFromLoadingTrace(LoadingTrace( |
| + _MINIMALIST_TRACE_EVENTS)) |
| self.assertEquals(4, len(metrics)) |
| self.assertEquals(20, metrics['total_load']) |
| self.assertEquals(5, metrics['onload']) |
| @@ -150,8 +200,8 @@ class PageTrackTest(unittest.TestCase): |
| json.dump({'urls': ['a.com', 'b.com', 'c.org']}, out_file) |
| for dirname in ['1', '2', 'whatever']: |
| os.mkdir(os.path.join(tmp_dir, dirname)) |
| - with open(os.path.join(tmp_dir, dirname, 'trace.json'), 'w') as out_file: |
| - json.dump(_MINIMALIST_TRACE, out_file) |
| + LoadingTrace(_MINIMALIST_TRACE_EVENTS).SaveToJsonFile( |
| + os.path.join(tmp_dir, dirname, 'trace.json')) |
| process = subprocess.Popen(['python', puller.__file__, tmp_dir]) |
| process.wait() |