Skip to content

Commit 04b1cca

Browse files
committed
Find available port to run benchmarks
1 parent e551bdf commit 04b1cca

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

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

@@ -268,3 +274,22 @@ String _generateScoreName(
268274
BenchmarkMetric metric,
269275
BenchmarkMetricComputation computation,
270276
) => '${metric.label}.${computation.name}';
277+
278+
Future<int> _findAvailablePort({required int startingAt}) async {
279+
int port = startingAt;
280+
while (!await _isPortAvailable(port)) {
281+
port++;
282+
}
283+
return port;
284+
}
285+
286+
Future<bool> _isPortAvailable(int port) async {
287+
try {
288+
final RawSocket socket = await RawSocket.connect('localhost', port);
289+
socket.shutdown(SocketDirection.both);
290+
await socket.close();
291+
return false;
292+
} on SocketException {
293+
return true;
294+
}
295+
}

0 commit comments

Comments
 (0)