Skip to content

Commit cedd052

Browse files
authored
Make testBenchmarks() synchronous. (#1499)
This avoids calling group() after an async pause, which breaks running short_format_test.dart and tall_format_test.dart outside of the test runner.
1 parent fbde0dd commit cedd052

File tree

3 files changed

+17
-23
lines changed

3 files changed

+17
-23
lines changed

benchmark/run.dart

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import 'package:dart_style/src/front_end/ast_node_visitor.dart';
1616
import 'package:dart_style/src/profile.dart';
1717
import 'package:dart_style/src/short/source_visitor.dart';
1818
import 'package:dart_style/src/testing/benchmark.dart';
19+
import 'package:dart_style/src/testing/test_file.dart';
1920
import 'package:path/path.dart' as p;
2021
import 'package:pub_semver/pub_semver.dart';
2122

@@ -71,9 +72,7 @@ Future<void> main(List<String> arguments) async {
7172
}
7273
}
7374

74-
if (_runWarmupTrials) {
75-
await _warmUp();
76-
}
75+
if (_runWarmupTrials) _warmUp();
7776

7877
var results = <(Benchmark, List<double>)>[];
7978
for (var benchmark in benchmarks) {
@@ -110,8 +109,8 @@ Future<void> main(List<String> arguments) async {
110109
/// Since the benchmarks are run on the VM, JIT warm-up has a large impact on
111110
/// performance. Warming up the VM gives it time for the optimized compiler to
112111
/// kick in.
113-
Future<void> _warmUp() async {
114-
var benchmark = await Benchmark.read('benchmark/case/large.unit');
112+
void _warmUp() {
113+
var benchmark = Benchmark.read('benchmark/case/large.unit');
115114
_runTrials('Warming up', benchmark, _warmUpTrials);
116115
}
117116

@@ -209,16 +208,14 @@ Future<List<Benchmark>> _parseArguments(List<String> arguments) async {
209208

210209
var benchmarks = switch (argResults.rest) {
211210
// Find all the benchmarks.
212-
['all'] => await Benchmark.findAll(),
211+
['all'] => Benchmark.findAll(await findPackageDirectory()),
213212

214213
// Default to the large benchmark.
215-
[] => [
216-
await Benchmark.read(p.join(_benchmarkDirectory, 'case/large.unit'))
217-
],
214+
[] => [Benchmark.read(p.join(_benchmarkDirectory, 'case/large.unit'))],
218215

219216
// The user-specified list of paths.
220217
[...var paths] when paths.isNotEmpty => [
221-
for (var path in paths) await Benchmark.read(path)
218+
for (var path in paths) Benchmark.read(path)
222219
],
223220
_ => _usage(argParser, exitCode: 64),
224221
};

lib/src/testing/benchmark.dart

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,15 @@ import 'dart:io';
66

77
import 'package:path/path.dart' as p;
88

9-
import 'test_file.dart';
10-
119
class Benchmark {
12-
/// Finds all of the benchmarks in the `benchmark/cases` directory.
13-
static Future<List<Benchmark>> findAll() async {
14-
var casesDirectory =
15-
Directory(p.join(await findPackageDirectory(), 'benchmark/case'));
10+
/// Finds all of the benchmarks in the `benchmark/cases` directory, relative
11+
/// to [packageDirectory].
12+
static List<Benchmark> findAll(String packageDirectory) {
13+
var casesDirectory = Directory(p.join(packageDirectory, 'benchmark/case'));
1614

1715
var benchmarks = [
1816
for (var entry in casesDirectory.listSync())
19-
if (p.extension(entry.path) case '.unit' || '.stmt')
20-
await read(entry.path)
17+
if (p.extension(entry.path) case '.unit' || '.stmt') read(entry.path)
2118
];
2219

2320
benchmarks.sort((a, b) => a.name.compareTo(b.name));
@@ -30,8 +27,8 @@ class Benchmark {
3027
/// This should point to a `.unit` or `.stmt` file that has a corresponding
3128
/// `.expect` and `expect_short` file in the same directory with those
3229
/// expectations.
33-
static Future<Benchmark> read(String path) async {
34-
var inputLines = await File(path).readAsLines();
30+
static Benchmark read(String path) {
31+
var inputLines = File(path).readAsLinesSync();
3532

3633
// The first line may have a "|" to indicate the page width.
3734
var pageWidth = 80;
@@ -43,8 +40,8 @@ class Benchmark {
4340
var input = inputLines.join('\n');
4441

4542
var shortOutput =
46-
await File(p.setExtension(path, '.expect_short')).readAsString();
47-
var tallOutput = await File(p.setExtension(path, '.expect')).readAsString();
43+
File(p.setExtension(path, '.expect_short')).readAsStringSync();
44+
var tallOutput = File(p.setExtension(path, '.expect')).readAsStringSync();
4845

4946
return Benchmark(
5047
name: p.basenameWithoutExtension(path),

test/utils.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ Future<void> testFile(String path, {Iterable<StyleFix>? fixes}) async {
142142

143143
/// Format all of the benchmarks and ensure they produce their expected outputs.
144144
Future<void> testBenchmarks({required bool useTallStyle}) async {
145-
var benchmarks = await Benchmark.findAll();
145+
var benchmarks = Benchmark.findAll(await findPackageDirectory());
146146

147147
group('Benchmarks', () {
148148
for (var benchmark in benchmarks) {

0 commit comments

Comments
 (0)