-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mini-benchmark for indexed blob. (#8440)
- Loading branch information
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright (c) 2024, 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 'dart:io'; | ||
|
||
import 'package:indexed_blob/indexed_blob.dart'; | ||
import 'package:path/path.dart' as p; | ||
import 'package:pub_dev/shared/utils.dart'; | ||
|
||
/// Runs a synthetic benchmark by creating and index file, loading it and running | ||
/// lookup multiple times. | ||
/// | ||
/// Note: these are some non-representative numbers from local benchmarking, for future reference: | ||
/// 27.826 index bytes/entry. | ||
/// 0.0647 ms/(load+lookup) | ||
/// 0.060964 ms/lookup | ||
Future<void> main() async { | ||
final index = await withTempDirectory((dir) async { | ||
final blobFile = File(p.join(dir.path, 'blob.data')); | ||
final builder = IndexedBlobBuilder(blobFile.openWrite()); | ||
for (var i = 0; i < 1000; i++) { | ||
await builder.addFile('doc/$i.html', Stream.value(List.filled(2000, 0))); | ||
} | ||
final index = await builder.buildIndex('123'); | ||
|
||
final blobIndex = File(p.join(dir.path, 'blob.index')); | ||
await blobIndex.writeAsBytes(index.asBytes()); | ||
|
||
print('${blobIndex.lengthSync() / 1000} index bytes/entry.'); | ||
|
||
final bytes = await blobIndex.readAsBytes(); | ||
final loadSw = Stopwatch()..start(); | ||
for (var r = 0; r < 10000; r++) { | ||
final x = BlobIndex.fromBytes(bytes); | ||
final r = x.lookup('doc/555.html'); | ||
r!.start; | ||
x.lookup('doc/555.txt'); | ||
} | ||
loadSw.stop(); | ||
print('${loadSw.elapsedMilliseconds / 10000} ms/(load+lookup)'); | ||
|
||
return index; | ||
}); | ||
|
||
final lookupSw = Stopwatch()..start(); | ||
for (var r = 0; r < 1000; r++) { | ||
for (var i = 0; i < 1000; i++) { | ||
index.lookup('doc/$i.html'); | ||
index.lookup('doc/$i.txt'); | ||
} | ||
} | ||
lookupSw.stop(); | ||
print('${lookupSw.elapsed.inMilliseconds / 1000 / 1000} ms/lookup'); | ||
} |