Skip to content
This repository has been archived by the owner on Sep 15, 2021. It is now read-only.

Commit

Permalink
Revert "Update to pkg/observable 0.17.0"
Browse files Browse the repository at this point in the history
This reverts commit c124c2c.
  • Loading branch information
Matan Lurey committed Nov 21, 2016
1 parent c124c2c commit 77f5f61
Show file tree
Hide file tree
Showing 20 changed files with 711 additions and 23 deletions.
171 changes: 171 additions & 0 deletions benchmark/index.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library observe.test.benchmark.index;

import 'dart:async';
import 'dart:html';
import 'package:benchmark_harness/benchmark_harness.dart';
import 'package:chart/chart.dart';
import 'package:observe/mirrors_used.dart' as mu; // Makes output smaller.
import 'package:smoke/mirrors.dart';
import 'object_benchmark.dart';
import 'setup_object_benchmark.dart';
import 'observable_list_benchmark.dart';
import 'setup_observable_list_benchmark.dart';
import 'path_benchmark.dart';
import 'setup_path_benchmark.dart';

/// Benchmark names to factory functions.
/// Uses [mu].
typedef BenchmarkBase BenchmarkFactory(
int objectCount, int mutationCount, String config);
final Map<String, BenchmarkFactory> benchmarkFactories = {
'ObjectBenchmark': (int o, int m, String c) => new ObjectBenchmark(o, m, c),
'SetupObjectBenchmark': (int o, int m, String c) =>
new SetupObjectBenchmark(o, c),
'ObservableListBenchmark': (int o, int m, String c) =>
new ObservableListBenchmark(o, m, c),
'SetupObservableListBenchmark': (int o, int m, String c) =>
new SetupObservableListBenchmark(o, c),
'PathBenchmark': (int o, int m, String c) => new PathBenchmark(o, m, c),
'SetupPathBenchmark': (int o, int m, String c) =>
new SetupPathBenchmark(o, c),
};

/// Benchmark names to possible configs.
final Map<String, List<String>> benchmarkConfigs = {
'ObjectBenchmark': [],
'SetupObjectBenchmark': [],
'ObservableListBenchmark': ['splice', 'update', 'push/pop', 'shift/unshift'],
'SetupObservableListBenchmark': [],
'PathBenchmark': ['leaf', 'root'],
'SetupPathBenchmark': [],
};

Iterable<int> objectCounts;
Iterable<int> mutationCounts;

final ButtonElement goButton = querySelector('#go');
final InputElement objectCountInput = querySelector('#objectCountInput');
final InputElement mutationCountInput = querySelector('#mutationCountInput');
final SpanElement mutationCountWrapper = querySelector('#mutationCountWrapper');
final SpanElement statusSpan = querySelector('#status');
final DivElement canvasWrapper = querySelector('#canvasWrapper');
final SelectElement benchmarkSelect = querySelector('#benchmarkSelect');
final SelectElement configSelect = querySelector('#configSelect');
final UListElement legendList = querySelector('#legendList');
final List<String> colors = [
[0, 0, 255],
[138, 43, 226],
[165, 42, 42],
[100, 149, 237],
[220, 20, 60],
[184, 134, 11]
].map((rgb) => 'rgba(' + rgb.join(',') + ',.7)').toList();

main() {
// TODO(jakemac): Use a transformer to generate the smoke config so we can see
// how that affects the benchmark.
useMirrors();

benchmarkSelect.onChange.listen((_) => changeBenchmark());
changeBenchmark();

goButton.onClick.listen((_) async {
canvasWrapper.children.clear();
goButton.disabled = true;
goButton.text = 'Running...';
legendList.text = '';
objectCounts =
objectCountInput.value.split(',').map((val) => int.parse(val));

if (benchmarkSelect.value.startsWith('Setup')) {
mutationCounts = [0];
} else {
mutationCounts =
mutationCountInput.value.split(',').map((val) => int.parse(val));
}

var i = 0;
mutationCounts.forEach((count) {
var li = document.createElement('li');
li.text = '$count mutations.';
li.style.color = colors[i % colors.length];
legendList.append(li);
i++;
});

var results = <List<double>>[];
for (int objectCount in objectCounts) {
int x = 0;
for (int mutationCount in mutationCounts) {
statusSpan.text =
'Testing: $objectCount objects with $mutationCount mutations';
// Let the status text render before running the next benchmark.
await new Future(() {});
var factory = benchmarkFactories[benchmarkSelect.value];
var benchmark = factory(objectCount, mutationCount, configSelect.value);
// Divide by 10 because benchmark_harness returns the amount of time it
// took to run 10 times, not once :(.
var resultMicros = benchmark.measure() / 10;

if (results.length <= x) results.add([]);
results[x].add(resultMicros / 1000);
x++;
}
}

drawBenchmarks(results);
});
}

void drawBenchmarks(List<List<double>> results) {
var datasets = [];
for (int i = 0; i < results.length; i++) {
datasets.add({
'fillColor': 'rgba(255, 255, 255, 0)',
'strokeColor': colors[i % colors.length],
'pointColor': colors[i % colors.length],
'pointStrokeColor': "#fff",
'data': results[i],
});
}
var data = {
'labels': objectCounts.map((c) => '$c').toList(),
'datasets': datasets,
};

new Line(data, {
'bezierCurve': false,
}).show(canvasWrapper);
goButton.disabled = false;
goButton.text = 'Run Benchmarks';
statusSpan.text = '';
}

void changeBenchmark() {
var configs = benchmarkConfigs[benchmarkSelect.value];
configSelect.text = '';
configs.forEach((config) {
var option = document.createElement('option');
option.text = config;
configSelect.append(option);
});

document.title = benchmarkSelect.value;

// Don't show the configSelect if there are no configs.
if (configs.isEmpty) {
configSelect.style.display = 'none';
} else {
configSelect.style.display = 'inline';
}

// Don't show the mutation counts box if running a Setup* benchmark.
if (benchmarkSelect.value.startsWith('Setup')) {
mutationCountWrapper.style.display = 'none';
} else {
mutationCountWrapper.style.display = 'inline';
}
}
73 changes: 73 additions & 0 deletions benchmark/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<html>
<!--
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<head>
<title>Observation Benchmarks</title>
<meta charset="utf-8">
<style>
* {
font-family: arial, helvetica, sans-serif;
font-weight: 400;
}
body {
margin: 0;
padding: 0;
background-color: rgba(0, 0, 0, .1);
}
#canvasWrapper {
width: 800px;
height: 400px;
}
</style>
</head>
<body>
<h1>Observation Benchmarks</h1>

<select id="benchmarkSelect">
<option>ObjectBenchmark</option>
<option>SetupObjectBenchmark</option>
<option>ObservableListBenchmark</option>
<option>SetupObservableListBenchmark</option>
<option>PathBenchmark</option>
<option>SetupPathBenchmark</option>
</select>
<select id="configSelect">
</select>

<button id="go">Run Benchmarks</button>

<span>Object Count: </span>
<input id="objectCountInput" style="width: 200px" value="4000, 8000, 16000, 32000">
<span id="mutationCountWrapper">
<span>Mutation Count: </span>
<input id="mutationCountInput" style="width: 200px"
value="0, 100, 200, 400, 800, 1600"><br>
</span>
<br>
<span id="status"></span>

<section style="width: 100%">
<article>
<div style="display:inline-block; padding-bottom: 20px">
Times in ms
</div>
<div id="canvasWrapper" style="display:inline-block">
</div>
<div style="display:inline-block">
<ul id="legendList">
</ul>
</div>
</article>
</section>
<h3 style="margin-left: 440px">Object Set Size</h3>
<script type="application/dart" src="index.dart"></script>
<script src="packages/browser/dart.js">
</script>
</body>
</html>
28 changes: 28 additions & 0 deletions benchmark/object_benchmark.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library observe.test.benchmark.object_benchmark;

import 'observation_benchmark_base.dart';
import 'test_observable.dart';

class ObjectBenchmark extends ObservationBenchmarkBase<TestObservable> {
ObjectBenchmark(int objectCount, int mutationCount, String config)
: super('ObjectBenchmark:$objectCount:$mutationCount:$config',
objectCount, mutationCount, config);

@override
int mutateObject(TestObservable obj) {
// Modify the first 5 properties.
obj.a++;
obj.b++;
obj.c++;
obj.d++;
obj.e++;
// Return # of modifications.
return 5;
}

@override
TestObservable newObject() => new TestObservable();
}
58 changes: 58 additions & 0 deletions benchmark/observable_list_benchmark.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library observe.test.benchmark.observable_list_benchmark;

import 'package:observable/observable.dart';
import 'observation_benchmark_base.dart';

class ObservableListBenchmark extends ObservationBenchmarkBase<ObservableList> {
final int elementCount = 100;

ObservableListBenchmark(int objectCount, int mutationCount, String config)
: super('ObservableListBenchmark:$objectCount:$mutationCount:$config',
objectCount, mutationCount, config);

@override
int mutateObject(ObservableList obj) {
switch (config) {
case 'update':
var size = (elementCount / 10).floor();
for (var j = 0; j < size; j++) {
obj[j * size]++;
}
return size;

case 'splice':
var size = (elementCount / 5).floor();
// No splice equivalent in List, so we hardcode it.
var removed = obj.sublist(size, size * 2);
obj.removeRange(size, size * 2);
obj.insertAll(size * 2, removed);
return size * 2;

case 'push/pop':
var val = obj.removeLast();
obj.add(val + 1);
return 2;

case 'shift/unshift':
var val = obj.removeAt(0);
obj.insert(0, val + 1);
return 2;

default:
throw new ArgumentError(
'Invalid config for ObservableListBenchmark: $config');
}
}

@override
ObservableList newObject() {
var list = new ObservableList();
for (int i = 0; i < elementCount; i++) {
list.add(i);
}
return list;
}
}
Loading

0 comments on commit 77f5f61

Please sign in to comment.