|
| 1 | +// Copyright (c) 2024, 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 | + |
| 5 | +import 'dart:io'; |
| 6 | + |
| 7 | +import 'package:indexed_blob/indexed_blob.dart'; |
| 8 | +import 'package:path/path.dart' as p; |
| 9 | +import 'package:pub_dev/shared/utils.dart'; |
| 10 | + |
| 11 | +/// Runs a synthetic benchmark by creating and index file, loading it and running |
| 12 | +/// lookup multiple times. |
| 13 | +/// |
| 14 | +/// Note: these are some non-representative numbers from local benchmarking, for future reference: |
| 15 | +/// 27.826 index bytes/entry. |
| 16 | +/// 0.0647 ms/(load+lookup) |
| 17 | +/// 0.060964 ms/lookup |
| 18 | +Future<void> main() async { |
| 19 | + final index = await withTempDirectory((dir) async { |
| 20 | + final blobFile = File(p.join(dir.path, 'blob.data')); |
| 21 | + final builder = IndexedBlobBuilder(blobFile.openWrite()); |
| 22 | + for (var i = 0; i < 1000; i++) { |
| 23 | + await builder.addFile('doc/$i.html', Stream.value(List.filled(2000, 0))); |
| 24 | + } |
| 25 | + final index = await builder.buildIndex('123'); |
| 26 | + |
| 27 | + final blobIndex = File(p.join(dir.path, 'blob.index')); |
| 28 | + await blobIndex.writeAsBytes(index.asBytes()); |
| 29 | + |
| 30 | + print('${blobIndex.lengthSync() / 1000} index bytes/entry.'); |
| 31 | + |
| 32 | + final bytes = await blobIndex.readAsBytes(); |
| 33 | + final loadSw = Stopwatch()..start(); |
| 34 | + for (var r = 0; r < 10000; r++) { |
| 35 | + final x = BlobIndex.fromBytes(bytes); |
| 36 | + final r = x.lookup('doc/555.html'); |
| 37 | + r!.start; |
| 38 | + x.lookup('doc/555.txt'); |
| 39 | + } |
| 40 | + loadSw.stop(); |
| 41 | + print('${loadSw.elapsedMilliseconds / 10000} ms/(load+lookup)'); |
| 42 | + |
| 43 | + return index; |
| 44 | + }); |
| 45 | + |
| 46 | + final lookupSw = Stopwatch()..start(); |
| 47 | + for (var r = 0; r < 1000; r++) { |
| 48 | + for (var i = 0; i < 1000; i++) { |
| 49 | + index.lookup('doc/$i.html'); |
| 50 | + index.lookup('doc/$i.txt'); |
| 51 | + } |
| 52 | + } |
| 53 | + lookupSw.stop(); |
| 54 | + print('${lookupSw.elapsed.inMilliseconds / 1000 / 1000} ms/lookup'); |
| 55 | +} |
0 commit comments