Skip to content

Commit e25288f

Browse files
authored
Find available port to run benchmarks (#9315)
Instead of using a hardcoded port number to run benchmarks, let's dynamically find an available port. Fixes #9314 This is not really a fix, but more of a workaround until the underlying `package:test` issue dart-lang/test#897 is resolved.
1 parent 1be93f6 commit e25288f

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

packages/devtools_app/benchmark/devtools_benchmarks_test.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ void main() {
5050

5151
Future<void> _runBenchmarks({bool useWasm = false}) async {
5252
stdout.writeln('Starting web benchmark tests ...');
53+
54+
final benchmarkPort = await _findAvailablePort(startingAt: 9999);
55+
final chromePort = await _findAvailablePort(startingAt: benchmarkPort + 1);
56+
5357
final taskResult = await serveWebBenchmark(
5458
benchmarkAppDirectory: projectRootDirectory(),
5559
entryPoint: generateBenchmarkEntryPoint(useWasm: useWasm),
@@ -58,6 +62,8 @@ Future<void> _runBenchmarks({bool useWasm = false}) async {
5862
: const CompilationOptions.js(),
5963
treeShakeIcons: false,
6064
benchmarkPath: benchmarkPath(useWasm: useWasm),
65+
benchmarkServerPort: benchmarkPort,
66+
chromeDebugPort: chromePort,
6167
);
6268
stdout.writeln('Web benchmark tests finished.');
6369

@@ -255,3 +261,22 @@ String _generateScoreName(
255261
BenchmarkMetric metric,
256262
BenchmarkMetricComputation computation,
257263
) => '${metric.label}.${computation.name}';
264+
265+
Future<int> _findAvailablePort({required int startingAt}) async {
266+
int port = startingAt;
267+
while (!await _isPortAvailable(port)) {
268+
port++;
269+
}
270+
return port;
271+
}
272+
273+
Future<bool> _isPortAvailable(int port) async {
274+
try {
275+
final RawSocket socket = await RawSocket.connect('localhost', port);
276+
socket.shutdown(SocketDirection.both);
277+
await socket.close();
278+
return false;
279+
} on SocketException {
280+
return true;
281+
}
282+
}

0 commit comments

Comments
 (0)