Skip to content

Commit 052f4c6

Browse files
committed
Add simple benchmark
1 parent eb2e8c6 commit 052f4c6

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

dart/benchmark/apply_lines.dart

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import 'dart:io';
2+
import 'dart:typed_data';
3+
4+
import '../test/utils/native_test_utils.dart';
5+
6+
/// Usage: dart run benchmark/apply_lines.dart path/to/lines.bin
7+
///
8+
/// This creates a new in-memory database and applies concatenated BSON sync
9+
/// lines from a file.
10+
void main(List<String> args) {
11+
if (args.length != 1) {
12+
throw 'Usage: dart run benchmark/apply_lines.dart path/to/lines.bin';
13+
}
14+
15+
final [path] = args;
16+
final file = File(path).openSync();
17+
final db = openTestDatabase();
18+
19+
db
20+
..execute('select powersync_init()')
21+
..execute('select powersync_control(?, null)', ['start']);
22+
23+
final stopwatch = Stopwatch()..start();
24+
25+
final lengthBuffer = Uint8List(4);
26+
while (file.positionSync() < file.lengthSync()) {
27+
// BSON document: <int32LE length, ... bytes>
28+
final bytesRead = file.readIntoSync(lengthBuffer);
29+
if (bytesRead != 4) {
30+
throw 'short read, expected length';
31+
}
32+
final length = lengthBuffer.buffer.asByteData().getInt32(0, Endian.little);
33+
file.setPositionSync(file.positionSync() - 4);
34+
35+
final syncLineBson = file.readSync(length);
36+
if (syncLineBson.length != length) {
37+
throw 'short read for bson document';
38+
}
39+
40+
db
41+
..execute('BEGIN')
42+
..execute('SELECT powersync_control(?, ?)', ['line_binary', syncLineBson])
43+
..execute('COMMIT;');
44+
}
45+
46+
stopwatch.stop();
47+
print('Applying $path took ${stopwatch.elapsed}');
48+
}

0 commit comments

Comments
 (0)