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.
4
5import codecs
6
7from telemetry.core.platform import profiler
8
9
10class TraceProfiler(profiler.Profiler):
11
12  def __init__(self, browser_backend, platform_backend, output_path, state):
13    super(TraceProfiler, self).__init__(
14        browser_backend, platform_backend, output_path, state)
15    assert self._browser_backend.supports_tracing
16    self._browser_backend.StartTracing(None, timeout=10)
17
18  @classmethod
19  def name(cls):
20    return 'trace'
21
22  @classmethod
23  def is_supported(cls, browser_type):
24    return True
25
26  def CollectProfile(self):
27    print 'Processing trace...'
28
29    trace_result = self._browser_backend.StopTracing()
30
31    trace_file = '%s.json' % self._output_path
32
33    with codecs.open(trace_file, 'w', encoding='utf-8') as f:
34      trace_result.Serialize(f)
35
36    print 'Trace saved as %s' % trace_file
37    print 'To view, open in chrome://tracing'
38
39    return [trace_file]
40