feat(benchmarks): initial version of tree benchmark

Closes #269
This commit is contained in:
Tobias Bosch
2014-12-08 10:57:09 -08:00
parent 9b7e2e3029
commit 01fa90c11d
8 changed files with 405 additions and 0 deletions

View File

@ -0,0 +1,7 @@
library tree_benchmark;
import './tree_benchmark_ng10.dart' as bm;
main () {
bm.main();
}

View File

@ -0,0 +1 @@
export {main} from './tree_benchmark_ng13';

View File

@ -0,0 +1,3 @@
$SCRIPTS$
<tree data="initData"></tree>

View File

@ -0,0 +1,76 @@
library tree_benchmark_ng10;
import 'package:angular/angular.dart';
import 'package:angular/application_factory.dart';
import 'package:benchpress/benchpress.dart';
import 'dart:html';
var MAX_DEPTH = 9;
setup() {
var m = new Module()
..bind(CompilerConfig, toValue: new CompilerConfig.withOptions(elementProbeEnabled: false))
..bind(ScopeDigestTTL, toFactory: () => new ScopeDigestTTL.value(15), inject: [])
..bind(TreeComponent);
final injector = applicationFactory().addModule(m).run();
return injector;
}
main() {
final injector = setup();
final zone = injector.get(VmTurnZone);
final rootScope = injector.get(Scope);
benchmark("tree benchmark", () {
var count = 0;
benchmarkStep("AngularDart destroyDom binary tree of depth ${MAX_DEPTH}", () {
zone.run(() {
rootScope.context['initData'] = new TreeNode('');
});
});
benchmarkStep("AngularDart createDom binary tree of depth ${MAX_DEPTH}", () {
zone.run(() {
var maxDepth = 9;
var values = count++ % 2 == 0 ?
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '*'] :
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', '-'];
rootScope.context['initData'] = buildTree(maxDepth, values, 0);
});
});
});
}
@Component(
selector: 'tree',
map: const {'data': '=>data'},
template: '<span> {{data.value}}'
'<span ng-if="data.right != null"><tree data=data.right></span>'
'<span ng-if="data.left != null"><tree data=data.left></span>'
'</span>'
)
class TreeComponent {
var data;
}
buildTree(maxDepth, values, curDepth) {
if (maxDepth == curDepth) return new TreeNode('');
return new TreeNode(
values[curDepth],
buildTree(maxDepth, values, curDepth+1),
buildTree(maxDepth, values, curDepth+1));
}
class TreeNode {
var value;
TreeNode left;
TreeNode right;
TreeNode([this.value, this.left, this.right]);
}

View File

@ -0,0 +1,112 @@
import {benchmark, benchmarkStep} from 'benchpress/benchpress';
var MAX_DEPTH = 9;
function setup() {
var $rootScope;
angular.module('app', [])
.directive('tree', function() {
return {
scope: {
data: '='
},
template:
'<span> {{data.value}}'+
' <span tree-if="data.left"></span>'+
' <span tree-if="data.right"></span>'+
'</span>'
};
})
// special directive for "if" as angular 1.3 does not support
// recursive components.
.directive('treeIf', ['$compile', '$parse', function($compile, $parse) {
var transcludeFn;
return {
compile: function(element, attrs) {
var expr = $parse(attrs.treeIf);
var template = '<tree data="'+attrs.treeIf+'"></tree>';
var transclude;
return function($scope, $element, $attrs) {
if (!transclude) {
transclude = $compile(template);
}
var childScope;
var childElement;
$scope.$watch(expr, function(newValue) {
if (childScope) {
childScope.$destroy();
childElement.remove();
childScope = null;
childElement = null;
}
if (newValue) {
childScope = $scope.$new();
childElement = transclude(childScope, function(clone) {
$element.append(clone);
});
}
});
}
}
}
}])
.config(['$compileProvider', function($compileProvider) {
$compileProvider.debugInfoEnabled(false);
}])
.run(['$rootScope', function(_$rootScope_) {
$rootScope = _$rootScope_;
}])
angular.bootstrap(document.body, ['app']);
return $rootScope;
}
export function main() {
var $rootScope = setup();
benchmark(`tree benchmark`, function() {
var count = 0;
benchmarkStep(`AngularJS destroyDom binary tree of depth ${MAX_DEPTH}`, function() {
$rootScope.$apply(function() {
$rootScope.initData = new TreeNode('', null, null);
});
});
benchmarkStep(`AngularJS createDom binary tree of depth ${MAX_DEPTH}`, function() {
var maxDepth = 9;
var values = count++ % 2 == 0 ?
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '*'] :
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', '-'];
$rootScope.$apply(function() {
$rootScope.initData = buildTree(maxDepth, values, 0);
});
});
});
}
class TreeNode {
value:string;
left:TreeNode;
right:TreeNode;
constructor(value, left, right) {
this.value = value;
this.left = left;
this.right = right;
}
}
function buildTree(maxDepth, values, curDepth) {
if (maxDepth === curDepth) return new TreeNode('', null, null);
return new TreeNode(
values[curDepth],
buildTree(maxDepth, values, curDepth+1),
buildTree(maxDepth, values, curDepth+1));
}