[imitatioin_game] Made the imitation_game start reporting its results in the README.md. (#193)

This commit is contained in:
gaaclarke
2020-08-18 15:54:09 -07:00
committed by GitHub
parent 9072dd205f
commit d824e748e4
93 changed files with 256 additions and 73 deletions

View File

@ -0,0 +1,3 @@
## 0.0.1
* Initial version (TBD).

View File

@ -0,0 +1,27 @@
Copyright 2019 The Chromium Authors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -34,7 +34,7 @@ tested. As new tests are added please add to this list:
```text ```text
./ ./
├─ imitation_game.dart ├─ imitation_game.dart
└─ tests/ └─ imitation_tests/
├─ smiley/ ├─ smiley/
│ ├─ README.md │ ├─ README.md
│ ├─ flutter/ │ ├─ flutter/
@ -91,3 +91,13 @@ An implementation has to follow these rules:
``` ```
A single test run can report multiple numbers. A single test run can report multiple numbers.
## Results
Date created: 2020-08-17 23:57:16.702500Z
- smiley
- flutter
- startupTime: 0.561475s
- uikit
- startupTime: 0.373102068901062s

View File

@ -1,6 +1,8 @@
import 'dart:async'; import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'package:mustache/mustache.dart';
import 'package:imitation_game/README_template.dart';
const int _port = 4040; const int _port = 4040;
@ -33,6 +35,14 @@ Future<List<FileSystemEntity>> findFiles(Directory dir, {FileFilter where}) {
return completer.future; return completer.future;
} }
String _makeMarkdownOutput(Map<String, dynamic> results) {
final Template template = Template(readmeTemplate, name: 'README.md');
final Map<String, dynamic> values = Map<String, dynamic>.from(results);
values['date'] = DateTime.now().toUtc();
final String output = template.renderString(values);
return output;
}
class _Script { class _Script {
_Script({this.path}); _Script({this.path});
String path; String path;
@ -75,6 +85,33 @@ class _ScriptRunner {
} }
} }
/// Recursively converts a map of maps to a map of lists of maps.
///
/// For example:
/// _map2List({'a': {'b': 123}}, ['foo', 'bar']) ->
/// {
/// 'foo':[
/// {
/// 'name': 'a',
/// 'bar': [{'name': 'b', 'value': 123}]
/// }
/// ]
/// }
Map<String, dynamic> _map2List(Map<String, dynamic> map, List<String> names) {
final List<Map<String, dynamic>> returnList = <Map<String, dynamic>>[];
final List<String> tail = names.sublist(1);
map.forEach((String key, dynamic value) {
final Map<String, dynamic> testResult = <String, dynamic>{'name': key};
if (tail.isEmpty) {
testResult['value'] = value;
} else {
testResult[tail.first] = _map2List(value, tail)[tail.first];
}
returnList.add(testResult);
});
return <String, dynamic>{names.first: returnList};
}
class _ImitationGame { class _ImitationGame {
final Map<String, dynamic> results = <String, dynamic>{}; final Map<String, dynamic> results = <String, dynamic>{};
_ScriptRunner _scriptRunner; _ScriptRunner _scriptRunner;
@ -95,6 +132,7 @@ class _ImitationGame {
results[test][platform] = <String, dynamic>{}; results[test][platform] = <String, dynamic>{};
} }
data['results'].forEach((String k, dynamic v) { data['results'].forEach((String k, dynamic v) {
// ignore: avoid_as
results[test][platform][k] = v as double; results[test][platform][k] = v as double;
}); });
return _runNext(); return _runNext();
@ -148,6 +186,7 @@ Future<void> main() async {
if (request.method == 'POST') { if (request.method == 'POST') {
final String content = await utf8.decoder.bind(request).join(); final String content = await utf8.decoder.bind(request).join();
final Map<String, dynamic> data = final Map<String, dynamic> data =
// ignore: avoid_as
jsonDecode(content) as Map<String, dynamic>; jsonDecode(content) as Map<String, dynamic>;
print('$data'); print('$data');
keepRunning = await game.handleResult(data); keepRunning = await game.handleResult(data);
@ -163,8 +202,9 @@ Future<void> main() async {
keepRunning = await game.handleTimeout(); keepRunning = await game.handleTimeout();
} }
} }
const JsonEncoder encoder = JsonEncoder.withIndent(' ');
final String jsonResults = encoder.convert(game.results); final Map<String, dynamic> markdownValues =
print('$jsonResults'); _map2List(game.results, <String>['tests', 'platforms', 'measurements']);
File('README.md').writeAsStringSync(_makeMarkdownOutput(markdownValues));
await server.close(force: true); await server.close(force: true);
} }

View File

@ -26,9 +26,7 @@ Future<void> _sendResult(double result) async {
body: jsonEncode(<String, dynamic>{ body: jsonEncode(<String, dynamic>{
'test': 'smiley', 'test': 'smiley',
'platform': 'flutter', 'platform': 'flutter',
'results': <String, double>{ 'results': <String, double>{'startupTime': result},
'startupTime': result
},
}), }),
); );
if (response.statusCode != 200) { if (response.statusCode != 200) {

View File

@ -6,6 +6,4 @@
@interface AppDelegate : UIResponder<UIApplicationDelegate> @interface AppDelegate : UIResponder<UIApplicationDelegate>
@end @end

View File

@ -10,28 +10,29 @@
@implementation AppDelegate @implementation AppDelegate
- (BOOL)application:(UIApplication *)application
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch. // Override point for customization after application launch.
return YES; return YES;
} }
#pragma mark - UISceneSession lifecycle #pragma mark - UISceneSession lifecycle
- (UISceneConfiguration *)application:(UIApplication *)application
- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options { configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession
options:(UISceneConnectionOptions *)options {
// Called when a new scene session is being created. // Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with. // Use this method to select a configuration to create the new scene with.
return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role]; return [[UISceneConfiguration alloc] initWithName:@"Default Configuration"
sessionRole:connectingSceneSession.role];
} }
- (void)application:(UIApplication *)application
- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions { didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions {
// Called when the user discards a scene session. // Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. // If any sessions were discarded while the application was not running, this will be called
// Use this method to release any resources that were specific to the discarded scenes, as they will not return. // shortly after application:didFinishLaunchingWithOptions. Use this method to release any
// resources that were specific to the discarded scenes, as they will not return.
} }
@end @end

View File

@ -9,4 +9,3 @@
@property(strong, nonatomic) UIWindow* window; @property(strong, nonatomic) UIWindow* window;
@end @end

View File

@ -10,45 +10,43 @@
@implementation SceneDelegate @implementation SceneDelegate
- (void)scene:(UIScene *)scene
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions { willConnectToSession:(UISceneSession *)session
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`. options:(UISceneConnectionOptions *)connectionOptions {
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene. // Use this method to optionally configure and attach the UIWindow `window` to the provided
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead). // UIWindowScene `scene`. If using a storyboard, the `window` property will automatically be
// initialized and attached to the scene. This delegate does not imply the connecting scene or
// session are new (see `application:configurationForConnectingSceneSession` instead).
} }
- (void)sceneDidDisconnect:(UIScene *)scene { - (void)sceneDidDisconnect:(UIScene *)scene {
// Called as the scene is being released by the system. // Called as the scene is being released by the system.
// This occurs shortly after the scene enters the background, or when its session is discarded. // This occurs shortly after the scene enters the background, or when its session is discarded.
// Release any resources associated with this scene that can be re-created the next time the scene connects. // Release any resources associated with this scene that can be re-created the next time the scene
// The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead). // connects. The scene may re-connect later, as its session was not neccessarily discarded (see
// `application:didDiscardSceneSessions` instead).
} }
- (void)sceneDidBecomeActive:(UIScene *)scene { - (void)sceneDidBecomeActive:(UIScene *)scene {
// Called when the scene has moved from an inactive state to an active state. // Called when the scene has moved from an inactive state to an active state.
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive. // Use this method to restart any tasks that were paused (or not yet started) when the scene was
// inactive.
} }
- (void)sceneWillResignActive:(UIScene *)scene { - (void)sceneWillResignActive:(UIScene *)scene {
// Called when the scene will move from an active state to an inactive state. // Called when the scene will move from an active state to an inactive state.
// This may occur due to temporary interruptions (ex. an incoming phone call). // This may occur due to temporary interruptions (ex. an incoming phone call).
} }
- (void)sceneWillEnterForeground:(UIScene *)scene { - (void)sceneWillEnterForeground:(UIScene *)scene {
// Called as the scene transitions from the background to the foreground. // Called as the scene transitions from the background to the foreground.
// Use this method to undo the changes made on entering the background. // Use this method to undo the changes made on entering the background.
} }
- (void)sceneDidEnterBackground:(UIScene *)scene { - (void)sceneDidEnterBackground:(UIScene *)scene {
// Called as the scene transitions from the foreground to the background. // Called as the scene transitions from the foreground to the background.
// Use this method to save data, release shared resources, and store enough scene-specific state information // Use this method to save data, release shared resources, and store enough scene-specific state
// to restore the scene back to its current state. // information to restore the scene back to its current state.
} }
@end @end

View File

@ -8,4 +8,3 @@
@property(nonatomic, strong) IBOutlet UIImageView* imageView; @property(nonatomic, strong) IBOutlet UIImageView* imageView;
@end @end

View File

@ -24,8 +24,7 @@ static int64_t loadStartupTime(NSError **error) {
struct timeval startTime = proc.kp_proc.p_starttime; struct timeval startTime = proc.kp_proc.p_starttime;
int64_t microsecondsInSecond = 1000000LL; int64_t microsecondsInSecond = 1000000LL;
int64_t microsecondsSinceEpoch = int64_t microsecondsSinceEpoch =
(int64_t)(startTime.tv_sec * microsecondsInSecond) + (int64_t)(startTime.tv_sec * microsecondsInSecond) + (int64_t)startTime.tv_usec;
(int64_t)startTime.tv_usec;
return microsecondsSinceEpoch; return microsecondsSinceEpoch;
} }
@ -34,16 +33,13 @@ static NSString *loadIpAddress() {
#if TARGET_IPHONE_SIMULATOR #if TARGET_IPHONE_SIMULATOR
return @"127.0.0.1:4040"; return @"127.0.0.1:4040";
#else #else
NSString *ipPath = [[NSBundle mainBundle] pathForResource:@"ip" NSString *ipPath = [[NSBundle mainBundle] pathForResource:@"ip" ofType:@"txt"];
ofType:@"txt"];
NSError *err; NSError *err;
NSString *ipAddress = [NSString stringWithContentsOfFile:ipPath NSString *ipAddress =
encoding:NSUTF8StringEncoding [NSString stringWithContentsOfFile:ipPath encoding:NSUTF8StringEncoding error:&err];
error:&err];
assert(err == nil); assert(err == nil);
return [ipAddress return
stringByTrimmingCharactersInSet:[NSCharacterSet [ipAddress stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
whitespaceAndNewlineCharacterSet]];
#endif #endif
} }
@ -61,18 +57,15 @@ static void sendResults(NSTimeInterval result) {
} }
}; };
NSError *error; NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:payload NSData *jsonData = [NSJSONSerialization dataWithJSONObject:payload options:0 error:&error];
options:0
error:&error];
assert(error == nil && jsonData); assert(error == nil && jsonData);
[urlRequest setHTTPMethod:@"POST"]; [urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:jsonData]; [urlRequest setHTTPBody:jsonData];
NSURLSession *session = [NSURLSession sharedSession]; NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session NSURLSessionDataTask *dataTask =
dataTaskWithRequest:urlRequest [session dataTaskWithRequest:urlRequest
completionHandler:^(NSData *data, NSURLResponse *response, completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSError *error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (httpResponse.statusCode != 200) { if (httpResponse.statusCode != 200) {
NSLog(@"Error %@ '%@'", error, url); NSLog(@"Error %@ '%@'", error, url);
@ -95,10 +88,8 @@ static void sendResults(NSTimeInterval result) {
- (void)loadView { - (void)loadView {
[super loadView]; [super loadView];
self.displayLink = [CADisplayLink displayLinkWithTarget:self self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(onTick:)];
selector:@selector(onTick:)]; [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
[self.displayLink addToRunLoop:[NSRunLoop mainRunLoop]
forMode:NSRunLoopCommonModes];
self.imageView.image = [UIImage imageNamed:@"smiley"]; self.imageView.image = [UIImage imageNamed:@"smiley"];
_waitingForDraw = YES; _waitingForDraw = YES;
} }

View File

@ -0,0 +1,109 @@
/// Mustache template used for generating README.md.
String readmeTemplate = """# Imitation Game
## Description
`imitation_game` is a platform for performing automated tests to compare the
performance of different UI frameworks. For example, how much memory does the
same app written in Flutter and UIKit take?
## Running all the tests
You need a mobile device plugged into your computer and setup for development.
The mobile device and the computer need to be on the same network, one that
allows communication between computers since that's how the mobile phone will
report its results to the computer.
```sh
dart imitation_game.dart
```
## Dependencies
In order to run the tests you will need the union of all the platforms being
tested. As new tests are added please add to this list:
### iOS
- Flutter
- Xcode
- [ios_deploy](https://github.com/ios-control/ios-deploy) - used to launch apps
on the attached iOS device.
## Example File Layout
```text
./
├─ imitation_game.dart
└─ tests/
├─ smiley/
│ ├─ README.md
│ ├─ flutter/
│ │ ├─ run_ios.sh
│ │ └─ <flutter project files>
│ └─ uikit/
│ ├─ run_ios.sh
│ └─ <uikit project files>
└─ memory/
├─ README.md
├─ flutter/
│ ├─ run_ios.sh
│ └─ <flutter project files>
└─ uikit/
├─ run_ios.sh
└─ <uikit project files>
```
Here there are 2 different tests with 2 different platform implementations. The
tests are named `smiley` and `memory`, they are both implemented on the
platforms `flutter` and `uikit`.
### Adding a test
Tests should comprise of implementations on one or more platform. The directory
for the test should be added to `./tests`. Inside that directory there should
be a directory of implementations and a `README.md` file that explains the test.
### Adding an implementation to a test
An implementation has to follow these rules:
- It needs to perform the same operations as the other implementations and
follow the description in the test's `README.md`.
- It needs to contain a `run_ios.sh` script that will build and launch the test
on the connected device.
- It should contain a file named `ip.txt` which will be overwritten by
`imitation_game.dart` with the ip address and port that should be used to
report results to.
- It needs to report its results to the ip and port in the `ip.txt` via an HTTP
POST of JSON data.
## Data format for results
```json
{
"test": "name_of_test",
"platform": "name_of_platform",
"results": {
"some_result_name": 1.23,
"some_result_name2": 4.56,
}
}
```
A single test run can report multiple numbers.
## Results
Date created: {{date}}
{{#tests}}
- {{name}}
{{#platforms}}
- {{name}}
{{#measurements}}
- {{name}}: {{value}}s
{{/measurements}}
{{/platforms}}
{{/tests}}
""";

View File

@ -0,0 +1 @@

View File

@ -0,0 +1,8 @@
name: imitation_game
version: 0.0.1
description: Testing framework for comparing multiple frameworks' performance.
homepage: https://github.com/flutter/packages/tree/master/packages/imitation_game
dependencies:
mustache: ^1.1.1
environment:
sdk: ">=1.8.0 <3.0.0"

1
packages/imitation_game/run.sh Executable file
View File

@ -0,0 +1 @@
pub run imitation_game