console-profile.js 1.8 KB
Newer Older
1 2 3 4
// Copyright 2016 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
let {session, contextGroup, Protocol} = InspectorTest.start("Tests that console.profile/profileEnd will record CPU profile when inspector front-end is connected.");
6

7
contextGroup.addScript(`
8 9 10 11 12 13 14 15 16 17 18 19 20 21
function collectProfiles()
{
  console.profile("outer");
  console.profile(42);
  console.profileEnd("outer");
  console.profileEnd(42);
}`);

InspectorTest.fail = function(message)
{
  InspectorTest.log("FAIL: " + message);
  InspectorTest.completeTest();
}

22 23
Protocol.Profiler.enable();
Protocol.Runtime.evaluate({ expression: "collectProfiles()"}).then(didCollectProfiles);
24 25

var headers = [];
26
Protocol.Profiler.onConsoleProfileFinished(function(messageObject)
27 28 29 30 31
{
  headers.push({
    profile: messageObject["params"]["profile"],
    title: messageObject["params"]["title"]
  });
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

function didCollectProfiles(messageObject)
{
  if (headers.length !== 2)
    return InspectorTest.fail("Cannot retrive headers: " + JSON.stringify(messageObject, null, 4));
  for (var i = 0; i < headers.length; i++) {
    if (headers[i].title === "42") {
      checkInnerProfile(headers[i].profile);
      return;
    }
  }
  InspectorTest.fail("Cannot find '42' profile header");
}

function checkInnerProfile(profile)
{
  InspectorTest.log("SUCCESS: retrieved '42' profile");
  if (!findFunctionInProfile(profile.nodes, "collectProfiles"))
    return InspectorTest.fail("collectProfiles function not found in the profile: " + JSON.stringify(profile, null, 4));
  InspectorTest.log("SUCCESS: found 'collectProfiles' function in the profile");
  InspectorTest.completeTest();
}

function findFunctionInProfile(nodes, functionName)
{
  return nodes.some(n => n.callFrame.functionName === functionName);
}