OLD | NEW |
| (Empty) |
1 #!mojo mojo:dart_content_handler | |
2 // Copyright 2015 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. | |
5 | |
6 // This example application illustrates how to use Mojo Tracing from Dart code. | |
7 // | |
8 // To run this app: | |
9 // mojo/devtools/common/mojo_run --trace-startup \ | |
10 // https://core.mojoapps.io/examples/dart/traced_application/lib/main.dart | |
11 // | |
12 // This will produce a file called |mojo_shell.trace| that may be loaded | |
13 // by Chrome's about:tracing. | |
14 | |
15 import 'dart:async'; | |
16 import 'dart:io'; | |
17 | |
18 import 'package:common/tracing_helper.dart'; | |
19 import 'package:mojo/application.dart'; | |
20 import 'package:mojo/bindings.dart'; | |
21 import 'package:mojo/core.dart'; | |
22 | |
23 class TestUsingTracingApp extends Application { | |
24 TracingHelper _tracing; | |
25 Timer _timer; | |
26 | |
27 TestUsingTracingApp.fromHandle(MojoHandle handle) : super.fromHandle(handle); | |
28 | |
29 void initialize(List<String> args, String url) { | |
30 // This sets up a connection between this application and the Mojo | |
31 // tracing service. | |
32 _tracing = new TracingHelper.fromApplication( | |
33 this, "example_traced_application", TraceSendTiming.AT_END); | |
34 _tracing.traceInstant("initialized", "traced_application"); | |
35 | |
36 // Now we schedule some random work just so we have something to trace. | |
37 _timer = new Timer.periodic(new Duration(seconds: 1), (t) => function1()); | |
38 | |
39 onError = (() { | |
40 if (_timer != null) { | |
41 _timer.cancel(); | |
42 } | |
43 }); | |
44 } | |
45 | |
46 @override | |
47 void acceptConnection(String requestorUrl, String resolvedUrl, | |
48 ApplicationConnection connection) { | |
49 _tracing.traceInstant("connected", "traced_application"); | |
50 } | |
51 | |
52 Future function1() { | |
53 return _tracing.traceAsync("function1", "traced_application", () async { | |
54 await waitForMilliseconds(100); | |
55 await function2(42); | |
56 await waitForMilliseconds(100); | |
57 }); | |
58 } | |
59 | |
60 Future function2(int x) { | |
61 return _tracing.traceAsync("function2", "traced_application", () async { | |
62 await waitForMilliseconds(200); | |
63 assert(await identity(42) == 42); | |
64 assert(await fourtyTwo() == 42); | |
65 assert(await addOne(42) == 43); | |
66 await doNothing(); | |
67 }, args: {"x": x}); | |
68 } | |
69 | |
70 Future identity(int x) { | |
71 return _tracing.traceAsync("identity", "traced_application", () async { | |
72 await waitForMilliseconds(10); | |
73 return x; | |
74 }); | |
75 } | |
76 | |
77 Future addOne(int x) { | |
78 return _tracing.traceAsync("add1", "traced_application", () async { | |
79 await waitForMilliseconds(10); | |
80 return x + 1; | |
81 }, args: {"x": x}); | |
82 } | |
83 | |
84 Future fourtyTwo() { | |
85 return _tracing.traceAsync("fourtyTwo", "traced_application", () async { | |
86 await waitForMilliseconds(10); | |
87 return 42; | |
88 }); | |
89 } | |
90 | |
91 Future doNothing() { | |
92 return _tracing.traceAsync("doNothing", "traced_application", () async { | |
93 await waitForMilliseconds(10); | |
94 }); | |
95 } | |
96 } | |
97 | |
98 Future waitForMilliseconds(int milliseconds) { | |
99 return new Future.delayed(new Duration(milliseconds: milliseconds), () {}); | |
100 } | |
101 | |
102 main(List args) { | |
103 MojoHandle appHandle = new MojoHandle(args[0]); | |
104 new TestUsingTracingApp.fromHandle(appHandle); | |
105 } | |
OLD | NEW |