runBenchmarks function

Future<int> runBenchmarks(
  1. List<String> paths, {
  2. List<String> targets = const ['aot'],
  3. List<String> arguments = const [],
  4. String outputDir = 'build/microbench',
  5. void log(
    1. String line
    ) = print,
})

Runs every benchmark program in paths (see findBenchmarks) on each of targets, one at a time, and returns 0 if all succeed, else 1 (also when there is no program to run).

A failed step stops that program on that target; the others still run. Output goes straight to this terminal.

Parameters:

  • paths: files and directories.
  • targets: names from runTargets.
  • arguments: passed to every program, for example --quick, both on its command line and in the MICROBENCH_ARGS environment variable, which declared benchmarks in a plain main read.
  • outputDir: where compiled files go.
  • log: receives a heading before each program and a line per failure.

Throws:

Implementation

Future<int> runBenchmarks(
  List<String> paths, {
  List<String> targets = const ['aot'],
  List<String> arguments = const [],
  String outputDir = 'build/microbench',
  void Function(String line) log = print,
}) async {
  for (final target in targets) {
    if (!runTargets.contains(target)) {
      throw ArgumentError.value(target, 'target', 'not one of $runTargets');
    }
  }
  final scripts = findBenchmarks(paths);
  if (scripts.isEmpty) {
    log('No benchmark programs found in ${paths.join(', ')}');
    log(firstBenchmarkHint);
    return 1;
  }
  final plans = [
    for (final script in scripts)
      for (final target in targets)
        [
          script,
          target,
          planRun(script, target, arguments: arguments, outputDir: outputDir)
        ],
  ];
  Directory(outputDir).createSync(recursive: true);
  // For programs whose plain `main` cannot see its arguments.
  final environment = {'MICROBENCH_ARGS': jsonEncode(arguments)};
  var failed = 0;
  for (final plan in plans) {
    log('== ${plan[0]} (${plan[1]}) ==');
    for (final step in plan[2] as List<RunStep>) {
      final path = step.path;
      if (path != null) {
        File(path).writeAsStringSync(step.content!);
        continue;
      }
      late Process process;
      try {
        process = await Process.start(
          step.executable,
          step.arguments,
          environment: environment,
          mode: ProcessStartMode.inheritStdio,
        );
      } on ProcessException catch (error) {
        log('Failed to start $step for ${plan[0]} (${plan[1]}): ${error.message}');
        failed++;
        break;
      }
      final code = await process.exitCode;
      if (code != 0) {
        log('Failed with exit code $code: $step');
        failed++;
        break;
      }
    }
  }
  return failed == 0 ? 0 : 1;
}