js/src/parjs-benchmarks/util.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 DEFAULT_WARMUP = 10
michael@0 2 DEFAULT_MEASURE = 3
michael@0 3 MODE = MODE || "compare" // MODE is often set on the command-line by run.sh
michael@0 4
michael@0 5 /**
michael@0 6 * label: for the printouts
michael@0 7 * w: warmup runs
michael@0 8 * m: measurement runs
michael@0 9 * seq: closure to compute sequentially
michael@0 10 * par: closure to compute in parallel
michael@0 11 */
michael@0 12 function benchmark(label, w, m, seq, par) {
michael@0 13 var SEQ = 1
michael@0 14 var PAR = 2
michael@0 15 var bits = 0
michael@0 16 if (MODE === "seq") { bits = SEQ; }
michael@0 17 else if (MODE === "par") { bits = PAR; }
michael@0 18 else {
michael@0 19 if (MODE !== "compare") {
michael@0 20 print("Invalid MODE, expected seq|par|compare: ", MODE);
michael@0 21 }
michael@0 22 bits = SEQ|PAR;
michael@0 23 }
michael@0 24
michael@0 25 if (mode(SEQ)) {
michael@0 26 print("Warming up sequential runs");
michael@0 27 warmup(w, seq);
michael@0 28
michael@0 29 print("Measuring sequential runs");
michael@0 30 var [seqTimes, seqResult] = measureN(m, seq);
michael@0 31 }
michael@0 32
michael@0 33 if (mode(PAR)) {
michael@0 34 print("Warming up parallel runs");
michael@0 35 warmup(w, par);
michael@0 36
michael@0 37 print("Measuring parallel runs");
michael@0 38 var [parTimes, parResult] = measureN(m, par);
michael@0 39 }
michael@0 40
michael@0 41 if (mode(SEQ|PAR)) {
michael@0 42 // Check correctness
michael@0 43 print("Checking correctness");
michael@0 44 assertStructuralEq(seqResult, parResult);
michael@0 45 }
michael@0 46
michael@0 47 if (mode(SEQ)) {
michael@0 48 var seqAvg = average(seqTimes);
michael@0 49 for (var i = 0; i < seqTimes.length; i++)
michael@0 50 print(label + " SEQUENTIAL MEASUREMENT " + i + ": " + seqTimes[i]);
michael@0 51 print(label + " SEQUENTIAL AVERAGE: " + seqAvg);
michael@0 52 }
michael@0 53
michael@0 54 if (mode(PAR)) {
michael@0 55 var parAvg = average(parTimes);
michael@0 56 for (var i = 0; i < parTimes.length; i++)
michael@0 57 print(label + " PARALLEL MEASUREMENT " + i + ": " + parTimes[i]);
michael@0 58 print(label + " PARALLEL AVERAGE : " + parAvg);
michael@0 59 }
michael@0 60
michael@0 61 if (mode(SEQ|PAR)) {
michael@0 62 print(label + " SEQ/PAR RATIO : " + seqAvg/parAvg);
michael@0 63 print(label + " PAR/SEQ RATIO : " + parAvg/seqAvg);
michael@0 64 print(label + " IMPROVEMENT : " +
michael@0 65 (((seqAvg - parAvg) / seqAvg * 100.0) | 0) + "%");
michael@0 66 }
michael@0 67
michael@0 68 function mode(m) {
michael@0 69 return (bits & m) === m;
michael@0 70 }
michael@0 71 }
michael@0 72
michael@0 73 function measure1(f) {
michael@0 74 var start = new Date();
michael@0 75 result = f();
michael@0 76 var end = new Date();
michael@0 77 return [end.getTime() - start.getTime(), result];
michael@0 78 }
michael@0 79
michael@0 80 function warmup(iters, f) {
michael@0 81 for (var i = 0; i < iters; i++) {
michael@0 82 print(".");
michael@0 83 f();
michael@0 84 }
michael@0 85 }
michael@0 86
michael@0 87 function average(measurements) {
michael@0 88 var sum = measurements.reduce(function (x, y) { return x + y; });
michael@0 89 return sum / measurements.length;
michael@0 90 }
michael@0 91
michael@0 92 function measureN(iters, f) {
michael@0 93 var measurement, measurements = [];
michael@0 94 var result;
michael@0 95
michael@0 96 for (var i = 0; i < iters; i++) {
michael@0 97 [measurement, result] = measure1(f);
michael@0 98 measurements.push(measurement);
michael@0 99 }
michael@0 100
michael@0 101 return [measurements, result];
michael@0 102 }
michael@0 103
michael@0 104 function assertStructuralEq(e1, e2) {
michael@0 105 if (e1 instanceof ParallelArray && e2 instanceof ParallelArray) {
michael@0 106 assertEqParallelArray(e1, e2);
michael@0 107 } else if (typeof(RectArray) != "undefined" &&
michael@0 108 e1 instanceof ParallelArray && e2 instanceof RectArray) {
michael@0 109 assertEqParallelArrayRectArray(e1, e2);
michael@0 110 } else if (typeof(RectArray) != "undefined" &&
michael@0 111 e1 instanceof RectArray && e2 instanceof ParallelArray) {
michael@0 112 assertEqParallelArrayRectArray(e2, e1);
michael@0 113 } else if (typeof(WrapArray) != "undefined" &&
michael@0 114 e1 instanceof ParallelArray && e2 instanceof WrapArray) {
michael@0 115 assertEqParallelArrayWrapArray(e1, e2);
michael@0 116 } else if (typeof(WrapArray) != "undefined" &&
michael@0 117 e1 instanceof WrapArray && e2 instanceof ParallelArray) {
michael@0 118 assertEqParallelArrayWrapArray(e2, e1);
michael@0 119 } else if (e1 instanceof Array && e2 instanceof ParallelArray) {
michael@0 120 assertEqParallelArrayArray(e2, e1);
michael@0 121 } else if (e1 instanceof ParallelArray && e2 instanceof Array) {
michael@0 122 assertEqParallelArrayArray(e1, e2);
michael@0 123 } else if (typeof(RectArray) != "undefined" &&
michael@0 124 e1 instanceof RectArray && e2 instanceof RectArray) {
michael@0 125 assertEqRectArray(e1, e2);
michael@0 126 } else if (typeof(WrapArray) != "undefined" &&
michael@0 127 e1 instanceof WrapArray && e2 instanceof WrapArray) {
michael@0 128 assertEqWrapArray(e1, e2);
michael@0 129 } else if (e1 instanceof Array && e2 instanceof Array) {
michael@0 130 assertEqArray(e1, e2);
michael@0 131 } else if (e1 instanceof Object && e2 instanceof Object) {
michael@0 132 assertEq(e1.__proto__, e2.__proto__);
michael@0 133 for (prop in e1) {
michael@0 134 if (e1.hasOwnProperty(prop)) {
michael@0 135 assertEq(e2.hasOwnProperty(prop), true);
michael@0 136 assertStructuralEq(e1[prop], e2[prop]);
michael@0 137 }
michael@0 138 }
michael@0 139 } else {
michael@0 140 assertEq(e1, e2);
michael@0 141 }
michael@0 142 }
michael@0 143
michael@0 144 function assertEqParallelArrayRectArray(a, b) {
michael@0 145 assertEq(a.shape.length, 2);
michael@0 146 assertEq(a.shape[0], b.width);
michael@0 147 assertEq(a.shape[1], b.height);
michael@0 148 for (var i = 0, w = a.shape[0]; i < w; i++) {
michael@0 149 for (var j = 0, h = a.shape[1]; j < h; j++) {
michael@0 150 assertStructuralEq(a.get(i,j), b.get(i,j));
michael@0 151 }
michael@0 152 }
michael@0 153 }
michael@0 154
michael@0 155 function assertEqParallelArrayWrapArray(a, b) {
michael@0 156 assertEq(a.shape.length, 2);
michael@0 157 assertEq(a.shape[0], b.width);
michael@0 158 assertEq(a.shape[1], b.height);
michael@0 159 for (var i = 0, w = a.shape[0]; i < w; i++) {
michael@0 160 for (var j = 0, h = a.shape[1]; j < h; j++) {
michael@0 161 assertStructuralEq(a.get(i,j), b.get(i,j));
michael@0 162 }
michael@0 163 }
michael@0 164 }
michael@0 165
michael@0 166 function assertEqParallelArrayArray(a, b) {
michael@0 167 assertEq(a.shape.length, 1);
michael@0 168 assertEq(a.length, b.length);
michael@0 169 for (var i = 0, l = a.shape[0]; i < l; i++) {
michael@0 170 assertStructuralEq(a.get(i), b[i]);
michael@0 171 }
michael@0 172 }
michael@0 173
michael@0 174 function assertEqRectArray(a, b) {
michael@0 175 assertEq(a.width, b.width);
michael@0 176 assertEq(a.height, b.height);
michael@0 177 for (var i = 0, w = a.width; i < w; i++) {
michael@0 178 for (var j = 0, h = a.height; j < h; j++) {
michael@0 179 assertStructuralEq(a.get(i,j), b.get(i,j));
michael@0 180 }
michael@0 181 }
michael@0 182 }
michael@0 183
michael@0 184 function assertEqWrapArray(a, b) {
michael@0 185 assertEq(a.width, b.width);
michael@0 186 assertEq(a.height, b.height);
michael@0 187 for (var i = 0, w = a.width; i < w; i++) {
michael@0 188 for (var j = 0, h = a.height; j < h; j++) {
michael@0 189 assertStructuralEq(a.get(i,j), b.get(i,j));
michael@0 190 }
michael@0 191 }
michael@0 192 }
michael@0 193
michael@0 194 function assertEqArray(a, b) {
michael@0 195 assertEq(a.length, b.length);
michael@0 196 for (var i = 0, l = a.length; i < l; i++) {
michael@0 197 assertStructuralEq(a[i], b[i]);
michael@0 198 }
michael@0 199 }
michael@0 200
michael@0 201 function assertEqParallelArray(a, b) {
michael@0 202 assertEq(a instanceof ParallelArray, true);
michael@0 203 assertEq(b instanceof ParallelArray, true);
michael@0 204
michael@0 205 var shape = a.shape;
michael@0 206 assertEqArray(shape, b.shape);
michael@0 207
michael@0 208 function bump(indices) {
michael@0 209 var d = indices.length - 1;
michael@0 210 while (d >= 0) {
michael@0 211 if (++indices[d] < shape[d])
michael@0 212 break;
michael@0 213 indices[d] = 0;
michael@0 214 d--;
michael@0 215 }
michael@0 216 return d >= 0;
michael@0 217 }
michael@0 218
michael@0 219 var iv = shape.map(function () { return 0; });
michael@0 220 do {
michael@0 221 var e1 = a.get.apply(a, iv);
michael@0 222 var e2 = b.get.apply(b, iv);
michael@0 223 assertStructuralEq(e1, e2);
michael@0 224 } while (bump(iv));
michael@0 225 }

mercurial