Skip to content

Commit f38d092

Browse files
jensjohaCommit Queue
authored andcommitted
[analyzer] Benchmark can output json with --json
E.g. ``` out/ReleaseX64/dart-sdk/bin/dart \ pkg/analysis_server/tool/benchmark_tools/big_chain_benchmark/lsp_with_plugin_that_times_out.dart \ --types=ImportChain \ --json \ --files=10,20,30 ``` This is preparation for a future CL. Change-Id: Icba35f3ab2fad6b645109800376c1eb454af07db Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/425501 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Jens Johansen <[email protected]>
1 parent d6a684b commit f38d092

File tree

2 files changed

+111
-62
lines changed

2 files changed

+111
-62
lines changed

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

Lines changed: 94 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
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';
56
import 'dart:io';
67

78
import '../language_server_benchmark.dart';
@@ -233,6 +234,7 @@ Future<void> runHelper(
233234
bool includePlugin = false,
234235
}) async {
235236
int verbosity = 0;
237+
bool jsonOutput = false;
236238
for (String arg in args) {
237239
if (arg.startsWith('--files=')) {
238240
numberOfFileOptions =
@@ -249,86 +251,116 @@ Future<void> runHelper(
249251
}
250252
} else if (arg.startsWith('--verbosity=')) {
251253
verbosity = int.parse(arg.substring('--verbosity='.length));
254+
} else if (arg == '--json') {
255+
jsonOutput = true;
252256
}
253257
}
258+
if (jsonOutput) {
259+
verbosity = -1;
260+
}
254261
StringBuffer sb = StringBuffer();
262+
Map<String, int> jsonData = {};
255263
for (CodeType codeType in codeTypes) {
256264
for (int numFiles in numberOfFileOptions) {
257-
Directory tmpDir = Directory.systemTemp.createTempSync('lsp_benchmark');
258265
try {
259-
Directory cacheDir = Directory.fromUri(tmpDir.uri.resolve('cache/'))
260-
..createSync(recursive: true);
261-
Directory dartDir = Directory.fromUri(tmpDir.uri.resolve('dart/'))
262-
..createSync(recursive: true);
263-
var runDetails = copyData(
264-
dartDir.uri,
265-
tmpDir.uri,
266-
numFiles,
267-
codeType,
268-
args,
269-
includePlugin: includePlugin,
270-
);
271-
var benchmark = benchmarkCreator(
272-
args,
273-
dartDir.uri,
274-
cacheDir.uri,
275-
runDetails,
276-
);
266+
Directory tmpDir = Directory.systemTemp.createTempSync('lsp_benchmark');
277267
try {
278-
benchmark.verbosity = verbosity;
279-
await benchmark.run();
280-
} finally {
281-
benchmark.exit();
282-
}
283-
284-
if (verbosity >= 0) print('====================');
285-
if (verbosity >= 0) print('$numFiles files / $codeType:');
286-
sb.writeln('$numFiles files / $codeType:');
287-
for (var durationInfo in benchmark.durationInfo) {
288-
if (verbosity >= 0) {
289-
print(
290-
'${durationInfo.name}: '
291-
'${formatDuration(durationInfo.duration)}',
292-
);
293-
}
294-
sb.writeln(
295-
'${durationInfo.name}: '
296-
'${formatDuration(durationInfo.duration)}',
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,
297279
);
298-
}
299-
for (var memoryInfo in benchmark.memoryInfo) {
300-
if (verbosity >= 0) {
301-
print(
302-
'${memoryInfo.name}: '
303-
'${formatKb(memoryInfo.kb)}',
304-
);
305-
}
306-
sb.writeln(
307-
'${memoryInfo.name}: '
308-
'${formatKb(memoryInfo.kb)}',
280+
var benchmark = benchmarkCreator(
281+
args,
282+
dartDir.uri,
283+
cacheDir.uri,
284+
runDetails,
309285
);
310-
}
311-
if (verbosity >= 0) print('====================');
312-
sb.writeln();
313-
} finally {
314-
try {
315-
tmpDir.deleteSync(recursive: true);
316-
} catch (e) {
317-
// Wait a little and retry.
318-
sleep(const Duration(milliseconds: 42));
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 {
319337
try {
320338
tmpDir.deleteSync(recursive: true);
321339
} catch (e) {
322-
if (verbosity >= 0) print('Warning: $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+
}
323347
}
324348
}
349+
} catch (e) {
350+
stderr.writeln(
351+
'Error while processing $numFiles files / $codeType: $e',
352+
);
325353
}
326354
}
327355
}
328356

329-
print('==================================');
330-
print(sb.toString().trim());
331-
print('==================================');
357+
if (jsonOutput) {
358+
print(json.encode(jsonData));
359+
} else {
360+
print('==================================');
361+
print(sb.toString().trim());
362+
print('==================================');
363+
}
332364
}
333365

334366
enum CodeType {

pkg/analysis_server/tool/benchmark_tools/language_server_benchmark.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ abstract class DartLanguageServerBenchmark {
1717
late final Process p;
1818
late final Timer longRunningRequestsTimer;
1919
bool _launched = false;
20+
bool _exited = false;
2021
Completer<bool> _analyzingCompleter = Completer();
2122
final _buffer = <int>[];
2223
int? _headerContentLength;
@@ -51,6 +52,7 @@ abstract class DartLanguageServerBenchmark {
5152
Future<void> afterInitialization();
5253

5354
void exit() {
55+
_exited = true;
5456
longRunningRequestsTimer.cancel();
5557
if (Platform.isLinux) {
5658
try {
@@ -304,6 +306,21 @@ abstract class DartLanguageServerBenchmark {
304306
]);
305307
}
306308

309+
// ignore: unawaited_futures
310+
p.exitCode.then((exitCode) {
311+
if (verbosity >= 0) {
312+
print('Process existed with exitCode $exitCode');
313+
}
314+
if (!_exited) {
315+
_analyzingCompleter.completeError('Process exited 1.');
316+
while (_outstandingRequestsWithId.isNotEmpty) {
317+
var entry = _outstandingRequestsWithId.entries.first;
318+
_outstandingRequestsWithId.remove(entry.key);
319+
entry.value.completer.completeError('Process exited 2.');
320+
}
321+
}
322+
});
323+
307324
if (verbosity >= 0) {
308325
print('Launched with pid ${p.pid}');
309326
}

0 commit comments

Comments
 (0)