|
| 1 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:io'; |
| 6 | + |
| 7 | +import 'bench_options.dart'; |
| 8 | + |
| 9 | +// TODO(kevmoo): allow the user to specify custom flags – for compile and/or run |
| 10 | + |
| 11 | +Future<void> compileAndRun(BenchOptions options) async { |
| 12 | + if (!FileSystemEntity.isFileSync(options.target)) { |
| 13 | + throw BenchException( |
| 14 | + 'The target Dart program `${options.target}` does not exist', |
| 15 | + 2, // standard bash code for file doesn't exist |
| 16 | + ); |
| 17 | + } |
| 18 | + |
| 19 | + for (var mode in options.flavor) { |
| 20 | + await _Runner(flavor: mode, target: options.target).run(); |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +class BenchException implements Exception { |
| 25 | + const BenchException(this.message, this.exitCode) : assert(exitCode > 0); |
| 26 | + final String message; |
| 27 | + final int exitCode; |
| 28 | + |
| 29 | + @override |
| 30 | + String toString() => 'BenchException: $message ($exitCode)'; |
| 31 | +} |
| 32 | + |
| 33 | +/// Base name for output files. |
| 34 | +const _outputFileRoot = 'out'; |
| 35 | + |
| 36 | +/// Denote the "stage" of the compile/run step for logging. |
| 37 | +enum _Stage { compile, run } |
| 38 | + |
| 39 | +/// Base class for runtime-specific runners. |
| 40 | +abstract class _Runner { |
| 41 | + _Runner._({required this.target, required this.flavor}) |
| 42 | + : assert(FileSystemEntity.isFileSync(target), '$target is not a file'); |
| 43 | + |
| 44 | + factory _Runner({required RuntimeFlavor flavor, required String target}) { |
| 45 | + return (switch (flavor) { |
| 46 | + RuntimeFlavor.jit => _JITRunner.new, |
| 47 | + RuntimeFlavor.aot => _AOTRunner.new, |
| 48 | + RuntimeFlavor.js => _JSRunner.new, |
| 49 | + RuntimeFlavor.wasm => _WasmRunner.new, |
| 50 | + })(target: target); |
| 51 | + } |
| 52 | + |
| 53 | + final String target; |
| 54 | + final RuntimeFlavor flavor; |
| 55 | + late Directory _tempDirectory; |
| 56 | + |
| 57 | + /// Executes the compile and run cycle. |
| 58 | + /// |
| 59 | + /// Takes care of creating and deleting the corresponding temp directory. |
| 60 | + Future<void> run() async { |
| 61 | + _tempDirectory = Directory.systemTemp |
| 62 | + .createTempSync('bench_${DateTime.now().millisecondsSinceEpoch}_'); |
| 63 | + try { |
| 64 | + await _runImpl(); |
| 65 | + } finally { |
| 66 | + _tempDirectory.deleteSync(recursive: true); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /// Overridden in implementations to handle the compile and run cycle. |
| 71 | + Future<void> _runImpl(); |
| 72 | + |
| 73 | + /// Executes the specific [executable] with the provided [args]. |
| 74 | + /// |
| 75 | + /// Also prints out a nice message before execution denoting the [flavor] and |
| 76 | + /// the [stage]. |
| 77 | + Future<void> _runProc( |
| 78 | + _Stage stage, String executable, List<String> args) async { |
| 79 | + print(''' |
| 80 | +\n${flavor.name.toUpperCase()} - ${stage.name.toUpperCase()} |
| 81 | +$executable ${args.join(' ')} |
| 82 | +'''); |
| 83 | + |
| 84 | + final proc = await Process.start(executable, args, |
| 85 | + mode: ProcessStartMode.inheritStdio); |
| 86 | + |
| 87 | + final exitCode = await proc.exitCode; |
| 88 | + |
| 89 | + if (exitCode != 0) { |
| 90 | + throw ProcessException(executable, args, 'Process errored', exitCode); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + String _outputFile(String ext) => |
| 95 | + _tempDirectory.uri.resolve('$_outputFileRoot.$ext').toFilePath(); |
| 96 | +} |
| 97 | + |
| 98 | +class _JITRunner extends _Runner { |
| 99 | + _JITRunner({required super.target}) : super._(flavor: RuntimeFlavor.jit); |
| 100 | + |
| 101 | + @override |
| 102 | + Future<void> _runImpl() async { |
| 103 | + await _runProc(_Stage.run, Platform.executable, [target]); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +class _AOTRunner extends _Runner { |
| 108 | + _AOTRunner({required super.target}) : super._(flavor: RuntimeFlavor.aot); |
| 109 | + |
| 110 | + @override |
| 111 | + Future<void> _runImpl() async { |
| 112 | + final outFile = _outputFile('exe'); |
| 113 | + await _runProc(_Stage.compile, Platform.executable, [ |
| 114 | + 'compile', |
| 115 | + 'exe', |
| 116 | + target, |
| 117 | + '-o', |
| 118 | + outFile, |
| 119 | + ]); |
| 120 | + |
| 121 | + await _runProc(_Stage.run, outFile, []); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +class _JSRunner extends _Runner { |
| 126 | + _JSRunner({required super.target}) : super._(flavor: RuntimeFlavor.js); |
| 127 | + |
| 128 | + @override |
| 129 | + Future<void> _runImpl() async { |
| 130 | + final outFile = _outputFile('js'); |
| 131 | + await _runProc(_Stage.compile, Platform.executable, [ |
| 132 | + 'compile', |
| 133 | + 'js', |
| 134 | + target, |
| 135 | + '-O4', // default for Flutter |
| 136 | + '-o', |
| 137 | + outFile, |
| 138 | + ]); |
| 139 | + |
| 140 | + await _runProc(_Stage.run, 'node', [outFile]); |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +class _WasmRunner extends _Runner { |
| 145 | + _WasmRunner({required super.target}) : super._(flavor: RuntimeFlavor.wasm); |
| 146 | + |
| 147 | + @override |
| 148 | + Future<void> _runImpl() async { |
| 149 | + final outFile = _outputFile('wasm'); |
| 150 | + await _runProc(_Stage.compile, Platform.executable, [ |
| 151 | + 'compile', |
| 152 | + 'wasm', |
| 153 | + target, |
| 154 | + '-O2', // default for Flutter |
| 155 | + '-o', |
| 156 | + outFile, |
| 157 | + ]); |
| 158 | + |
| 159 | + final jsFile = |
| 160 | + File.fromUri(_tempDirectory.uri.resolve('$_outputFileRoot.js')); |
| 161 | + jsFile.writeAsStringSync(_wasmInvokeScript); |
| 162 | + |
| 163 | + await _runProc(_Stage.run, 'node', [jsFile.path]); |
| 164 | + } |
| 165 | + |
| 166 | + static const _wasmInvokeScript = ''' |
| 167 | +import { readFile } from 'node:fs/promises'; // For async file reading |
| 168 | +import { fileURLToPath } from 'url'; |
| 169 | +import { dirname, join } from 'path'; |
| 170 | +
|
| 171 | +// Get the current directory name |
| 172 | +const __filename = fileURLToPath(import.meta.url); |
| 173 | +const __dirname = dirname(__filename); |
| 174 | +
|
| 175 | +const wasmFilePath = join(__dirname, '$_outputFileRoot.wasm'); |
| 176 | +const wasmBytes = await readFile(wasmFilePath); |
| 177 | +
|
| 178 | +const mjsFilePath = join(__dirname, '$_outputFileRoot.mjs'); |
| 179 | +const dartModule = await import(mjsFilePath); |
| 180 | +const {compile} = dartModule; |
| 181 | +
|
| 182 | +const compiledApp = await compile(wasmBytes); |
| 183 | +const instantiatedApp = await compiledApp.instantiate({}); |
| 184 | +await instantiatedApp.invokeMain(); |
| 185 | +'''; |
| 186 | +} |
0 commit comments