|
| 1 | +var performance=performance||{};performance.now=function(){return performance.now||performance.mozNow||performance.msNow||performance.oNow||performance.webkitNow||Date.now}();function Benchmark(a,c,b,d,e,f,g,h,l){this.name=a;this.doWarmup=c;this.doDeterministic=b;this.deterministicIterations=d;this.run=e;this.Setup=f?f:function(){};this.TearDown=g?g:function(){};this.rmsResult=h?h:null;this.minIterations=l?l:32}function BenchmarkResult(a,c,b){this.benchmark=a;this.time=c;this.latency=b} |
| 2 | +BenchmarkResult.prototype.valueOf=function(){return this.time};function BenchmarkSuite(a,c,b){this.name=a;this.reference=c;this.benchmarks=b;BenchmarkSuite.suites.push(this)}BenchmarkSuite.suites=[];BenchmarkSuite.version="9";BenchmarkSuite.config={doWarmup:void 0,doDeterministic:void 0};alert=function(a){throw"Alert called with argument: "+a;}; |
| 3 | +BenchmarkSuite.ResetRNG=function(){Math.random=function(){var a=49734321;return function(){a=a+2127912214+(a<<12)&4294967295;a=(a^3345072700^a>>>19)&4294967295;a=a+374761393+(a<<5)&4294967295;a=(a+3550635116^a<<9)&4294967295;a=a+4251993797+(a<<3)&4294967295;a=(a^3042594569^a>>>16)&4294967295;return(a&268435455)/268435456}}()}; |
| 4 | +BenchmarkSuite.RunSuites=function(a,c){function b(){for(;d||g<f;){if(d)d=d();else{var h=e[g++];a.NotifyStart&&a.NotifyStart(h.name);-1<c.indexOf(h.name)?h.NotifySkipped(a):d=h.RunStep(a)}if(d&&"undefined"!=typeof window&&window.setTimeout){window.setTimeout(b,25);return}}a.NotifyScore&&(h=BenchmarkSuite.GeometricMean(BenchmarkSuite.scores),h=BenchmarkSuite.FormatScore(100*h),a.NotifyScore(h))}c="undefined"===typeof c?[]:c;var d=null,e=BenchmarkSuite.suites,f=e.length;BenchmarkSuite.scores=[];var g= |
| 5 | +0;b()};BenchmarkSuite.CountBenchmarks=function(){for(var a=0,c=BenchmarkSuite.suites,b=0;b<c.length;b++)a+=c[b].benchmarks.length;return a};BenchmarkSuite.GeometricMean=function(a){for(var c=0,b=0;b<a.length;b++)c+=Math.log(a[b]);return Math.pow(Math.E,c/a.length)};BenchmarkSuite.GeometricMeanTime=function(a){for(var c=0,b=0;b<a.length;b++)c+=Math.log(a[b].time);return Math.pow(Math.E,c/a.length)}; |
| 6 | +BenchmarkSuite.GeometricMeanLatency=function(a){for(var c=0,b=!1,d=0;d<a.length;d++)0!=a[d].latency&&(c+=Math.log(a[d].latency),b=!0);return b?Math.pow(Math.E,c/a.length):0};BenchmarkSuite.FormatScore=function(a){return 100<a?a.toFixed(0):a.toPrecision(3)};BenchmarkSuite.prototype.NotifyStep=function(a){this.results.push(a);this.runner.NotifyStep&&this.runner.NotifyStep(a.benchmark.name)}; |
| 7 | +BenchmarkSuite.prototype.NotifyResult=function(){var a=BenchmarkSuite.GeometricMeanTime(this.results),a=this.reference[0]/a;BenchmarkSuite.scores.push(a);this.runner.NotifyResult&&(a=BenchmarkSuite.FormatScore(100*a),this.runner.NotifyResult(this.name,a));2==this.reference.length&&(a=BenchmarkSuite.GeometricMeanLatency(this.results),0!=a&&(a=this.reference[1]/a,BenchmarkSuite.scores.push(a),this.runner.NotifyResult&&(a=BenchmarkSuite.FormatScore(100*a),this.runner.NotifyResult(this.name+"Latency", |
| 8 | +a))))};BenchmarkSuite.prototype.NotifySkipped=function(a){BenchmarkSuite.scores.push(1);a.NotifyResult&&a.NotifyResult(this.name,"Skipped")};BenchmarkSuite.prototype.NotifyError=function(a){this.runner.NotifyError&&this.runner.NotifyError(this.name,a);this.runner.NotifyStep&&this.runner.NotifyStep(this.name)}; |
| 9 | +BenchmarkSuite.prototype.RunSingleBenchmark=function(a,c){function b(b){for(var c=0,d=new Date,f=0;e?f<a.deterministicIterations:1E3>c;f++)a.run(),c=new Date-d;null!=b&&(b.runs+=f,b.elapsed+=c)}var d=BenchmarkSuite.config,e=void 0!==d.doDeterministic?d.doDeterministic:a.doDeterministic;(void 0!==d.doWarmup?d.doWarmup:a.doWarmup)||null!=c||(c={runs:0,elapsed:0});if(null==c)return b(null),{runs:0,elapsed:0};b(c);if(c.runs<a.minIterations)return c;var d=1E3*c.elapsed/c.runs,f=null!=a.rmsResult?a.rmsResult(): |
| 10 | +0;this.NotifyStep(new BenchmarkResult(a,d,f));return null}; |
| 11 | +BenchmarkSuite.prototype.RunStep=function(a){function c(){if(f<e){try{g.benchmarks[f].Setup()}catch(a){return g.NotifyError(a),null}return b}g.NotifyResult();return null}function b(){try{h=g.RunSingleBenchmark(g.benchmarks[f],h)}catch(a){return g.NotifyError(a),null}return null==h?d:b()}function d(){try{g.benchmarks[f++].TearDown()}catch(a){return g.NotifyError(a),null}return c}BenchmarkSuite.ResetRNG();this.results=[];this.runner=a;var e=this.benchmarks.length,f=0,g=this,h;return c()}; |
| 12 | +var DeltaBlue=new BenchmarkSuite("DeltaBlue",[66118],[new Benchmark("DeltaBlue",!0,!1,4400,deltaBlue)]);Object.defineProperty(Object.prototype,"inheritsFrom",{value:function(a){function c(){}c.prototype=a.prototype;this.prototype=new c;this.superConstructor=a}});function OrderedCollection(){this.elms=[]}OrderedCollection.prototype.add=function(a){this.elms.push(a)};OrderedCollection.prototype.at=function(a){return this.elms[a]};OrderedCollection.prototype.size=function(){return this.elms.length}; |
| 13 | +OrderedCollection.prototype.removeFirst=function(){return this.elms.pop()};OrderedCollection.prototype.remove=function(a){for(var c=0,b=0,d=0;d<this.elms.length;d++){var e=this.elms[d];e!=a?(this.elms[c]=e,c++):b++}for(d=0;d<b;d++)this.elms.pop()};function Strength(a,c){this.strengthValue=a;this.name=c}Strength.stronger=function(a,c){return a.strengthValue<c.strengthValue};Strength.weaker=function(a,c){return a.strengthValue>c.strengthValue}; |
| 14 | +Strength.weakestOf=function(a,c){return this.weaker(a,c)?a:c};Strength.strongest=function(a,c){return this.stronger(a,c)?a:c};Strength.prototype.nextWeaker=function(){switch(this.strengthValue){case 0:return Strength.WEAKEST;case 1:return Strength.WEAK_DEFAULT;case 2:return Strength.NORMAL;case 3:return Strength.STRONG_DEFAULT;case 4:return Strength.PREFERRED;case 5:return Strength.REQUIRED}};Strength.REQUIRED=new Strength(0,"required");Strength.STONG_PREFERRED=new Strength(1,"strongPreferred"); |
| 15 | +Strength.PREFERRED=new Strength(2,"preferred");Strength.STRONG_DEFAULT=new Strength(3,"strongDefault");Strength.NORMAL=new Strength(4,"normal");Strength.WEAK_DEFAULT=new Strength(5,"weakDefault");Strength.WEAKEST=new Strength(6,"weakest");function Constraint(a){this.strength=a}Constraint.prototype.addConstraint=function(){this.addToGraph();planner.incrementalAdd(this)}; |
| 16 | +Constraint.prototype.satisfy=function(a){this.chooseMethod(a);if(!this.isSatisfied())return this.strength==Strength.REQUIRED&&alert("Could not satisfy a required constraint!"),null;this.markInputs(a);var c=this.output(),b=c.determinedBy;null!=b&&b.markUnsatisfied();c.determinedBy=this;planner.addPropagate(this,a)||alert("Cycle encountered");c.mark=a;return b};Constraint.prototype.destroyConstraint=function(){this.isSatisfied()?planner.incrementalRemove(this):this.removeFromGraph()}; |
| 17 | +Constraint.prototype.isInput=function(){return!1};function UnaryConstraint(a,c){UnaryConstraint.superConstructor.call(this,c);this.myOutput=a;this.satisfied=!1;this.addConstraint()}UnaryConstraint.inheritsFrom(Constraint);UnaryConstraint.prototype.addToGraph=function(){this.myOutput.addConstraint(this);this.satisfied=!1};UnaryConstraint.prototype.chooseMethod=function(a){this.satisfied=this.myOutput.mark!=a&&Strength.stronger(this.strength,this.myOutput.walkStrength)}; |
| 18 | +UnaryConstraint.prototype.isSatisfied=function(){return this.satisfied};UnaryConstraint.prototype.markInputs=function(a){};UnaryConstraint.prototype.output=function(){return this.myOutput};UnaryConstraint.prototype.recalculate=function(){this.myOutput.walkStrength=this.strength;this.myOutput.stay=!this.isInput();this.myOutput.stay&&this.execute()};UnaryConstraint.prototype.markUnsatisfied=function(){this.satisfied=!1};UnaryConstraint.prototype.inputsKnown=function(){return!0}; |
| 19 | +UnaryConstraint.prototype.removeFromGraph=function(){null!=this.myOutput&&this.myOutput.removeConstraint(this);this.satisfied=!1};function StayConstraint(a,c){StayConstraint.superConstructor.call(this,a,c)}StayConstraint.inheritsFrom(UnaryConstraint);StayConstraint.prototype.execute=function(){};function EditConstraint(a,c){EditConstraint.superConstructor.call(this,a,c)}EditConstraint.inheritsFrom(UnaryConstraint);EditConstraint.prototype.isInput=function(){return!0}; |
| 20 | +EditConstraint.prototype.execute=function(){};var Direction={NONE:0,FORWARD:1,BACKWARD:-1};function BinaryConstraint(a,c,b){BinaryConstraint.superConstructor.call(this,b);this.v1=a;this.v2=c;this.direction=Direction.NONE;this.addConstraint()}BinaryConstraint.inheritsFrom(Constraint); |
| 21 | +BinaryConstraint.prototype.chooseMethod=function(a){this.v1.mark==a&&(this.direction=this.v2.mark!=a&&Strength.stronger(this.strength,this.v2.walkStrength)?Direction.FORWARD:Direction.NONE);this.v2.mark==a&&(this.direction=this.v1.mark!=a&&Strength.stronger(this.strength,this.v1.walkStrength)?Direction.BACKWARD:Direction.NONE);Strength.weaker(this.v1.walkStrength,this.v2.walkStrength)?this.direction=Strength.stronger(this.strength,this.v1.walkStrength)?Direction.BACKWARD:Direction.NONE:this.direction= |
| 22 | +Strength.stronger(this.strength,this.v2.walkStrength)?Direction.FORWARD:Direction.BACKWARD};BinaryConstraint.prototype.addToGraph=function(){this.v1.addConstraint(this);this.v2.addConstraint(this);this.direction=Direction.NONE};BinaryConstraint.prototype.isSatisfied=function(){return this.direction!=Direction.NONE};BinaryConstraint.prototype.markInputs=function(a){this.input().mark=a};BinaryConstraint.prototype.input=function(){return this.direction==Direction.FORWARD?this.v1:this.v2}; |
| 23 | +BinaryConstraint.prototype.output=function(){return this.direction==Direction.FORWARD?this.v2:this.v1};BinaryConstraint.prototype.recalculate=function(){var a=this.input(),c=this.output();c.walkStrength=Strength.weakestOf(this.strength,a.walkStrength);c.stay=a.stay;c.stay&&this.execute()};BinaryConstraint.prototype.markUnsatisfied=function(){this.direction=Direction.NONE};BinaryConstraint.prototype.inputsKnown=function(a){var c=this.input();return c.mark==a||c.stay||null==c.determinedBy}; |
| 24 | +BinaryConstraint.prototype.removeFromGraph=function(){null!=this.v1&&this.v1.removeConstraint(this);null!=this.v2&&this.v2.removeConstraint(this);this.direction=Direction.NONE};function ScaleConstraint(a,c,b,d,e){this.direction=Direction.NONE;this.scale=c;this.offset=b;ScaleConstraint.superConstructor.call(this,a,d,e)}ScaleConstraint.inheritsFrom(BinaryConstraint); |
| 25 | +ScaleConstraint.prototype.addToGraph=function(){ScaleConstraint.superConstructor.prototype.addToGraph.call(this);this.scale.addConstraint(this);this.offset.addConstraint(this)};ScaleConstraint.prototype.removeFromGraph=function(){ScaleConstraint.superConstructor.prototype.removeFromGraph.call(this);null!=this.scale&&this.scale.removeConstraint(this);null!=this.offset&&this.offset.removeConstraint(this)}; |
| 26 | +ScaleConstraint.prototype.markInputs=function(a){ScaleConstraint.superConstructor.prototype.markInputs.call(this,a);this.scale.mark=this.offset.mark=a};ScaleConstraint.prototype.execute=function(){this.direction==Direction.FORWARD?this.v2.value=this.v1.value*this.scale.value+this.offset.value:this.v1.value=(this.v2.value-this.offset.value)/this.scale.value}; |
| 27 | +ScaleConstraint.prototype.recalculate=function(){var a=this.input(),c=this.output();c.walkStrength=Strength.weakestOf(this.strength,a.walkStrength);c.stay=a.stay&&this.scale.stay&&this.offset.stay;c.stay&&this.execute()};function EqualityConstraint(a,c,b){EqualityConstraint.superConstructor.call(this,a,c,b)}EqualityConstraint.inheritsFrom(BinaryConstraint);EqualityConstraint.prototype.execute=function(){this.output().value=this.input().value}; |
| 28 | +function Variable(a,c){this.value=c||0;this.constraints=new OrderedCollection;this.determinedBy=null;this.mark=0;this.walkStrength=Strength.WEAKEST;this.stay=!0;this.name=a}Variable.prototype.addConstraint=function(a){this.constraints.add(a)};Variable.prototype.removeConstraint=function(a){this.constraints.remove(a);this.determinedBy==a&&(this.determinedBy=null)};function Planner(){this.currentMark=0}Planner.prototype.incrementalAdd=function(a){var c=this.newMark();for(a=a.satisfy(c);null!=a;)a=a.satisfy(c)}; |
| 29 | +Planner.prototype.incrementalRemove=function(a){var c=a.output();a.markUnsatisfied();a.removeFromGraph();a=this.removePropagateFrom(c);c=Strength.REQUIRED;do{for(var b=0;b<a.size();b++){var d=a.at(b);d.strength==c&&this.incrementalAdd(d)}c=c.nextWeaker()}while(c!=Strength.WEAKEST)};Planner.prototype.newMark=function(){return++this.currentMark}; |
| 30 | +Planner.prototype.makePlan=function(a){for(var c=this.newMark(),b=new Plan;0<a.size();){var d=a.removeFirst();d.output().mark!=c&&d.inputsKnown(c)&&(b.addConstraint(d),d.output().mark=c,this.addConstraintsConsumingTo(d.output(),a))}return b};Planner.prototype.extractPlanFromConstraints=function(a){for(var c=new OrderedCollection,b=0;b<a.size();b++){var d=a.at(b);d.isInput()&&d.isSatisfied()&&c.add(d)}return this.makePlan(c)}; |
| 31 | +Planner.prototype.addPropagate=function(a,c){var b=new OrderedCollection;for(b.add(a);0<b.size();){var d=b.removeFirst();if(d.output().mark==c)return this.incrementalRemove(a),!1;d.recalculate();this.addConstraintsConsumingTo(d.output(),b)}return!0}; |
| 32 | +Planner.prototype.removePropagateFrom=function(a){a.determinedBy=null;a.walkStrength=Strength.WEAKEST;a.stay=!0;var c=new OrderedCollection,b=new OrderedCollection;for(b.add(a);0<b.size();){a=b.removeFirst();for(var d=0;d<a.constraints.size();d++){var e=a.constraints.at(d);e.isSatisfied()||c.add(e)}e=a.determinedBy;for(d=0;d<a.constraints.size();d++){var f=a.constraints.at(d);f!=e&&f.isSatisfied()&&(f.recalculate(),b.add(f.output()))}}return c}; |
| 33 | +Planner.prototype.addConstraintsConsumingTo=function(a,c){for(var b=a.determinedBy,d=a.constraints,e=0;e<d.size();e++){var f=d.at(e);f!=b&&f.isSatisfied()&&c.add(f)}};function Plan(){this.v=new OrderedCollection}Plan.prototype.addConstraint=function(a){this.v.add(a)};Plan.prototype.size=function(){return this.v.size()};Plan.prototype.constraintAt=function(a){return this.v.at(a)};Plan.prototype.execute=function(){for(var a=0;a<this.size();a++)this.constraintAt(a).execute()}; |
| 34 | +function chainTest(a){planner=new Planner;for(var c=null,b=null,d=null,e=0;e<=a;e++){var f=new Variable("v"+e);null!=c&&new EqualityConstraint(c,f,Strength.REQUIRED);0==e&&(b=f);e==a&&(d=f);c=f}new StayConstraint(d,Strength.STRONG_DEFAULT);e=new EditConstraint(b,Strength.PREFERRED);a=new OrderedCollection;a.add(e);a=planner.extractPlanFromConstraints(a);for(e=0;100>e;e++)b.value=e,a.execute(),d.value!=e&&alert("Chain test failed.")} |
| 35 | +function projectionTest(a){planner=new Planner;for(var c=new Variable("scale",10),b=new Variable("offset",1E3),d=null,e=null,f=new OrderedCollection,g=0;g<a;g++)d=new Variable("src"+g,g),e=new Variable("dst"+g,g),f.add(e),new StayConstraint(d,Strength.NORMAL),new ScaleConstraint(d,c,b,e,Strength.REQUIRED);change(d,17);1170!=e.value&&alert("Projection 1 failed");change(e,1050);5!=d.value&&alert("Projection 2 failed");change(c,5);for(g=0;g<a-1;g++)f.at(g).value!=5*g+1E3&&alert("Projection 3 failed"); |
| 36 | +change(b,2E3);for(g=0;g<a-1;g++)f.at(g).value!=5*g+2E3&&alert("Projection 4 failed")}function change(a,c){var b=new EditConstraint(a,Strength.PREFERRED),d=new OrderedCollection;d.add(b);for(var d=planner.extractPlanFromConstraints(d),e=0;10>e;e++)a.value=c,d.execute();b.destroyConstraint()}var planner=null;function deltaBlue(){chainTest(100);projectionTest(100)}; |
| 37 | +//////////////////////////////////////////////////////////////////////////////// |
| 38 | +// Runner |
| 39 | +//////////////////////////////////////////////////////////////////////////////// |
| 40 | + |
| 41 | +var success = true; |
| 42 | + |
| 43 | +function NotifyStart(name) { |
| 44 | +} |
| 45 | + |
| 46 | +function NotifyError(name, error) { |
| 47 | + WScript.Echo(name + " : ERROR : " + error.stack); |
| 48 | + success = false; |
| 49 | +} |
| 50 | + |
| 51 | +function NotifyResult(name, score) { |
| 52 | + if (success) { |
| 53 | + WScript.Echo("### SCORE:", score); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +function NotifyScore(score) { |
| 58 | +} |
| 59 | + |
| 60 | +BenchmarkSuite.RunSuites({ |
| 61 | + NotifyStart: NotifyStart, |
| 62 | + NotifyError: NotifyError, |
| 63 | + NotifyResult: NotifyResult, |
| 64 | + NotifyScore: NotifyScore |
| 65 | +}); |
0 commit comments