Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: bin/inference/client.dart

Issue 1372333002: Add measurements and send-metrics to dart2js's info. (Closed) Base URL: git@github.com:dart-lang/dart2js_info.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 /// Client component to display [GlobalResult]s as a web app.
6 library dart2js_info.bin.inference.client;
7
8 import 'dart:html' hide Entry;
9 import 'dart:convert';
10 import 'package:dart2js_info/info.dart';
11 import 'package:dart2js_info/src/string_edit_buffer.dart';
12 import 'package:charcode/charcode.dart';
13
14 AllInfo data;
15 main() async {
16 data =
17 AllInfo.parseFromJson(JSON.decode(await HttpRequest.getString('/data')));
18
19 routeByHash();
20 window.onHashChange.listen((_) => routeByHash());
21 }
22
23 /// Does basic routing for the client UI.
24 routeByHash() {
25 var hash = window.location.hash;
26 if (hash.isEmpty || hash == '#' || hash == '#!') {
27 handleHomePage();
28 } else if (hash.startsWith('#!')) {
29 handleFileView(hash.substring(2));
30 }
31 }
32
33 /// Renders the home screen: a list of files with results.
34 handleHomePage() {
35 var files = UrlRetriever.run(data);
36 var html = new StringBuffer()..write('<ul>');
37 for (var file in files) {
38 html.write('<li> <a href="#!$file">$file</a></li>');
39 }
40 html.write('</ul>');
41 document.body.setInnerHtml('$html', treeSanitizer: NodeTreeSanitizer.trusted);
42 }
43
44 /// Renders the results of a single file: the code with highlighting for each
45 /// send.
46 handleFileView(String path) async {
47 var contents = await HttpRequest.getString('file/$path');
48 var visitor = new SendHighlighter(path, contents);
49 data.accept(visitor);
50 var code = '${visitor.code}';
51 document.body.setInnerHtml(
52 '''
53 <div class="grid">
54 <div class="main code">$code</div>
55 <div id="selections" class="right code"></div>
56 </div>
57 ''',
58 treeSanitizer: NodeTreeSanitizer.trusted);
59
60 var div = document.querySelector('#selections');
61 visitAllMetrics((metric, _) {
62 if (metric is GroupedMetric || metric.name == 'reachable functions') return;
63 var cssClassName = _classNameForMetric(metric);
64 var node = new Element.html('<div>'
65 '<span class="send $cssClassName inactive">${metric.name}</span>'
66 '</div>');
67 node.children[0].onClick.listen((_) {
68 document.querySelectorAll('.$cssClassName').classes.toggle('inactive');
69 });
70 div.append(node);
71 });
72 }
73
74 /// Extracts urls for all files mentioned in the results.
75 class UrlRetriever extends RecursiveInfoVisitor {
76 List<String> _paths = [];
77
78 static List<String> run(AllInfo results) {
79 var visitor = new UrlRetriever();
80 results.accept(visitor);
81 return visitor._paths;
82 }
83
84 @override
85 visitLibrary(LibraryInfo info) {
86 _paths.add(info.uri.path);
87 super.visitLibrary(info);
88 }
89
90 @override
91 visitFunction(FunctionInfo info) {
92 var path = info.measurements?.uri?.path;
93 if (path != null) _paths.add(path);
94 }
95 }
96
97 /// Visitors that highlights every send in the text of a file using HTML
98 /// `<span>` tags.
99 class SendHighlighter extends RecursiveInfoVisitor {
100 final String path;
101 final StringEditBuffer code;
102
103 SendHighlighter(this.path, String contents)
104 : code = new StringEditBuffer(contents) {
105 code.insert(0, '<span class="line">');
106 for (int i = 0; i < contents.length; i++) {
107 if (contents.codeUnitAt(i) == $lt) {
108 code.replace(i, i + 1, '&lt;');
109 } else if (contents.codeUnitAt(i) == $gt) {
110 code.replace(i, i + 1, '&gt;');
111 } else if (contents.codeUnitAt(i) == $lf) {
112 code.insert(i + 1, '</span><span class="line">');
113 }
114 }
115 code.insert(contents.length, '</span>');
116 }
117
118 @override
119 visitFunction(FunctionInfo function) {
120 if (function.measurements?.uri?.path != path) return;
121 var entries = function.measurements.entries;
122 for (var metric in entries.keys) {
123 if (metric is GroupedMetric) continue;
124 var cssClassName = _classNameForMetric(metric);
125 for (var entry in entries[metric]) {
126 code.insert(entry.begin, '<span class="send ${cssClassName} inactive">',
127 -entry.end);
128 code.insert(entry.end, '</span>');
129 }
130 }
131 }
132 }
133
134 _classNameForMetric(Metric metric) => metric.name.replaceAll(' ', '-');
OLDNEW
« no previous file with comments | « CHANGELOG.md ('k') | bin/inference/index.html » ('j') | lib/src/table.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698