Refactor devicelab logic to use TaskResult instead of JSON (#67550)
This commit is contained in:
@@ -5,6 +5,8 @@
|
||||
import 'dart:math' as math;
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
import 'task_result.dart';
|
||||
|
||||
const String kBenchmarkTypeKeyName = 'benchmark_type';
|
||||
const String kBenchmarkVersionKeyName = 'version';
|
||||
const String kLocalEngineKeyName = 'local_engine';
|
||||
@@ -58,7 +60,7 @@ class ABTest {
|
||||
/// The result may contain multiple score keys.
|
||||
///
|
||||
/// [result] is expected to be a serialization of [TaskResult].
|
||||
void addAResult(Map<String, dynamic> result) {
|
||||
void addAResult(TaskResult result) {
|
||||
if (_runEnd != null) {
|
||||
throw StateError('Cannot add results to ABTest after it is finalized');
|
||||
}
|
||||
@@ -70,7 +72,7 @@ class ABTest {
|
||||
/// The result may contain multiple score keys.
|
||||
///
|
||||
/// [result] is expected to be a serialization of [TaskResult].
|
||||
void addBResult(Map<String, dynamic> result) {
|
||||
void addBResult(TaskResult result) {
|
||||
if (_runEnd != null) {
|
||||
throw StateError('Cannot add results to ABTest after it is finalized');
|
||||
}
|
||||
@@ -276,11 +278,9 @@ class _ScoreSummary {
|
||||
}
|
||||
}
|
||||
|
||||
void _addResult(Map<String, dynamic> result, Map<String, List<double>> results) {
|
||||
final List<String> scoreKeys = (result['benchmarkScoreKeys'] as List<dynamic>).cast<String>();
|
||||
final Map<String, dynamic> data = result['data'] as Map<String, dynamic>;
|
||||
for (final String scoreKey in scoreKeys) {
|
||||
final double score = (data[scoreKey] as num).toDouble();
|
||||
void _addResult(TaskResult result, Map<String, List<double>> results) {
|
||||
for (final String scoreKey in result.benchmarkScoreKeys) {
|
||||
final double score = (result.data[scoreKey] as num).toDouble();
|
||||
results.putIfAbsent(scoreKey, () => <double>[]).add(score);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,9 +5,9 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:flutter_devicelab/framework/framework.dart';
|
||||
import 'package:flutter_devicelab/framework/utils.dart';
|
||||
|
||||
import 'task_result.dart';
|
||||
import 'utils.dart';
|
||||
|
||||
final String platformLineSep = Platform.isWindows ? '\r\n' : '\n';
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ import 'package:logging/logging.dart';
|
||||
import 'package:stack_trace/stack_trace.dart';
|
||||
|
||||
import 'running_processes.dart';
|
||||
import 'task_result.dart';
|
||||
import 'utils.dart';
|
||||
|
||||
/// Represents a unit of work performed in the CI environment that can
|
||||
@@ -185,102 +186,3 @@ class _TaskRunner {
|
||||
return completer.future;
|
||||
}
|
||||
}
|
||||
|
||||
/// A result of running a single task.
|
||||
class TaskResult {
|
||||
/// Constructs a successful result.
|
||||
TaskResult.success(this.data, {
|
||||
this.benchmarkScoreKeys = const <String>[],
|
||||
this.detailFiles,
|
||||
})
|
||||
: succeeded = true,
|
||||
message = 'success' {
|
||||
const JsonEncoder prettyJson = JsonEncoder.withIndent(' ');
|
||||
if (benchmarkScoreKeys != null) {
|
||||
for (final String key in benchmarkScoreKeys) {
|
||||
if (!data.containsKey(key)) {
|
||||
throw 'Invalid benchmark score key "$key". It does not exist in task '
|
||||
'result data ${prettyJson.convert(data)}';
|
||||
} else if (data[key] is! num) {
|
||||
throw 'Invalid benchmark score for key "$key". It is expected to be a num '
|
||||
'but was ${data[key].runtimeType}: ${prettyJson.convert(data[key])}';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Constructs a successful result using JSON data stored in a file.
|
||||
factory TaskResult.successFromFile(File file,
|
||||
{List<String> benchmarkScoreKeys}) {
|
||||
return TaskResult.success(
|
||||
json.decode(file.readAsStringSync()) as Map<String, dynamic>,
|
||||
benchmarkScoreKeys: benchmarkScoreKeys,
|
||||
);
|
||||
}
|
||||
|
||||
/// Constructs an unsuccessful result.
|
||||
TaskResult.failure(this.message)
|
||||
: succeeded = false,
|
||||
data = null,
|
||||
detailFiles = null,
|
||||
benchmarkScoreKeys = const <String>[];
|
||||
|
||||
/// Whether the task succeeded.
|
||||
final bool succeeded;
|
||||
|
||||
/// Task-specific JSON data
|
||||
final Map<String, dynamic> data;
|
||||
|
||||
/// Files containing detail on the run (e.g. timeline trace files)
|
||||
final List<String> detailFiles;
|
||||
|
||||
/// Keys in [data] that store scores that will be submitted to Cocoon.
|
||||
///
|
||||
/// Each key is also part of a benchmark's name tracked by Cocoon.
|
||||
final List<String> benchmarkScoreKeys;
|
||||
|
||||
/// Whether the task failed.
|
||||
bool get failed => !succeeded;
|
||||
|
||||
/// Explains the result in a human-readable format.
|
||||
final String message;
|
||||
|
||||
/// Serializes this task result to JSON format.
|
||||
///
|
||||
/// The JSON format is as follows:
|
||||
///
|
||||
/// {
|
||||
/// "success": true|false,
|
||||
/// "data": arbitrary JSON data valid only for successful results,
|
||||
/// "detailFiles": list of filenames containing detail on the run
|
||||
/// "benchmarkScoreKeys": [
|
||||
/// contains keys into "data" that represent benchmarks scores, which
|
||||
/// can be uploaded, for example. to golem, valid only for successful
|
||||
/// results
|
||||
/// ],
|
||||
/// "reason": failure reason string valid only for unsuccessful results
|
||||
/// }
|
||||
Map<String, dynamic> toJson() {
|
||||
final Map<String, dynamic> json = <String, dynamic>{
|
||||
'success': succeeded,
|
||||
};
|
||||
|
||||
if (succeeded) {
|
||||
json['data'] = data;
|
||||
if (detailFiles != null)
|
||||
json['detailFiles'] = detailFiles;
|
||||
json['benchmarkScoreKeys'] = benchmarkScoreKeys;
|
||||
} else {
|
||||
json['reason'] = message;
|
||||
}
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() => message;
|
||||
}
|
||||
|
||||
class TaskResultCheckProcesses extends TaskResult {
|
||||
TaskResultCheckProcesses() : super.success(null);
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@ import 'package:vm_service_client/vm_service_client.dart';
|
||||
import 'package:flutter_devicelab/framework/utils.dart';
|
||||
import 'package:flutter_devicelab/framework/adb.dart';
|
||||
|
||||
import 'task_result.dart';
|
||||
|
||||
/// Runs a task in a separate Dart VM and collects the result using the VM
|
||||
/// service protocol.
|
||||
///
|
||||
@@ -20,7 +22,7 @@ import 'package:flutter_devicelab/framework/adb.dart';
|
||||
///
|
||||
/// Running the task in [silent] mode will suppress standard output from task
|
||||
/// processes and only print standard errors.
|
||||
Future<Map<String, dynamic>> runTask(
|
||||
Future<TaskResult> runTask(
|
||||
String taskName, {
|
||||
bool silent = false,
|
||||
String localEngine,
|
||||
@@ -79,7 +81,8 @@ Future<Map<String, dynamic>> runTask(
|
||||
|
||||
try {
|
||||
final VMIsolateRef isolate = await _connectToRunnerIsolate(await uri.future);
|
||||
final Map<String, dynamic> taskResult = await isolate.invokeExtension('ext.cocoonRunTask') as Map<String, dynamic>;
|
||||
final Map<String, dynamic> taskResultJson = await isolate.invokeExtension('ext.cocoonRunTask') as Map<String, dynamic>;
|
||||
final TaskResult taskResult = TaskResult.fromJson(taskResultJson);
|
||||
await runner.exitCode;
|
||||
return taskResult;
|
||||
} finally {
|
||||
|
||||
117
dev/devicelab/lib/framework/task_result.dart
Normal file
117
dev/devicelab/lib/framework/task_result.dart
Normal file
@@ -0,0 +1,117 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
/// A result of running a single task.
|
||||
class TaskResult {
|
||||
/// Constructs a successful result.
|
||||
TaskResult.success(this.data, {
|
||||
this.benchmarkScoreKeys = const <String>[],
|
||||
this.detailFiles,
|
||||
})
|
||||
: succeeded = true,
|
||||
message = 'success' {
|
||||
const JsonEncoder prettyJson = JsonEncoder.withIndent(' ');
|
||||
if (benchmarkScoreKeys != null) {
|
||||
for (final String key in benchmarkScoreKeys) {
|
||||
if (!data.containsKey(key)) {
|
||||
throw 'Invalid benchmark score key "$key". It does not exist in task '
|
||||
'result data ${prettyJson.convert(data)}';
|
||||
} else if (data[key] is! num) {
|
||||
throw 'Invalid benchmark score for key "$key". It is expected to be a num '
|
||||
'but was ${data[key].runtimeType}: ${prettyJson.convert(data[key])}';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Constructs a successful result using JSON data stored in a file.
|
||||
factory TaskResult.successFromFile(File file,
|
||||
{List<String> benchmarkScoreKeys}) {
|
||||
return TaskResult.success(
|
||||
json.decode(file.readAsStringSync()) as Map<String, dynamic>,
|
||||
benchmarkScoreKeys: benchmarkScoreKeys,
|
||||
);
|
||||
}
|
||||
|
||||
/// Constructs a [TaskResult] from JSON.
|
||||
factory TaskResult.fromJson(Map<String, dynamic> json) {
|
||||
final bool success = json['success'] as bool;
|
||||
if (success) {
|
||||
final List<String> benchmarkScoreKeys = (json['benchmarkScoreKeys'] as List<dynamic> ?? <String>[]).cast<String>();
|
||||
return TaskResult.success(json['data'] as Map<String, dynamic>,
|
||||
benchmarkScoreKeys: benchmarkScoreKeys);
|
||||
}
|
||||
|
||||
return TaskResult.failure(json['reason'] as String);
|
||||
}
|
||||
|
||||
/// Constructs an unsuccessful result.
|
||||
TaskResult.failure(this.message)
|
||||
: succeeded = false,
|
||||
data = null,
|
||||
detailFiles = null,
|
||||
benchmarkScoreKeys = const <String>[];
|
||||
|
||||
/// Whether the task succeeded.
|
||||
final bool succeeded;
|
||||
|
||||
/// Task-specific JSON data
|
||||
final Map<String, dynamic> data;
|
||||
|
||||
/// Files containing detail on the run (e.g. timeline trace files)
|
||||
final List<String> detailFiles;
|
||||
|
||||
/// Keys in [data] that store scores that will be submitted to Cocoon.
|
||||
///
|
||||
/// Each key is also part of a benchmark's name tracked by Cocoon.
|
||||
final List<String> benchmarkScoreKeys;
|
||||
|
||||
/// Whether the task failed.
|
||||
bool get failed => !succeeded;
|
||||
|
||||
/// Explains the result in a human-readable format.
|
||||
final String message;
|
||||
|
||||
/// Serializes this task result to JSON format.
|
||||
///
|
||||
/// The JSON format is as follows:
|
||||
///
|
||||
/// {
|
||||
/// "success": true|false,
|
||||
/// "data": arbitrary JSON data valid only for successful results,
|
||||
/// "detailFiles": list of filenames containing detail on the run
|
||||
/// "benchmarkScoreKeys": [
|
||||
/// contains keys into "data" that represent benchmarks scores, which
|
||||
/// can be uploaded, for example. to golem, valid only for successful
|
||||
/// results
|
||||
/// ],
|
||||
/// "reason": failure reason string valid only for unsuccessful results
|
||||
/// }
|
||||
Map<String, dynamic> toJson() {
|
||||
final Map<String, dynamic> json = <String, dynamic>{
|
||||
'success': succeeded,
|
||||
};
|
||||
|
||||
if (succeeded) {
|
||||
json['data'] = data;
|
||||
if (detailFiles != null)
|
||||
json['detailFiles'] = detailFiles;
|
||||
json['benchmarkScoreKeys'] = benchmarkScoreKeys;
|
||||
} else {
|
||||
json['reason'] = message;
|
||||
}
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() => message;
|
||||
}
|
||||
|
||||
class TaskResultCheckProcesses extends TaskResult {
|
||||
TaskResultCheckProcesses() : super.success(null);
|
||||
}
|
||||
@@ -12,7 +12,7 @@ import 'package:path/path.dart' as path;
|
||||
import 'package:process/process.dart';
|
||||
import 'package:stack_trace/stack_trace.dart';
|
||||
|
||||
import 'framework.dart';
|
||||
import 'task_result.dart';
|
||||
|
||||
/// Virtual current working directory, which affect functions, such as [exec].
|
||||
String cwd = Directory.current.path;
|
||||
|
||||
Reference in New Issue
Block a user