Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit efbff9c

Browse files
committed
chore(reporter): record lexer benchmark results
This initial version sends benchmark metrics to the backend server. Visit https://ng-dash.appspot.com/commit/fb2aa60a584dd4b2bbb3d30e8fcd4c588f8669ed or https://ng-dash.appspot.com/all to see an example. (github: https://github.com/chirayuk/ng-dash) In this version, only Dart code run from the command line is able to record such reports. The code in benchmark/_reporter.dart handles sending the data to the server. A future version will mode this code into a locally running proxy server enabling browser based Dart code to send such reports while still including the information from the build environment. Reading data from the report server does not require any auth. See a simple example at chirayuk/ng-dash@ex_01 Writing data requires authentication. Specifically, the REST server requires two cookies set: user_email and user_secret. (user_email does not have to be a valid e-mail just like AppEngine user e-mails.) Of these, user_secret is meant to be secret and should be safeguarded. For Travis, user_email is "travis-ci.org" and user_secret is encrypted in the .travis.yml. Refer http://docs.travis-ci.com/user/build-configuration/#Secure-environment-variables for details on how this is done.
1 parent 1c7c0ba commit efbff9c

File tree

5 files changed

+221
-8
lines changed

5 files changed

+221
-8
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ env:
2323
- FIREFOX_VERSION="29.0"
2424
- CHROME_BIN=/usr/bin/google-chrome
2525
- secure: "AKoqpZ699egF0i4uT/FQ5b4jIc0h+KVbhtVCql0uFxwFIl2HjOYgDayrUCAf6USfpW0LghZxJJhBamWOl/505eNSe9HvEd8JLg/to+1Fo9xi9llsu5ehmNH31/5pue4EvsrVuEap1qqL6/BNwI2cAryayU0p5tV0g8gL5h4IxG8="
26+
# Config for sending benchmark/perf metrics.
27+
- NGDASH_BASE_URL=https://ng-dash.appspot.com
28+
- NGDASH_USER_EMAIL=travis-ci.org
29+
- secure: "n3KJsLLXEh1wlLRTF2wWvnDBAL+sOg+Mf/gc/Ub9/zCpXLDd1LP76hWBH/d7TCaC0oH5dnyD3ugV6PtJ7VEPRBZp72IbuNZzj8Ui8SisXVd0aos4u7s7X5NVwcxobhxd8Csoi5QPT31w8iT6qaC9VSXnYM3EEGqeppRqRBu6Hkg="
2630

2731
# Don't test these branches.
2832
branches:

benchmark/_reporter.dart

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
library _reporter;
2+
3+
import 'dart:io';
4+
import 'dart:convert' show UTF8, JSON;
5+
6+
7+
String getBaseUrl() {
8+
String ngDashBaseUrl = Platform.environment["NGDASH_BASE_URL"];
9+
if (ngDashBaseUrl == null || ngDashBaseUrl.isEmpty) {
10+
ngDashBaseUrl = "http://ng-dash.gae.localhost";
11+
}
12+
return ngDashBaseUrl;
13+
}
14+
15+
16+
class ResultData {
17+
String name = "";
18+
String description = "";
19+
Map<String, dynamic> dimensions = {};
20+
Map<String, dynamic> metrics = {};
21+
List<ResultData> children = [];
22+
23+
ResultData(String this.name, String this.description);
24+
25+
newChild(String name, String description) {
26+
ResultData child = new ResultData(name, description);
27+
children.add(child);
28+
return child;
29+
}
30+
31+
toJson() => {
32+
"name": name,
33+
"description": description,
34+
"dimensions_json": JSON.encode(dimensions),
35+
"metrics_json": JSON.encode(metrics),
36+
"children": children.map((i) => i.toJson()).toList(),
37+
};
38+
}
39+
40+
41+
Map<String, String> getTravisDimension() {
42+
if (Platform.environment["TRAVIS"] != "true") {
43+
throw "getTravisDimension(): Not called on TRAVIS";
44+
}
45+
Map<String, String> result = {};
46+
// Ref: http://docs.travis-ci.com/user/ci-environment/
47+
for (String envVar in const [
48+
"TRAVIS_BRANCH", // The name of the branch currently being built.
49+
"TRAVIS_BUILD_ID", // The id of the current build that Travis CI uses internally.
50+
"TRAVIS_BUILD_NUMBER", // The number of the current build (for example, "4").
51+
"TRAVIS_COMMIT", // The commit that the current build is testing.
52+
"TRAVIS_COMMIT_RANGE", // The range of commits that were included in the push or pull request.
53+
"TRAVIS_JOB_ID", // The id of the current job that Travis CI uses internally.
54+
"TRAVIS_JOB_NUMBER", // The number of the current job (for example, "4.1").
55+
"TRAVIS_PULL_REQUEST", // The pull request number if the current job is a pull request, "false" if it's not a pull request.
56+
"TRAVIS_REPO_SLUG", // "owner_name/repo_name" (e.g. "travis-ci/travis-build").
57+
"TRAVIS_OS_NAME", // Name of the operating system built on. (e.g. linux or osx)
58+
"TRAVIS_TAG", // (optional) tag name for current build if relevant.
59+
]) {
60+
String value = Platform.environment[envVar];
61+
if (value != null && !value.isEmpty) {
62+
result[envVar.substring("TRAVIS_".length).toLowerCase()] = value;
63+
}
64+
}
65+
return result;
66+
}
67+
68+
69+
String getOsType() {
70+
if (Platform.isMacOS) {
71+
return "OSX";
72+
} else if (Platform.isLinux) {
73+
return "Linux";
74+
} else if (Platform.isWindows) {
75+
return "Windows";
76+
} else if (Platform.Android) {
77+
return "Android";
78+
}
79+
}
80+
81+
82+
class Reporter {
83+
ResultData data = new ResultData("", "");
84+
final String baseUrl = getBaseUrl();
85+
String commitSha = "";
86+
String treeSha = "";
87+
String reportId = null;
88+
List<Cookie> cookies;
89+
90+
Reporter() {
91+
var dimensions = data.dimensions;
92+
dimensions["project"] = "AngularDart";
93+
dimensions["dart"] = {
94+
"full_version": Platform.version,
95+
"version": Platform.version.split(" ")[0],
96+
};
97+
dimensions["os"] = {
98+
"type": getOsType(),
99+
};
100+
if (Platform.environment["TRAVIS"] == "true") {
101+
dimensions["travis"] = getTravisDimension();
102+
commitSha = dimensions["travis"]["commit"];
103+
}
104+
105+
// Auth cookies
106+
var user_email = Platform.environment["NGDASH_USER_EMAIL"];
107+
var user_secret = Platform.environment["NGDASH_USER_SECRET"];
108+
if (user_email == null || user_email.isEmpty ||
109+
user_secret == null || user_secret.isEmpty) {
110+
throw "Please set NGDASH_USER_EMAIL and NGDASH_USER_SECRET credentials.";
111+
}
112+
cookies = [new Cookie("user_email", user_email),
113+
new Cookie("user_secret", user_secret)];
114+
115+
if (commitSha.isEmpty) {
116+
throw "Could not detect the commit SHA. (non-travis detection not implemented yet.)";
117+
}
118+
}
119+
120+
121+
// The following machinery is there just to serialize saving to the server.
122+
// If we're already in the process of saving the results, then just mark that
123+
// we need to save again. This is also particularly important because the
124+
// first time, we create a new report, and in all subsequent calls, we update
125+
// that same report using the report ID that was received when we created it.
126+
var _messageQueue = [];
127+
var _needKick = true;
128+
129+
_sendNextMessage() {
130+
print("_sendNextMessage: reportId=$reportId");
131+
132+
if (_messageQueue.length == 0) {
133+
_needKick = true;
134+
print("_sendNextMessage: _messageQueue is empty. returning");
135+
return;
136+
}
137+
String requestData = JSON.encode(_messageQueue.removeAt(0));
138+
139+
_needKick = false;
140+
141+
if (reportId == null) {
142+
Uri url = Uri.parse("${baseUrl}/api/run");
143+
new HttpClient().postUrl(url)
144+
.then((HttpClientRequest request) {
145+
request.headers.contentType = ContentType.JSON;
146+
request.cookies.addAll(cookies);
147+
request.write(requestData);
148+
return request.close();
149+
})
150+
.then((HttpClientResponse response) {
151+
var responseData = "";
152+
response.transform(UTF8.decoder).listen(
153+
(partialData) { responseData += partialData; },
154+
onDone: () {
155+
reportId = JSON.decode(responseData)["id"];
156+
_sendNextMessage();
157+
}
158+
);
159+
});
160+
} else {
161+
Uri url = Uri.parse("${baseUrl}/api/run/id=$reportId");
162+
new HttpClient().putUrl(url)
163+
.then((HttpClientRequest request) {
164+
request.headers.contentType = ContentType.JSON;
165+
request.cookies.addAll(cookies);
166+
request.write(requestData);
167+
return request.close();
168+
})
169+
.then((HttpClientResponse response) {
170+
response.transform(UTF8.decoder).listen(null, onDone: _sendNextMessage);
171+
});
172+
}
173+
}
174+
175+
176+
void saveReport() {
177+
print("saveReport: _needKick=$_needKick");
178+
_messageQueue.add(this);
179+
if (_needKick) {
180+
_sendNextMessage();
181+
}
182+
}
183+
184+
185+
toJson() => {
186+
"commit_sha": commitSha,
187+
"tree_sha": treeSha,
188+
"data": data.toJson(),
189+
};
190+
}

benchmark/lexer_perf.dart

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,39 @@
11
library lexer_perf;
22

33
import '_perf.dart';
4+
import '_reporter.dart';
45
import 'package:angular/core/parser/lexer.dart';
56

7+
8+
Reporter reporter = new Reporter();
9+
var lexerStats = reporter.data.newChild("lexer", "lexer benchmarks");
10+
11+
12+
report(name, fn) {
13+
var stats = lexerStats.newChild(name, "");
14+
var metrics = statMeasure(fn);
15+
stats.metrics["ops_per_sec"] = metrics.mean_ops_sec;
16+
stats.metrics["variance"] = metrics.variance;
17+
print('$name: => $metrics');
18+
reporter.saveReport();
19+
}
20+
21+
622
main() {
723
Lexer lexer = new Lexer();
8-
time('ident', () =>
24+
25+
report('ident', () =>
926
lexer.call('ctrl foo baz ctrl.foo ctrl.bar ctrl.baz'));
10-
time('ident-path', () =>
27+
report('ident-path', () =>
1128
lexer.call('a.b a.b.c a.b.c.d a.b.c.d.e.f'));
12-
time('num', () =>
29+
report('num', () =>
1330
lexer.call('1 23 34 456 12341234 12351235'));
14-
time('num-double', () =>
31+
report('num-double', () =>
1532
lexer.call('.0 .1 .12 0.123 0.1234'));
16-
time('string', () =>
33+
report('string', () =>
1734
lexer.call("'quick brown dog and fox say what'"));
18-
time('string-escapes', () =>
35+
report('string-escapes', () =>
1936
lexer.call("quick '\\' brown \u1234 dog and fox\n\rsay what'"));
37+
38+
reporter.saveReport();
2039
}

benchmark/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ packages:
3838
di:
3939
description: di
4040
source: hosted
41-
version: "1.0.0"
41+
version: "1.1.0"
4242
html5lib:
4343
description: html5lib
4444
source: hosted

benchmark/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ dependencies:
66
path: ..
77
dev_dependencies:
88
benchmark_harness: '>=1.0.0'
9-
unittest: '>=0.10.1 <0.12.0'
109
mock: '>=0.10.0 <0.12.0'
10+
unittest: '>=0.10.1 <0.12.0'
1111
transformers:
1212
- angular
1313
- $dart2js:

0 commit comments

Comments
 (0)