-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathrepeated_string_json.dart
88 lines (73 loc) · 2.52 KB
/
repeated_string_json.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import '../benchmark.dart';
import '../generated/benchmark.pb.dart'
show BenchmarkID, Params, Request, Sample;
import '../generated/string_grid.pb.dart' as pb;
/// A benchmark that deserializes a grid of string fields.
class RepeatedStringBenchmark extends Benchmark {
final int width;
final int height;
final int stringSize;
late String json;
int? lastFieldTag;
RepeatedStringBenchmark(this.width, this.height, this.stringSize)
: super($id);
@override
String get summary => '${id.name}($width x $height x $stringSize)';
@override
Params makeParams() => Params()
..stringFieldCount = width
..messageCount = height
..stringSize = stringSize;
@override
void setup() {
final grid = _makeGrid(width, height, stringSize);
json = grid.writeToJson();
}
// makes a rectangle of the of the form:
// "01" "12" "23" "34"
// "12" "23" "34" "45"
// "23" "34" "45" "56"
static pb.Grid _makeGrid(int width, int height, int stringSize) {
if (width > 10) throw ArgumentError('width out of range: $width');
final grid = pb.Grid();
final zero = '0'.codeUnits[0];
for (var y = 0; y < height; y++) {
final line = pb.Line();
for (var x = 0; x < width; x++) {
final charCodes = <int>[];
for (var i = 0; i < stringSize; i++) {
charCodes.add(zero + ((x + y + i) % 10));
}
line.cells.add(String.fromCharCodes(charCodes));
}
grid.lines.add(line);
}
return grid;
}
@override
void run() {
final grid = pb.Grid.fromJson(json);
final actual = grid.lines[height - 1].cells[width - 1];
if (actual.length != stringSize) throw 'failed; got $actual';
}
@override
void setCounts(Sample m) {
m.counts.stringReads = width * height * m.loopCount;
}
@override
double measureSample(Sample? s) => stringReadsPerMillisecond(s);
@override
String get measureSampleUnits => 'string reads/ms';
static const $id = BenchmarkID.READ_STRING_REPEATED_JSON;
static final $type = BenchmarkType($id, $create);
static RepeatedStringBenchmark $create(Request r) {
assert(r.params.hasStringFieldCount());
assert(r.params.hasMessageCount());
assert(r.params.hasStringSize());
return RepeatedStringBenchmark(
r.params.stringFieldCount, r.params.messageCount, r.params.stringSize);
}
}