@@ -2,7 +2,6 @@ import 'dart:convert';
22import 'dart:io' ;
33import 'dart:isolate' ;
44import 'dart:math' show min;
5- import 'package:args/args.dart' show ArgParser;
65
76/// Environment declarations are evaluated at compile-time. Use 'const' to
87/// ensure values are baked into AOT/Native binaries for the benchmark.
@@ -14,49 +13,54 @@ import 'package:args/args.dart' show ArgParser;
1413/// but most ahead-of-time compiled platforms will not have this information."
1514const _maxIsolatesfromEnvironment = int .fromEnvironment ('MAX_ISOLATES' );
1615
16+ /// The fixed TCP port used by the server.
17+ /// Defined here for visibility and ease of configuration.
18+ const _defaultPort = 8080 ;
19+
20+ /// A reusable instance of the UTF-8 JSON encoder to efficiently
21+ /// transform Dart objects into byte arrays for HTTP responses.
22+ final _jsonEncoder = JsonUtf8Encoder ();
23+
24+ /// Internal token used to notify newly spawned processes that they
25+ /// belong to a secondary "worker group".
26+ const workerGroupTag = '--workerGroup' ;
27+
1728void main (List <String > args) {
1829 /// Defines local isolate quota, using MAX_ISOLATES if provided.
1930 /// Falls back to total available cores while respecting hardware limits.
2031 var maxIsolates = _maxIsolatesfromEnvironment > 0
2132 ? min (_maxIsolatesfromEnvironment, Platform .numberOfProcessors)
2233 : Platform .numberOfProcessors;
2334
24- /// Triggers process-level horizontal scaling when running in AOT.
25- if (Platform .script.toFilePath ().endsWith ('.aot' )) {
26- /// Internal token used to notify newly spawned processes that they
27- /// belong to a secondary "worker group".
28- const workerGroupTag = '--workerGroup' ;
29-
30- /// Determine if this process instance was initialized as a worker group.
31- final isWorkerGroup = args.contains (workerGroupTag);
35+ /// Determine if this process instance was initialized as a worker group.
36+ final isWorkerGroup = args.contains (workerGroupTag);
3237
33- if (isWorkerGroup) {
34- /// Sanitize the argument list to ensure the internal token does not
35- /// interfere with application-level argument parsing.
36- args.removeAt (args.indexOf (workerGroupTag));
37- }
38- /// Prevents recursive spawning
39- /// by ensuring only the primary process can spawn worker groups
40- else {
41- /// Calculate the number of secondary worker groups required
42- /// to fully utilize the available hardware capacity.
43- ///
44- /// Each group serves as a container for multiple isolates,
45- /// helping to bypass internal VM scaling bottlenecks.
46- final workerGroups = Platform .numberOfProcessors ~ / maxIsolates - 1 ;
47-
48- /// Bootstraps independent worker processes via AOT snapshots.
49- /// Each process initializes its own Isolate Group.
50- for (var i = 0 ; i < workerGroups; i++ ) {
51- /// [Platform.script] identifies the AOT snapshot or executable.
52- /// [Isolate.spawnUri] spawns a new process group via [main()] .
53- Isolate .spawnUri (Platform .script, [...args, workerGroupTag], null );
54- }
55-
56- /// Updates local isolate limits, assigning the primary group
57- /// the remaining cores after worker group allocation.
58- maxIsolates = Platform .numberOfProcessors - workerGroups * maxIsolates;
38+ if (isWorkerGroup) {
39+ /// Sanitize the argument list to ensure the internal token does not
40+ /// interfere with application-level argument parsing.
41+ args.removeAt (args.indexOf (workerGroupTag));
42+ }
43+ /// Prevents recursive spawning
44+ /// by ensuring only the primary process can spawn worker groups
45+ else {
46+ /// Calculate the number of secondary worker groups required
47+ /// to fully utilize the available hardware capacity.
48+ ///
49+ /// Each group serves as a container for multiple isolates,
50+ /// helping to bypass internal VM scaling bottlenecks.
51+ final workerGroups = Platform .numberOfProcessors ~ / maxIsolates - 1 ;
52+
53+ /// Bootstraps independent worker processes via AOT snapshots.
54+ /// Each process initializes its own Isolate Group.
55+ for (var i = 0 ; i < workerGroups; i++ ) {
56+ /// [Platform.script] identifies the AOT snapshot or executable.
57+ /// [Isolate.spawnUri] spawns a new process group via [main()] .
58+ Isolate .spawnUri (Platform .script, [...args, workerGroupTag], null );
5959 }
60+
61+ /// Updates local isolate limits, assigning the primary group
62+ /// the remaining cores after worker group allocation.
63+ maxIsolates = Platform .numberOfProcessors - workerGroups * maxIsolates;
6064 }
6165
6266 /// Create an [Isolate] containing an [HttpServer]
@@ -74,14 +78,14 @@ void _startServer(List<String> args) async {
7478 /// Binds the [HttpServer] on `0.0.0.0:8080` .
7579 final server = await HttpServer .bind (
7680 InternetAddress .anyIPv4,
77- _portParser (args, defaultPort : 8080 ) ,
81+ _defaultPort ,
7882 shared: true ,
7983 );
8084
8185 server
8286 ..defaultResponseHeaders.clear ()
8387 /// Sets [HttpServer] 's [serverHeader] .
84- ..serverHeader = 'dart '
88+ ..serverHeader = 'dart_aot '
8589 /// Handles [HttpRequest] 's from [HttpServer] .
8690 ..listen (_handleRequest);
8791}
@@ -142,18 +146,3 @@ void _plaintextTest(HttpRequest request) => _sendText(
142146 request,
143147 'Hello, World!' ,
144148);
145-
146- final _jsonEncoder = JsonUtf8Encoder ();
147-
148- int _portParser (
149- List <String > args, {
150- required int defaultPort,
151- portTag = 'port' ,
152- }) {
153- final parser = ArgParser ()
154- ..addOption (
155- portTag,
156- defaultsTo: '$defaultPort ' ,
157- );
158- return int .tryParse (parser.parse (args)[portTag]) ?? defaultPort;
159- }
0 commit comments