Skip to content

Commit e8da101

Browse files
committed
Benchmark tool
1 parent 2c55514 commit e8da101

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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:convert';
6+
import 'dart:io';
7+
8+
import 'package:pub_dev/package/overrides.dart';
9+
import 'package:pub_dev/search/mem_index.dart';
10+
import 'package:pub_dev/search/models.dart';
11+
import 'package:pub_dev/search/search_service.dart';
12+
13+
/// Loads a search snapshot and executes queries on it, benchmarking their total time to complete.
14+
Future<void> main(List<String> args) async {
15+
// Assumes that the first argument is a search snapshot file.
16+
final file = File(args.first);
17+
final content =
18+
json.decode(utf8.decode(gzip.decode(await file.readAsBytes())))
19+
as Map<String, Object?>;
20+
final snapshot = SearchSnapshot.fromJson(content);
21+
snapshot.documents!
22+
.removeWhere((packageName, doc) => isSoftRemoved(packageName));
23+
final index = InMemoryPackageIndex(documents: snapshot.documents!.values);
24+
25+
// NOTE: please add more queries to this list, especially if there is a performance bottleneck.
26+
final queries = [
27+
'json',
28+
'camera',
29+
'android camera',
30+
'sql database',
31+
];
32+
33+
final sw = Stopwatch()..start();
34+
var count = 0;
35+
for (var i = 0; i < 100; i++) {
36+
index.search(ServiceSearchQuery.parse(query: queries[i % queries.length]));
37+
count++;
38+
}
39+
sw.stop();
40+
print('${(sw.elapsedMilliseconds / count).toStringAsFixed(2)} ms/request');
41+
}

0 commit comments

Comments
 (0)