Skip to content

Commit 7d82418

Browse files
jensjohaCommit Queue
authored andcommitted
[analyzer] Refactor benchmarks to make it easier to add new benchmarks based on different sources
Change-Id: I7e6f70c62faad55619de6677100608f406091a5e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/425502 Commit-Queue: Jens Johansen <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent f38d092 commit 7d82418

9 files changed

+216
-165
lines changed

pkg/analysis_server/tool/benchmark_tools/big_chain_benchmark/utils.dart renamed to pkg/analysis_server/tool/benchmark_tools/big_chain_benchmark/benchmark_utils.dart

Lines changed: 11 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'dart:convert';
65
import 'dart:io';
76

8-
import '../language_server_benchmark.dart';
97
import '../utils.dart';
108

119
RunDetails copyData(
1210
Uri packageDirUri,
1311
Uri outerDirForAdditionalData,
1412
int numFiles,
15-
CodeType copyType,
13+
CodeType? copyType,
1614
List<String> args, {
17-
required bool includePlugin,
15+
required ({bool includePlugin})? extraInformation,
1816
}) {
17+
bool includePlugin = extraInformation?.includePlugin ?? false;
18+
if (copyType == null) throw 'Unsupported.';
1919
Uri filesUri = Platform.script.resolve('files/');
2020
Uri libDirUri = packageDirUri.resolve('lib/');
2121
Directory.fromUri(libDirUri).createSync();
@@ -201,45 +201,10 @@ analyzer:
201201
);
202202
}
203203

204-
String formatDuration(Duration duration) {
205-
int seconds = duration.inSeconds;
206-
int ms = duration.inMicroseconds - seconds * Duration.microsecondsPerSecond;
207-
return '$seconds.${ms.toString().padLeft(6, '0')}';
208-
}
209-
210-
String formatKb(int kb) {
211-
if (kb > 1024) {
212-
return '${kb ~/ 1024} MB';
213-
} else {
214-
return '$kb KB';
215-
}
216-
}
217-
218-
String getFilenameFor(int i) {
219-
return "file${i.toString().padLeft(5, '0')}.dart";
220-
}
221-
222-
Future<void> runHelper(
223-
List<String> args,
224-
DartLanguageServerBenchmark Function(
225-
List<String> args,
226-
Uri rootUri,
227-
Uri cacheFolder,
228-
RunDetails runDetails,
229-
)
230-
benchmarkCreator, {
231-
required bool runAsLsp,
232-
List<int> numberOfFileOptions = const [16, 32, 64, 128, 256, 512, 1024],
233-
List<CodeType> codeTypes = CodeType.values,
234-
bool includePlugin = false,
235-
}) async {
236-
int verbosity = 0;
237-
bool jsonOutput = false;
204+
List<CodeType?> getExtraIterations(List<String> args) {
205+
var codeTypes = CodeType.values;
238206
for (String arg in args) {
239-
if (arg.startsWith('--files=')) {
240-
numberOfFileOptions =
241-
arg.substring('--files='.length).split(',').map(int.parse).toList();
242-
} else if (arg.startsWith('--types=')) {
207+
if (arg.startsWith('--types=')) {
243208
codeTypes = [];
244209
for (var type in arg.substring('--types='.length).split(',')) {
245210
type = type.toLowerCase();
@@ -249,118 +214,13 @@ Future<void> runHelper(
249214
}
250215
}
251216
}
252-
} else if (arg.startsWith('--verbosity=')) {
253-
verbosity = int.parse(arg.substring('--verbosity='.length));
254-
} else if (arg == '--json') {
255-
jsonOutput = true;
256-
}
257-
}
258-
if (jsonOutput) {
259-
verbosity = -1;
260-
}
261-
StringBuffer sb = StringBuffer();
262-
Map<String, int> jsonData = {};
263-
for (CodeType codeType in codeTypes) {
264-
for (int numFiles in numberOfFileOptions) {
265-
try {
266-
Directory tmpDir = Directory.systemTemp.createTempSync('lsp_benchmark');
267-
try {
268-
Directory cacheDir = Directory.fromUri(tmpDir.uri.resolve('cache/'))
269-
..createSync(recursive: true);
270-
Directory dartDir = Directory.fromUri(tmpDir.uri.resolve('dart/'))
271-
..createSync(recursive: true);
272-
var runDetails = copyData(
273-
dartDir.uri,
274-
tmpDir.uri,
275-
numFiles,
276-
codeType,
277-
args,
278-
includePlugin: includePlugin,
279-
);
280-
var benchmark = benchmarkCreator(
281-
args,
282-
dartDir.uri,
283-
cacheDir.uri,
284-
runDetails,
285-
);
286-
try {
287-
benchmark.verbosity = verbosity;
288-
await benchmark.run();
289-
} finally {
290-
benchmark.exit();
291-
}
292-
293-
var caption = '$numFiles files / $codeType';
294-
if (jsonOutput) {
295-
for (var durationInfo in benchmark.durationInfo) {
296-
var key = '$caption: ${durationInfo.name} (ms)';
297-
if (jsonData.containsKey(key)) {
298-
throw 'Already contains data for $key';
299-
}
300-
jsonData[key] = durationInfo.duration.inMilliseconds;
301-
}
302-
for (var memoryInfo in benchmark.memoryInfo) {
303-
jsonData['$caption: ${memoryInfo.name} (kb)'] = memoryInfo.kb;
304-
}
305-
} else {
306-
if (verbosity >= 0) print('====================');
307-
if (verbosity >= 0) print('$caption:');
308-
sb.writeln('$caption:');
309-
for (var durationInfo in benchmark.durationInfo) {
310-
if (verbosity >= 0) {
311-
print(
312-
'${durationInfo.name}: '
313-
'${formatDuration(durationInfo.duration)}',
314-
);
315-
}
316-
sb.writeln(
317-
'${durationInfo.name}: '
318-
'${formatDuration(durationInfo.duration)}',
319-
);
320-
}
321-
for (var memoryInfo in benchmark.memoryInfo) {
322-
if (verbosity >= 0) {
323-
print(
324-
'${memoryInfo.name}: '
325-
'${formatKb(memoryInfo.kb)}',
326-
);
327-
}
328-
sb.writeln(
329-
'${memoryInfo.name}: '
330-
'${formatKb(memoryInfo.kb)}',
331-
);
332-
}
333-
if (verbosity >= 0) print('====================');
334-
sb.writeln();
335-
}
336-
} finally {
337-
try {
338-
tmpDir.deleteSync(recursive: true);
339-
} catch (e) {
340-
// Wait a little and retry.
341-
sleep(const Duration(milliseconds: 42));
342-
try {
343-
tmpDir.deleteSync(recursive: true);
344-
} catch (e) {
345-
if (verbosity >= 0) print('Warning: $e');
346-
}
347-
}
348-
}
349-
} catch (e) {
350-
stderr.writeln(
351-
'Error while processing $numFiles files / $codeType: $e',
352-
);
353-
}
354217
}
355218
}
219+
return codeTypes;
220+
}
356221

357-
if (jsonOutput) {
358-
print(json.encode(jsonData));
359-
} else {
360-
print('==================================');
361-
print(sb.toString().trim());
362-
print('==================================');
363-
}
222+
String getFilenameFor(int i) {
223+
return "file${i.toString().padLeft(5, '0')}.dart";
364224
}
365225

366226
enum CodeType {

pkg/analysis_server/tool/benchmark_tools/big_chain_benchmark/legacy_many_files_in_flutter_set_subscriptions.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
import '../language_server_benchmark.dart';
66
import '../legacy_messages.dart';
7-
import 'utils.dart';
7+
import '../run_utils.dart';
8+
import 'benchmark_utils.dart';
89

910
/// At least until "https://github.com/flutter/flutter-intellij/issues/7980" is
1011
/// fixed, when a user in IntelliJ in a Flutter project opens a file it's added
@@ -26,6 +27,8 @@ Future<void> main(List<String> args) async {
2627
await runHelper(
2728
args,
2829
LegacyManyFilesInFlutterSetSubscriptionsBenchmark.new,
30+
copyData,
31+
extraIterations: getExtraIterations,
2932
runAsLsp: false,
3033
);
3134
}

pkg/analysis_server/tool/benchmark_tools/big_chain_benchmark/legacy_many_get_fixes_and_get_assists_requests.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
import '../language_server_benchmark.dart';
66
import '../legacy_messages.dart';
7-
import 'utils.dart';
7+
import '../run_utils.dart';
8+
import 'benchmark_utils.dart';
89

910
/// In reports of the analyzer being slow we've seen `edit.getFixes` causing a
1011
/// long queue because they take longer to execute than the wait before the next
@@ -19,9 +20,11 @@ Future<void> main(List<String> args) async {
1920
await runHelper(
2021
args,
2122
LegacyManyGetFixesAndGetAssisstRequestsBenchmark.new,
23+
copyData,
24+
extraIterations: getExtraIterations,
2225
runAsLsp: false,
2326
// The number of files doesn't seem to be important on this one.
24-
numberOfFileOptions: [4],
27+
sizeOptions: [4],
2528
);
2629
}
2730

pkg/analysis_server/tool/benchmark_tools/big_chain_benchmark/legacy_many_hover_requests.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
import '../language_server_benchmark.dart';
66
import '../legacy_messages.dart';
7-
import 'utils.dart';
7+
import '../run_utils.dart';
8+
import 'benchmark_utils.dart';
89

910
/// Hovering over an import with ctrl down in IntelliJ sends getHover requests
1011
/// for the import uri at position 0 every ~8 ms and never cancels old requests.
@@ -15,9 +16,11 @@ Future<void> main(List<String> args) async {
1516
await runHelper(
1617
args,
1718
LegacyManyHoverRequestsBenchmark.new,
19+
copyData,
20+
extraIterations: getExtraIterations,
1821
runAsLsp: false,
1922
// The number of files doesn't seem to be important on this one.
20-
numberOfFileOptions: [4],
23+
sizeOptions: [4],
2124
);
2225
}
2326

pkg/analysis_server/tool/benchmark_tools/big_chain_benchmark/legacy_typing_temporary_missing_end_brace.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
import '../language_server_benchmark.dart';
66
import '../legacy_messages.dart';
7-
import 'utils.dart';
7+
import '../run_utils.dart';
8+
import 'benchmark_utils.dart';
89

910
/// In IntelliJ, typing `if (something) {` only automatically inserts the
1011
/// matching end brace `}` when hitting enter. This makes it "not unlikely"
@@ -20,6 +21,8 @@ Future<void> main(List<String> args) async {
2021
await runHelper(
2122
args,
2223
LegacyTypingTemporaryMissingEndBraceBenchmark.new,
24+
copyData,
25+
extraIterations: getExtraIterations,
2326
runAsLsp: false,
2427
);
2528
}

pkg/analysis_server/tool/benchmark_tools/big_chain_benchmark/legacy_with_plugin_that_times_out.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
import '../language_server_benchmark.dart';
66
import '../legacy_messages.dart';
7-
import 'utils.dart';
7+
import '../run_utils.dart';
8+
import 'benchmark_utils.dart';
89

910
/// We've observed that sometimes the plugin that users has installed times out
1011
/// (takes > 500 ms to answer). This benchmark simulates that and shows the
@@ -13,10 +14,12 @@ Future<void> main(List<String> args) async {
1314
await runHelper(
1415
args,
1516
LegacyWithPluginThatTimesOutBencmark.new,
17+
copyData,
18+
extraIterations: getExtraIterations,
1619
runAsLsp: false,
17-
includePlugin: true,
20+
extraInformation: (includePlugin: true),
1821
// The number of files isn't important here.
19-
numberOfFileOptions: [10],
22+
sizeOptions: [10],
2023
);
2124
}
2225

pkg/analysis_server/tool/benchmark_tools/big_chain_benchmark/lsp_completion_after_change.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,20 @@
44

55
import '../language_server_benchmark.dart';
66
import '../lsp_messages.dart';
7-
import 'utils.dart';
7+
import '../run_utils.dart';
8+
import 'benchmark_utils.dart';
89

910
/// Change a file in a big project and reports how long it takes before the
1011
/// analysis server is responsive again (measured by when it responds to a
1112
/// completion request) and when it's actually done analysing.
1213
Future<void> main(List<String> args) async {
13-
await runHelper(args, LspCompletionAfterChange.new, runAsLsp: true);
14+
await runHelper(
15+
args,
16+
LspCompletionAfterChange.new,
17+
copyData,
18+
extraIterations: getExtraIterations,
19+
runAsLsp: true,
20+
);
1421
}
1522

1623
class LspCompletionAfterChange extends DartLanguageServerBenchmark {

pkg/analysis_server/tool/benchmark_tools/big_chain_benchmark/lsp_with_plugin_that_times_out.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
import '../language_server_benchmark.dart';
66
import '../lsp_messages.dart';
7-
import 'utils.dart';
7+
import '../run_utils.dart';
8+
import 'benchmark_utils.dart';
89

910
/// We've observed that sometimes the plugin that users has installed times out
1011
/// (takes > 500 ms to answer). This benchmark simulates that and shows the
@@ -13,10 +14,12 @@ Future<void> main(List<String> args) async {
1314
await runHelper(
1415
args,
1516
LspWithPluginThatTimesOutBencmark.new,
17+
copyData,
18+
extraIterations: getExtraIterations,
1619
runAsLsp: true,
17-
includePlugin: true,
20+
extraInformation: (includePlugin: true),
1821
// The number of files isn't important here.
19-
numberOfFileOptions: [10],
22+
sizeOptions: [10],
2023
);
2124
}
2225

0 commit comments

Comments
 (0)