Add A/B test mode to local devicelab runner (#54494)

* Add A/B test mode to local devicelab runner
This commit is contained in:
Yegor
2020-04-10 16:53:52 -07:00
committed by GitHub
parent 03c566768b
commit d119e5f1e4
3 changed files with 285 additions and 6 deletions

View File

@@ -9,18 +9,34 @@ import 'dart:io';
import 'package:args/args.dart';
import 'package:path/path.dart' as path;
import 'package:flutter_devicelab/framework/ab.dart';
import 'package:flutter_devicelab/framework/manifest.dart';
import 'package:flutter_devicelab/framework/runner.dart';
import 'package:flutter_devicelab/framework/utils.dart';
ArgResults args;
List<String> _taskNames = <String>[];
/// Suppresses standard output, prints only standard error output.
bool silent;
/// The build of the local engine to use.
///
/// Required for A/B test mode.
String localEngine;
/// The path to the engine "src/" directory.
String localEngineSrcPath;
/// Whether to exit on first test failure.
bool exitOnFirstTestFailure;
/// Runs tasks.
///
/// The tasks are chosen depending on the command-line options
/// (see [_argParser]).
Future<void> main(List<String> rawArgs) async {
ArgResults args;
try {
args = _argParser.parse(rawArgs);
} on FormatException catch (error) {
@@ -55,10 +71,19 @@ Future<void> main(List<String> rawArgs) async {
return;
}
final bool silent = args['silent'] as bool;
final String localEngine = args['local-engine'] as String;
final String localEngineSrcPath = args['local-engine-src-path'] as String;
silent = args['silent'] as bool;
localEngine = args['local-engine'] as String;
localEngineSrcPath = args['local-engine-src-path'] as String;
exitOnFirstTestFailure = args['exit'] as bool;
if (args.wasParsed('ab')) {
await _runABTest();
} else {
await _runTasks();
}
}
Future<void> _runTasks() async {
for (final String taskName in _taskNames) {
section('Running task "$taskName"');
final Map<String, dynamic> result = await runTask(
@@ -74,13 +99,73 @@ Future<void> main(List<String> rawArgs) async {
if (!(result['success'] as bool)) {
exitCode = 1;
if (args['exit'] as bool) {
if (exitOnFirstTestFailure) {
return;
}
}
}
}
Future<void> _runABTest() async {
final int runsPerTest = int.parse(args['ab'] as String);
if (_taskNames.length > 1) {
stderr.writeln('When running in A/B test mode exactly one task must be passed but got ${_taskNames.join(', ')}.\n');
stderr.writeln(_argParser.usage);
exit(1);
}
if (!args.wasParsed('local-engine')) {
stderr.writeln('When running in A/B test mode --local-engine is required.\n');
stderr.writeln(_argParser.usage);
exit(1);
}
final String taskName = _taskNames.single;
print('$taskName A/B test. Will run $runsPerTest times.');
final ABTest abTest = ABTest();
for (int i = 1; i <= runsPerTest; i++) {
section('Run #$i');
print('Running with the default engine (A)');
final Map<String, dynamic> defaultEngineResult = await runTask(
taskName,
silent: silent,
);
print('Default engine result:');
print(const JsonEncoder.withIndent(' ').convert(defaultEngineResult));
if (!(defaultEngineResult['success'] as bool)) {
stderr.writeln('Task failed on the default engine.');
exit(1);
}
abTest.addAResult(defaultEngineResult);
print('Running with the local engine (B)');
final Map<String, dynamic> localEngineResult = await runTask(
taskName,
silent: silent,
localEngine: localEngine,
localEngineSrcPath: localEngineSrcPath,
);
print('Task localEngineResult:');
print(const JsonEncoder.withIndent(' ').convert(localEngineResult));
if (!(localEngineResult['success'] as bool)) {
stderr.writeln('Task failed on the local engine.');
exit(1);
}
abTest.addBResult(localEngineResult);
}
print(abTest.printSummary());
}
void addTasks({
List<ManifestTask> tasks,
ArgResults args,
@@ -132,6 +217,22 @@ final ArgParser _argParser = ArgParser()
}
},
)
..addOption(
'ab',
help: 'Runs an A/B test comparing the default engine with the local\n'
'engine build for one task. This option does not support running\n'
'multiple tasks. The value is the number of times to run the task.\n'
'The task is expected to be a benchmark that reports score keys.\n'
'The A/B test collects the metrics collected by the test and\n'
'produces a report containing averages, noise, and the speed-up\n'
'between the two engines. --local-engine is required when running\n'
'an A/B test.',
callback: (String value) {
if (value != null && int.tryParse(value) == null) {
throw ArgParserException('Option --ab must be a number, but was "$value".');
}
},
)
..addFlag(
'all',
abbr: 'a',
@@ -152,7 +253,8 @@ final ArgParser _argParser = ArgParser()
help: 'Name of a build output within the engine out directory, if you\n'
'are building Flutter locally. Use this to select a specific\n'
'version of the engine if you have built multiple engine targets.\n'
'This path is relative to --local-engine-src-path/out.',
'This path is relative to --local-engine-src-path/out. This option\n'
'is required when running an A/B test (see the --ab option).',
)
..addFlag(
'list',