Created
July 13, 2012 11:29
-
-
Save mraleph/3104414 to your computer and use it in GitHub Desktop.
stupid GC stressing code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To make the benchmark results predictable, we replace Math.random | |
// with a 100% deterministic alternative. | |
Math.random = (function() { | |
var seed = 49734321; | |
return function() { | |
// Robert Jenkins' 32 bit integer hash function. | |
seed = ((seed + 0x7ed55d16) + (seed << 12)) & 0xffffffff; | |
seed = ((seed ^ 0xc761c23c) ^ (seed >>> 19)) & 0xffffffff; | |
seed = ((seed + 0x165667b1) + (seed << 5)) & 0xffffffff; | |
seed = ((seed + 0xd3a2646c) ^ (seed << 9)) & 0xffffffff; | |
seed = ((seed + 0xfd7046c5) + (seed << 3)) & 0xffffffff; | |
seed = ((seed ^ 0xb55a4f09) ^ (seed >>> 16)) & 0xffffffff; | |
return (seed & 0xfffffff) / 0x10000000; | |
}; | |
})(); | |
function makeRandomKey() { | |
var a = "a".charCodeAt(0); | |
var z = "z".charCodeAt(0); | |
var key = []; | |
for (var i = 0; i < 20; i++) { | |
key.push(a + Math.floor(Math.random() * (z - a + 1))); | |
} | |
return String.fromCharCode.apply(String, key) + Math.floor(Math.random() * 1000); | |
} | |
var totalkeys = 0; | |
var db = new DBSlice; | |
function DBSlice(next) { | |
this.nkeys = 0; | |
this.table = Object.create(null); | |
this.next = next; | |
} | |
function Payload(depth) { | |
if (depth < 3) { | |
this.x = new Payload(depth + 1); | |
this.y = makeRandomKey(); | |
this.z = new Payload(depth + 1); | |
this.k = makeRandomKey(); | |
this.l = 10101; | |
} | |
} | |
function Rand(n) { return Math.floor(Math.random() * n); } | |
DBSlice.prototype.insertNewKey = function () { | |
do { | |
var key = makeRandomKey(); | |
} while (key in this.table); | |
this.nkeys++; | |
totalkeys++; | |
this.table[key] = new Payload(); | |
if (this.nkeys === 100000) { | |
print("100000 keys created (total keys: " + totalkeys + ")"); | |
return new DBSlice(this); | |
} | |
return this; | |
} | |
function permutatedb() { | |
while (true) { | |
switch (Rand(5)) { | |
case 0: | |
case 1: | |
case 2: | |
new Payload(); // make some garbage | |
break; | |
case 3: | |
case 4: | |
for (var i = 0, n = Rand(5) + 5; i < n; i++) db = db.insertNewKey(); // grow database | |
break; | |
} | |
} | |
} | |
permutatedb(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment