Reland the ULONG -> SIZE_T change from 317177
[chromium-blink-merge.git] / tools / telemetry / examples / measure_trace.py
blob31e49e765aba6da3872878ab0eed10df91488e4b
1 #!/usr/bin/env python
2 # Copyright 2014 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 import collections
7 import json
8 import os
9 import sys
11 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir))
12 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir,
13 'perf'))
15 from telemetry.page import page as page_module
16 from telemetry.timeline import model
17 from telemetry.timeline import tracing_timeline_data
18 from telemetry.results import page_test_results
19 from telemetry.results import buildbot_output_formatter
20 from telemetry.web_perf import timeline_interaction_record as tir_module
21 from telemetry.web_perf.metrics import smoothness
22 # pylint: disable=F0401
23 from measurements import smoothness_controller
24 from measurements import smooth_gesture_util
27 def _ExtractInteractionsRecordFromThread(thread, timeline_model):
28 run_smooth_actions_record = None
29 records = []
30 for event in thread.async_slices:
31 if not tir_module.IsTimelineInteractionRecord(event.name):
32 continue
33 assert event.start_thread
34 assert event.start_thread is event.end_thread
35 r = smooth_gesture_util.GetAdjustedInteractionIfContainGesture(
36 timeline_model,
37 tir_module.TimelineInteractionRecord.FromAsyncEvent(event))
38 if r.label == smoothness_controller.RUN_SMOOTH_ACTIONS:
39 assert run_smooth_actions_record is None, (
40 'There can\'t be more than 1 %s record' %
41 smoothness_controller.RUN_SMOOTH_ACTIONS)
42 run_smooth_actions_record = r
43 else:
44 records.append(r)
45 if not records:
46 # Only include run_smooth_actions_record (label =
47 # smoothness_controller.RUN_SMOOTH_ACTIONS) if there is no other records
48 records = [run_smooth_actions_record]
49 return records
52 def Main(args):
53 if len(args) is not 1:
54 print 'Invalid arguments. Usage: measure_trace.py <trace file>'
55 return 1
56 with open(args[0]) as trace_file:
57 trace_data = tracing_timeline_data.TracingTimelineData(
58 json.load(trace_file))
60 timeline_model = model.TimelineModel(trace_data)
61 smoothness_metric = smoothness.SmoothnessMetric()
62 formatters = [
63 buildbot_output_formatter.BuildbotOutputFormatter(sys.stdout)
65 results = page_test_results.PageTestResults(output_formatters=formatters)
66 for thread in timeline_model.GetAllThreads():
67 interaction_records = _ExtractInteractionsRecordFromThread(
68 thread, timeline_model)
69 if not any(interaction_records):
70 continue
71 records_label_to_records_map = collections.defaultdict(list)
72 for r in interaction_records:
73 records_label_to_records_map[r.label].append(r)
74 for label, records in records_label_to_records_map.iteritems():
75 if records[0].is_smooth:
76 page = page_module.Page('interaction-record://%s' % label)
77 results.WillRunPage(page)
78 smoothness_metric.AddResults(
79 timeline_model, thread, records, results)
80 results.DidRunPage(page)
81 results.PrintSummary()
82 return 0
85 if __name__ == '__main__':
86 sys.exit(Main(sys.argv[1:]))