coverage-block.js 10.8 KB
Newer Older
1 2 3 4
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
// Flags: --allow-natives-syntax --no-always-opt --opt
6
// Flags: --no-stress-flush-bytecode
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

var source =
`
function fib(x) {
  if (x < 2) return 1;
  return fib(x-1) + fib(x-2);
}
function is_optimized(f) {
  return (%GetOptimizationStatus(f) & 16) ? "optimized" : "unoptimized";
}
(function iife() {
  return 1;
})();
fib(5);
`;

var break_source =
`
function g() {
  debugger;
}
function f(x) {
  if (x == 0) g();
  else f(x - 1);
}
function h() {
  g();
}
f(3);
`;

var nested =
`
var f = (function outer() {
  function nested_0() {
    return function nested_1() {
      return function nested_2() {
        return function nested_3() {}
      }
    }
  }
  function nested_4() {}
  return nested_0();
})();
f()()();
`;

let {session, contextGroup, Protocol} = InspectorTest.start("Test collecting code coverage data with Profiler.collectCoverage.");

function ClearAndGC() {
  return Protocol.Runtime.evaluate({ expression: "fib = g = f = h = is_optimized = null;" })
             .then(GC);
}

function GC() {
  return Protocol.HeapProfiler.collectGarbage();
}

function LogSorted(message) {
  message.result.result.sort((a, b) => parseInt(a.scriptId) - parseInt(b.scriptId));
  return InspectorTest.logMessage(message);
}

InspectorTest.runTestSuite([
  function testPreciseCountBaseline(next)
  {
    Protocol.Runtime.enable()
      .then(() => Protocol.Runtime.compileScript({ expression: source, sourceURL: arguments.callee.name, persistScript: true }))
      .then((result) => Protocol.Runtime.runScript({ scriptId: result.result.scriptId }))
      .then(GC)
      .then(Protocol.Profiler.enable)
78
      .then(() => Protocol.Profiler.startPreciseCoverage({callCount: true, detailed: true}))
79 80 81 82 83 84 85 86 87 88 89 90 91 92
      .then(Protocol.Profiler.takePreciseCoverage)
      .then(LogSorted)
      .then(Protocol.Profiler.takePreciseCoverage)
      .then(LogSorted)
      .then(Protocol.Profiler.stopPreciseCoverage)
      .then(Protocol.Profiler.disable)
      .then(Protocol.Runtime.disable)
      .then(ClearAndGC)
      .then(next);
  },
  function testPreciseCountCoverage(next)
  {
    Protocol.Runtime.enable()
      .then(Protocol.Profiler.enable)
93
      .then(() => Protocol.Profiler.startPreciseCoverage({callCount: true, detailed: true}))
94 95 96 97 98 99 100 101 102 103 104 105 106 107
      .then(() => Protocol.Runtime.compileScript({ expression: source, sourceURL: arguments.callee.name, persistScript: true }))
      .then((result) => Protocol.Runtime.runScript({ scriptId: result.result.scriptId }))
      .then(InspectorTest.logMessage)
      .then(ClearAndGC)
      .then(Protocol.Profiler.takePreciseCoverage)
      .then(LogSorted)
      .then(Protocol.Profiler.takePreciseCoverage)
      .then(LogSorted)
      .then(Protocol.Profiler.stopPreciseCoverage)
      .then(Protocol.Profiler.disable)
      .then(Protocol.Runtime.disable)
      .then(ClearAndGC)
      .then(next);
  },
108 109 110 111
  function testPreciseCountCoverageIncremental(next)
  {
    Protocol.Runtime.enable()
      .then(Protocol.Profiler.enable)
112
      .then(() => Protocol.Profiler.startPreciseCoverage({callCount: true, detailed: true}))
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
      .then(() => Protocol.Runtime.compileScript({ expression: source, sourceURL: arguments.callee.name, persistScript: true }))
      .then((result) => Protocol.Runtime.runScript({ scriptId: result.result.scriptId }))
      .then(InspectorTest.logMessage)
      .then(Protocol.Profiler.takePreciseCoverage)
      .then(LogSorted)
      .then(() => Protocol.Runtime.evaluate({ expression: "is_optimized(fib)" }))
      .then(message => InspectorTest.logMessage(message))
      .then(() => Protocol.Runtime.evaluate({ expression: "fib(20)" }))
      .then(message => InspectorTest.logMessage(message))
      .then(() => Protocol.Runtime.evaluate({ expression: "is_optimized(fib)" }))
      .then(message => InspectorTest.logMessage(message))
      .then(Protocol.Profiler.takePreciseCoverage)
      .then(LogSorted)
      .then(Protocol.Profiler.stopPreciseCoverage)
      .then(Protocol.Profiler.disable)
      .then(Protocol.Runtime.disable)
      .then(ClearAndGC)
      .then(next);
  },
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
  function testPreciseCoverageFail(next)
  {
    Protocol.Runtime.enable()
      .then(Protocol.Profiler.enable)
      .then(() => Protocol.Runtime.compileScript({ expression: source, sourceURL: arguments.callee.name, persistScript: true }))
      .then((result) => Protocol.Runtime.runScript({ scriptId: result.result.scriptId }))
      .then(InspectorTest.logMessage)
      .then(ClearAndGC)
      .then(Protocol.Profiler.takePreciseCoverage)
      .then(InspectorTest.logMessage)
      .then(Protocol.Profiler.disable)
      .then(Protocol.Runtime.disable)
      .then(ClearAndGC)
      .then(next);
  },
  function testBestEffortCoverage(next)
  {
    Protocol.Runtime.enable()
      .then(() => Protocol.Runtime.compileScript({ expression: source, sourceURL: arguments.callee.name, persistScript: true }))
      .then((result) => Protocol.Runtime.runScript({ scriptId: result.result.scriptId }))
      .then(InspectorTest.logMessage)
      .then(ClearAndGC)
      .then(Protocol.Profiler.getBestEffortCoverage)
      .then(LogSorted)
      .then(Protocol.Profiler.getBestEffortCoverage)
      .then(LogSorted)
      .then(Protocol.Runtime.disable)
      .then(ClearAndGC)
      .then(next);
  },
  function testBestEffortCoverageWithPreciseBinaryEnabled(next)
  {
    Protocol.Runtime.enable()
    .then(Protocol.Profiler.enable)
166
    .then(() => Protocol.Profiler.startPreciseCoverage({detailed: true}))
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    .then(() => Protocol.Runtime.compileScript({ expression: source, sourceURL: arguments.callee.name, persistScript: true }))
    .then((result) => Protocol.Runtime.runScript({ scriptId: result.result.scriptId }))
    .then(InspectorTest.logMessage)
    .then(ClearAndGC)
    .then(Protocol.Profiler.getBestEffortCoverage)
    .then(LogSorted)
    .then(Protocol.Profiler.getBestEffortCoverage)
    .then(LogSorted)
    .then(ClearAndGC)
    .then(Protocol.Profiler.stopPreciseCoverage)
    .then(Protocol.Profiler.disable)
    .then(Protocol.Runtime.disable)
    .then(ClearAndGC)
    .then(next);
  },
  function testBestEffortCoverageWithPreciseCountEnabled(next)
  {
    Protocol.Runtime.enable()
      .then(Protocol.Profiler.enable)
186
      .then(() => Protocol.Profiler.startPreciseCoverage({callCount: true, detailed: true}))
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
      .then(() => Protocol.Runtime.compileScript({ expression: source, sourceURL: arguments.callee.name, persistScript: true }))
      .then((result) => Protocol.Runtime.runScript({ scriptId: result.result.scriptId }))
      .then(InspectorTest.logMessage)
      .then(ClearAndGC)
      .then(Protocol.Profiler.getBestEffortCoverage)
      .then(LogSorted)
      .then(Protocol.Profiler.getBestEffortCoverage)
      .then(LogSorted)
      .then(ClearAndGC)
      .then(Protocol.Profiler.stopPreciseCoverage)
      .then(Protocol.Profiler.disable)
      .then(Protocol.Runtime.disable)
      .then(ClearAndGC)
      .then(next);
  },
  function testEnablePreciseCountCoverageAtPause(next)
  {
    function handleDebuggerPause() {
      Protocol.Profiler.enable()
206
          .then(() => Protocol.Profiler.startPreciseCoverage({callCount: true, detailed: true}))
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
          .then(Protocol.Debugger.resume)
    }
    Protocol.Debugger.enable();
    Protocol.Debugger.oncePaused().then(handleDebuggerPause);
    Protocol.Runtime.enable()
      .then(() => Protocol.Runtime.compileScript({ expression: break_source, sourceURL: arguments.callee.name, persistScript: true }))
      .then((result) => Protocol.Runtime.runScript({ scriptId: result.result.scriptId }))
      .then(InspectorTest.logMessage)
      .then(ClearAndGC)
      .then(Protocol.Profiler.takePreciseCoverage)
      .then(LogSorted)
      .then(ClearAndGC)
      .then(Protocol.Profiler.stopPreciseCoverage)
      .then(Protocol.Profiler.disable)
      .then(Protocol.Runtime.disable)
      .then(Protocol.Debugger.disable)
      .then(ClearAndGC)
      .then(next);
  },
  function testPreciseBinaryCoverage(next)
  {
    Protocol.Runtime.enable()
      .then(Protocol.Profiler.enable)
230
      .then(() => Protocol.Profiler.startPreciseCoverage({detailed: true}))
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
      .then(() => Protocol.Runtime.compileScript({ expression: source, sourceURL: arguments.callee.name, persistScript: true }))
      .then((result) => Protocol.Runtime.runScript({ scriptId: result.result.scriptId }))
      .then(InspectorTest.logMessage)
      .then(Protocol.Profiler.takePreciseCoverage)
      .then(LogSorted)
      .then(() => Protocol.Runtime.evaluate({ expression: "is_optimized(fib)" }))
      .then(message => InspectorTest.logMessage(message))
      .then(() => Protocol.Runtime.evaluate({ expression: "fib(20)" }))
      .then(message => InspectorTest.logMessage(message))
      .then(() => Protocol.Runtime.evaluate({ expression: "is_optimized(fib)" }))
      .then(message => InspectorTest.logMessage(message))
      .then(Protocol.Profiler.takePreciseCoverage)
      .then(LogSorted)
      .then(Protocol.Profiler.stopPreciseCoverage)
      .then(Protocol.Profiler.disable)
      .then(Protocol.Runtime.disable)
      .then(ClearAndGC)
      .then(next);
  },
  function testPreciseEmptyScriptCoverageEntries(next)
  {
    // Enabling the debugger holds onto script objects even though its
    // functions can be garbage collected. We would get empty ScriptCoverage
    // entires unless we remove them.
    Protocol.Debugger.enable()
      .then(Protocol.Runtime.enable)
      .then(() => Protocol.Runtime.compileScript({ expression: source, sourceURL: arguments.callee.name, persistScript: true }))
      .then((result) => Protocol.Runtime.runScript({ scriptId: result.result.scriptId }))
      .then(ClearAndGC)
      .then(Protocol.Profiler.enable)
261
      .then(() => Protocol.Profiler.startPreciseCoverage({detailed: true}))
262 263 264 265 266 267 268 269 270 271 272 273 274
      .then(Protocol.Profiler.takePreciseCoverage)
      .then(LogSorted)
      .then(Protocol.Profiler.stopPreciseCoverage)
      .then(Protocol.Profiler.disable)
      .then(Protocol.Runtime.disable)
      .then(Protocol.Debugger.disable)
      .then(ClearAndGC)
      .then(next);
  },
  function testPreciseCountCoveragePartial(next)
  {
    Protocol.Runtime.enable()
      .then(Protocol.Profiler.enable)
275
      .then(() => Protocol.Profiler.startPreciseCoverage({callCount: true, detailed: true}))
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
      .then(() => Protocol.Runtime.compileScript({ expression: nested, sourceURL: arguments.callee.name, persistScript: true }))
      .then((result) => Protocol.Runtime.runScript({ scriptId: result.result.scriptId }))
      .then(InspectorTest.logMessage)
      .then(Protocol.Profiler.takePreciseCoverage)
      .then(LogSorted)
      .then(() => Protocol.Runtime.evaluate({ expression: "f()" }))
      .then(Protocol.Profiler.takePreciseCoverage)
      .then(LogSorted)
      .then(Protocol.Profiler.stopPreciseCoverage)
      .then(Protocol.Profiler.disable)
      .then(Protocol.Runtime.disable)
      .then(ClearAndGC)
      .then(next);
  },
]);