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

Side by Side Diff: lib/benchmarks.dart

Issue 1080123002: add benchmarks.dart which allows you to easily benchmark transformer code (Closed) Base URL: git@github.com:dart-lang/code-transformers.git@master
Patch Set: add missing return Created 5 years, 8 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) 2014, 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 library code_transformers.benchmarks;
5
6 import 'dart:async';
7 import 'package:barback/barback.dart';
8 import 'src/async_benchmark_base.dart';
9
10 /// A benchmark for testing the performance of transformer phases.
11 class TransformerBenchmark extends AsyncBenchmarkBase {
12 /// Internal abstraction layer for barback.
13 _BenchmarkHelper _helper;
14
15 /// The transformer phases to be ran.
16 final List<List<Transformer>> transformers;
17
18 /// The files to pass to barback.
19 final Map<AssetId, String> files;
20
21 TransformerBenchmark(this.transformers, this.files);
22
23 @override
24 Future setup() {
25 _helper = new _BenchmarkHelper(transformers, files);
26 return new Future.value();
27 }
28
29 @override
30 Future run() => _helper.run();
31
32 @override
33 teardown() {
34 _helper = null;
35 return new Future.value();
36 }
37 }
38
39 /// Barback abstraction layer.
40 class _BenchmarkHelper implements PackageProvider {
41 /// All the files available.
42 final Map<AssetId, String> _files;
43
44 /// All the packages.
45 final Iterable<String> packages;
46
47 /// Internal instance of barback.
48 Barback _barback;
49
50 /// Subscription to barback results.
51 StreamSubscription<BuildResult> resultSubscription;
52
53 _BenchmarkHelper(
54 List<List<Transformer>> transformers, Map<AssetId, String> files)
55 : _files = files,
56 packages = new Set.from(files.keys.map((assetId) => assetId.package)) {
57 _barback = new Barback(this);
58 for (var package in packages) {
59 _barback.updateTransformers(package, transformers);
60 }
61 }
62
63 /// Look up an [AssetId] in [files] and return an [Asset] for it.
64 @override
65 Future<Asset> getAsset(AssetId id) =>
66 new Future.value(new Asset.fromString(id, _files[id]));
67
68 /// Tells barback which files have changed, and thus anything that depends on
69 /// it on should be computed. Returns a [Future] that completes once some
70 /// results are received.
71 Future run([Iterable<AssetId> assetIds]) {
72 if (assetIds == null) assetIds = _files.keys;
73 var done = new Completer();
74 _barback.results.take(1).listen((_) => done.complete());
75 _barback.updateSources(assetIds);
76 return done.future;
Siggi Cherem (dart-lang) 2015/04/13 20:46:02 I think you can replace the completer by doing: r
jakemac 2015/04/13 21:04:19 Done.
77 }
78 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698