Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ linter:
- directives_ordering
- no_leading_underscores_for_local_identifiers
- omit_local_variable_types
- prefer_final_in_for_each
- prefer_final_locals
- prefer_relative_imports
- prefer_single_quotes
- prefer_spread_collections
Expand Down
24 changes: 12 additions & 12 deletions api_benchmark/lib/benchmark.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ abstract class Benchmark {
{Profiler? profiler}) sync* {
checkRequest(r);

var sampleMillis = r.duration;
final sampleMillis = r.duration;
setup();

for (var i = 0; i < samples; i++) {
Expand All @@ -61,7 +61,7 @@ abstract class Benchmark {

if (profiler != null) {
profiler.startProfile(r);
var s = _measureOnce(sampleMillis);
final s = _measureOnce(sampleMillis);
profiler.endProfile(s);
}

Expand All @@ -84,7 +84,7 @@ abstract class Benchmark {
return 1;
}, 100);

var sample = _measureFor(exercise, sampleMillis);
final sample = _measureFor(exercise, sampleMillis);
setCounts(sample);
return sample;
}
Expand Down Expand Up @@ -121,10 +121,10 @@ abstract class Benchmark {
String summarizeResponse(pb.Response r) {
checkRequest(r.request);

var prefix = summary.padRight(39);
var sampleCount = r.samples.length.toStringAsFixed(0).padLeft(2);
var median = measureSample(medianSample(r)).toStringAsFixed(0).padLeft(4);
var max = measureSample(maxSample(r)).toStringAsFixed(0).padLeft(4);
final prefix = summary.padRight(39);
final sampleCount = r.samples.length.toStringAsFixed(0).padLeft(2);
final median = measureSample(medianSample(r)).toStringAsFixed(0).padLeft(4);
final max = measureSample(maxSample(r)).toStringAsFixed(0).padLeft(4);

return '$prefix samples: $sampleCount'
' median: $median max: $max $measureSampleUnits';
Expand All @@ -133,19 +133,19 @@ abstract class Benchmark {
/// Returns the sample with the median measurement.
pb.Sample? medianSample(pb.Response? response) {
if (response == null || response.samples.isEmpty) return null;
var samples = [...response.samples];
final samples = [...response.samples];
samples.sort((a, b) {
return measureSample(a).compareTo(measureSample(b));
});
var index = samples.length ~/ 2;
final index = samples.length ~/ 2;
return samples[index];
}

/// Returns the sample with the highest measurement.
pb.Sample? maxSample(pb.Response? response) {
if (response == null) return null;
pb.Sample? best;
for (var s in response.samples) {
for (final s in response.samples) {
best ??= s;
if (measureSample(best) < measureSample(s)) {
best = s;
Expand All @@ -166,10 +166,10 @@ abstract class Benchmark {
/// Executes [runner] repeatedly until [minimumMillis] has been reached.
/// [runner] should return the number of times it ran the benchmark.
static pb.Sample _measureFor(int Function() runner, int minimumMillis) {
var minimumMicros = minimumMillis * 1000;
final minimumMicros = minimumMillis * 1000;
var reps = 0;
var elapsed = 0;
var watch = Stopwatch()..start();
final watch = Stopwatch()..start();
while (elapsed < minimumMicros) {
reps += runner();
elapsed = watch.elapsedMicroseconds;
Expand Down
14 changes: 7 additions & 7 deletions api_benchmark/lib/benchmarks/get_strings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ class GetStringsBenchmark extends Benchmark {

@override
String get summary {
var fill = fillValue == null ? 'null' : "'$fillValue'";
final fill = fillValue == null ? 'null' : "'$fillValue'";
return '${id.name}($height x $fill)';
}

@override
Params makeParams() {
var p = Params()..messageCount = height;
final p = Params()..messageCount = height;
if (fillValue != null) p.stringValue = fillValue!;
return p;
}
Expand All @@ -36,13 +36,13 @@ class GetStringsBenchmark extends Benchmark {

// makes a rectangle where every cell has the same value.
static pb.Grid10 _makeGrid(int height, String? fillValue) {
var grid = pb.Grid10();
final grid = pb.Grid10();

for (var y = 0; y < height; y++) {
var line = pb.Line10();
final line = pb.Line10();
if (fillValue != null) {
for (var x = 0; x < width; x++) {
var tag = getTagForColumn(line, x);
final tag = getTagForColumn(line, x);
line.setField(tag, fillValue);
}
}
Expand Down Expand Up @@ -76,7 +76,7 @@ class GetStringsBenchmark extends Benchmark {

void _getDefaults() {
var ok = true;
for (var line in grid.lines) {
for (final line in grid.lines) {
ok = ok && line.cell1.isEmpty;
ok = ok && line.cell2.isEmpty;
ok = ok && line.cell3.isEmpty;
Expand All @@ -93,7 +93,7 @@ class GetStringsBenchmark extends Benchmark {

void _getStrings() {
var ok = true;
for (var line in grid.lines) {
for (final line in grid.lines) {
ok = ok && line.cell1.isNotEmpty;
ok = ok && line.cell2.isNotEmpty;
ok = ok && line.cell3.isNotEmpty;
Expand Down
14 changes: 7 additions & 7 deletions api_benchmark/lib/benchmarks/has_strings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ class HasStringsBenchmark extends Benchmark {

@override
String get summary {
var fill = fillValue == null ? 'null' : "'$fillValue'";
final fill = fillValue == null ? 'null' : "'$fillValue'";
return '${id.name}($height x $fill)';
}

@override
Params makeParams() {
var p = Params()..messageCount = height;
final p = Params()..messageCount = height;
if (fillValue != null) p.stringValue = fillValue!;
return p;
}
Expand All @@ -36,13 +36,13 @@ class HasStringsBenchmark extends Benchmark {

// makes a rectangle where no fields have been set.
static pb.Grid10 _makeGrid(int width, int height, String? fillValue) {
var grid = pb.Grid10();
final grid = pb.Grid10();

for (var y = 0; y < height; y++) {
var line = pb.Line10();
final line = pb.Line10();
if (fillValue != null) {
for (var x = 0; x < width; x++) {
var tag = getTagForColumn(line, x);
final tag = getTagForColumn(line, x);
line.setField(tag, fillValue);
}
}
Expand Down Expand Up @@ -76,7 +76,7 @@ class HasStringsBenchmark extends Benchmark {

void runFilled() {
var allPresent = true;
for (var line in grid.lines) {
for (final line in grid.lines) {
allPresent = allPresent && line.hasCell1();
allPresent = allPresent && line.hasCell2();
allPresent = allPresent && line.hasCell3();
Expand All @@ -93,7 +93,7 @@ class HasStringsBenchmark extends Benchmark {

void runEmpty() {
var allEmpty = true;
for (var line in grid.lines) {
for (final line in grid.lines) {
allEmpty = allEmpty && !line.hasCell1();
allEmpty = allEmpty && !line.hasCell2();
allEmpty = allEmpty && !line.hasCell3();
Expand Down
6 changes: 3 additions & 3 deletions api_benchmark/lib/benchmarks/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import 'string_json.dart';

/// Creates the appropriate Benchmark instance for a protobuf.
Benchmark createBenchmark(pb.Request r) {
var type = allBenchmarks[r.id];
final type = allBenchmarks[r.id];
if (type == null) {
throw ArgumentError('unknown benchmark: ${r.id.name}');
}
Expand All @@ -36,8 +36,8 @@ final Map<pb.BenchmarkID, BenchmarkType> allBenchmarks = _makeTypeMap([
]);

Map<pb.BenchmarkID, BenchmarkType> _makeTypeMap(List<BenchmarkType> types) {
var out = <pb.BenchmarkID, BenchmarkType>{};
for (var type in types) {
final out = <pb.BenchmarkID, BenchmarkType>{};
for (final type in types) {
if (out.containsKey(type.id)) {
throw 'already added: $type.id.name';
}
Expand Down
12 changes: 6 additions & 6 deletions api_benchmark/lib/benchmarks/int32_json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Int32Benchmark extends Benchmark {

@override
void setup() {
var grid = _makeGrid(width, height);
final grid = _makeGrid(width, height);
json = grid.writeToJson();
lastFieldTag = getTagForColumn(pb.Line10(), width - 1);
}
Expand All @@ -37,12 +37,12 @@ class Int32Benchmark extends Benchmark {
// 2 3 4 5
static pb.Grid10 _makeGrid(int width, int height) {
if (width > 10) throw ArgumentError('width out of range: $width');
var grid = pb.Grid10();
final grid = pb.Grid10();

for (var y = 0; y < height; y++) {
var line = pb.Line10();
final line = pb.Line10();
for (var x = 0; x < width; x++) {
var tag = getTagForColumn(line, x)!;
final tag = getTagForColumn(line, x)!;
line.setField(tag, x + y);
}
grid.lines.add(line);
Expand All @@ -57,8 +57,8 @@ class Int32Benchmark extends Benchmark {

@override
void run() {
var grid = pb.Grid10.fromJson(json);
var actual = grid.lines[height - 1].getField(lastFieldTag!);
final grid = pb.Grid10.fromJson(json);
final actual = grid.lines[height - 1].getField(lastFieldTag!);
if (actual != width + height - 2) throw 'failed; got $actual';
}

Expand Down
12 changes: 6 additions & 6 deletions api_benchmark/lib/benchmarks/int64_json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Int64Benchmark extends Benchmark {

@override
void setup() {
var grid = _makeGrid(width, height);
final grid = _makeGrid(width, height);
json = grid.writeToJson();
lastFieldTag = getTagForColumn(pb.Line10(), width - 1);
}
Expand All @@ -39,12 +39,12 @@ class Int64Benchmark extends Benchmark {
// 2 3 4 5
static pb.Grid10 _makeGrid(int width, int height) {
if (width > 10) throw ArgumentError('width out of range: $width');
var grid = pb.Grid10();
final grid = pb.Grid10();

for (var y = 0; y < height; y++) {
var line = pb.Line10();
final line = pb.Line10();
for (var x = 0; x < width; x++) {
var tag = getTagForColumn(line, x)!;
final tag = getTagForColumn(line, x)!;
line.setField(tag, Int64(x + y));
}
grid.lines.add(line);
Expand All @@ -59,8 +59,8 @@ class Int64Benchmark extends Benchmark {

@override
void run() {
var grid = pb.Grid10.fromJson(json);
var actual = grid.lines[height - 1].getField(lastFieldTag!);
final grid = pb.Grid10.fromJson(json);
final actual = grid.lines[height - 1].getField(lastFieldTag!);
if (actual != width + height - 2) throw 'failed; got $actual';
}

Expand Down
10 changes: 5 additions & 5 deletions api_benchmark/lib/benchmarks/repeated_int32_json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class RepeatedInt32Benchmark extends Benchmark {

@override
void setup() {
var grid = _makeGrid(width, height);
final grid = _makeGrid(width, height);
json = grid.writeToJson();
}

Expand All @@ -34,10 +34,10 @@ class RepeatedInt32Benchmark extends Benchmark {
// 1 2 3 4
// 2 3 4 5
static pb.Grid _makeGrid(int width, int height) {
var grid = pb.Grid();
final grid = pb.Grid();

for (var y = 0; y < height; y++) {
var line = pb.Line();
final line = pb.Line();
for (var x = 0; x < width; x++) {
line.cells.add(x + y);
}
Expand All @@ -49,8 +49,8 @@ class RepeatedInt32Benchmark extends Benchmark {

@override
void run() {
var grid = pb.Grid.fromJson(json);
var actual = grid.lines[height - 1].cells[width - 1];
final grid = pb.Grid.fromJson(json);
final actual = grid.lines[height - 1].cells[width - 1];
if (actual != width + height - 2) throw 'failed; got $actual';
}

Expand Down
10 changes: 5 additions & 5 deletions api_benchmark/lib/benchmarks/repeated_int64_json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class RepeatedInt64Benchmark extends Benchmark {

@override
void setup() {
var grid = _makeGrid(width, height);
final grid = _makeGrid(width, height);
json = grid.writeToJson();
}

Expand All @@ -36,10 +36,10 @@ class RepeatedInt64Benchmark extends Benchmark {
// 1 2 3 4
// 2 3 4 5
static pb.Grid _makeGrid(int width, int height) {
var grid = pb.Grid();
final grid = pb.Grid();

for (var y = 0; y < height; y++) {
var line = pb.Line();
final line = pb.Line();
for (var x = 0; x < width; x++) {
line.cells.add(Int64(x + y));
}
Expand All @@ -51,8 +51,8 @@ class RepeatedInt64Benchmark extends Benchmark {

@override
void run() {
var grid = pb.Grid.fromJson(json);
var actual = grid.lines[height - 1].cells[width - 1];
final grid = pb.Grid.fromJson(json);
final actual = grid.lines[height - 1].cells[width - 1];
if (actual != width + height - 2) throw 'failed; got $actual';
}

Expand Down
14 changes: 7 additions & 7 deletions api_benchmark/lib/benchmarks/repeated_string_json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class RepeatedStringBenchmark extends Benchmark {

@override
void setup() {
var grid = _makeGrid(width, height, stringSize);
final grid = _makeGrid(width, height, stringSize);
json = grid.writeToJson();
}

Expand All @@ -39,14 +39,14 @@ class RepeatedStringBenchmark extends Benchmark {
// "23" "34" "45" "56"
static pb.Grid _makeGrid(int width, int height, int stringSize) {
if (width > 10) throw ArgumentError('width out of range: $width');
var grid = pb.Grid();
final grid = pb.Grid();

var zero = '0'.codeUnits[0];
final zero = '0'.codeUnits[0];

for (var y = 0; y < height; y++) {
var line = pb.Line();
final line = pb.Line();
for (var x = 0; x < width; x++) {
var charCodes = <int>[];
final charCodes = <int>[];
for (var i = 0; i < stringSize; i++) {
charCodes.add(zero + ((x + y + i) % 10));
}
Expand All @@ -59,8 +59,8 @@ class RepeatedStringBenchmark extends Benchmark {

@override
void run() {
var grid = pb.Grid.fromJson(json);
var actual = grid.lines[height - 1].cells[width - 1];
final grid = pb.Grid.fromJson(json);
final actual = grid.lines[height - 1].cells[width - 1];
if (actual.length != stringSize) throw 'failed; got $actual';
}

Expand Down
Loading