Skip to content

Instantly share code, notes, and snippets.

@jorendorff
Created July 14, 2011 20:15
Show Gist options
  • Save jorendorff/1083337 to your computer and use it in GitHub Desktop.
Save jorendorff/1083337 to your computer and use it in GitHub Desktop.
getExecutionCounts test code
// Basic getExecutionCounts test.
var g = newGlobal('new-compartment');
var dbg = Debugger(g);
var s;
dbg.onNewScript = function (script) { s = script; };
var f = g.Function("i",
"line0 = Error().lineNumber;\n" +
"var s = 0;\n" + // line0 + 1
"for (var a = 0; a < i; a++)\n" + // line0 + 2
" s += a;\n" + // line0 + 3
"return s;\n");
var N = 99;
f(N);
var counts = s.getExecutionCounts();
print(uneval(counts));
assertEq(Array.isArray(counts), true);
// Compile the total number of times each line is hit. Some instructions are
// underreported or aren't reported at all; this technique of using the maximum
// number of hits per line copes fairly nicely with the noise.
var hitsByLine = [];
for (var i = 0; i < counts.length; i++) {
if (i in counts) {
var lineno = s.getOffsetLine(i);
var data = counts[i];
var sum = data[0] + data[1] + data[2];
if (lineno in hitsByLine)
hitsByLine[lineno] = Math.max(hitsByLine[lineno], sum);
else
hitsByLine[lineno] = sum;
}
}
assertEq(hitsByLine[g.line0 + 1], 1);
assertEq(hitsByLine[g.line0 + 3], N);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment