Skip to content

Instantly share code, notes, and snippets.

@tomnomnom
Created November 3, 2012 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomnomnom/4007744 to your computer and use it in GitHub Desktop.
Save tomnomnom/4007744 to your computer and use it in GitHub Desktop.
Splitting test files into roughly similar buckets
// Files vs. tests in file
var files = {
'one.js': 12,
'two.js': 22,
'three.js': 14,
'four.js': 10,
'five.js': 19,
'six.js': 10,
'seven.js': 18,
'eight.js': 10,
'nine.js': 10,
'ten.js': 50,
'eleven.js': 2,
'twelve.js': 32,
'thirteen.js': 9,
'fourteen.js': 12,
'fifteen.js': 40,
'sixteen.js': 23,
'seventeen.js': 44,
'eighteen.js': 25,
'nineteen.js': 12,
'twenty.js': 29,
'twentyone.js': 88,
};
// Split a "ring" in two as well as possible
var halve = function(ring){
var midPoint = ring.length / 2;
var upperSlicePoint = Math.ceil(midPoint);
var lowerSlicePoint = Math.floor(midPoint);
var midFile = ring[upperSlicePoint];
var upperFile = midFile;
while (upperFile == midFile){
upperSlicePoint++;
upperFile = ring[upperSlicePoint];
}
var lowerFile = midFile;
while (lowerFile == midFile){
lowerSlicePoint--;
lowerFile = ring[lowerSlicePoint];
}
// Pick the upper or lower slice point; whichever is closest to the middle
if ((midPoint - lowerSlicePoint) > (upperSlicePoint - midPoint)){
return [
ring.slice(0, upperSlicePoint),
ring.slice(upperSlicePoint, ring.length)
];
} else {
return [
ring.slice(0, lowerSlicePoint+1),
ring.slice(lowerSlicePoint+1, ring.length)
];
}
};
// Get unique elements from an array
var uniq = function(items){
var map = {};
for (var i in items){
map[items[i]] = true;
}
var keys = [];
for (var key in map){
if (map.hasOwnProperty(key)){
keys.push(key);
}
}
return keys;
};
////////////////////////
// Meat is below here //
////////////////////////
// Not actually a ring, but fuck it
var ring = [];
// Fill the "ring" with filenames; repeat each file testCount times
for (var file in files){
var testCount = files[file];
for (var i = 0; i < testCount; ++i){
ring.push(file);
}
}
// Split into 4 buckets
var halves = halve(ring);
var buckets = halve(halves[0]).concat(halve(halves[1]));
// Remove duplicates from the buckets and we're done.
buckets = buckets.map(uniq);
// See how many tests (and files) are in each bucket
for (var i in buckets){
var bucket = buckets[i];
var testCount = 0;
for (var j in bucket){
testCount += files[bucket[j]];
}
console.log("Bucket "+ i +" contains "+ bucket.length +" files and "+ testCount +" tests");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment