summaryrefslogtreecommitdiff
path: root/deps/v8/test/inspector/cpu-profiler/console-profile-wasm.js
blob: dc96406d4a817920e967a01f4fdafd897495c3d0 (plain)
1
2
3
4
5
6
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
78
79
80
// Copyright 2019 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.

let {session, contextGroup, Protocol} = InspectorTest.start(
    'Test that console profiles contain wasm function names.');

utils.load('test/mjsunit/wasm/wasm-module-builder.js');

// Add fibonacci function.
var builder = new WasmModuleBuilder();
builder.addFunction('fib', kSig_i_i)
    .addBody([
      kExprGetLocal, 0,
      kExprGetLocal, 0,
      kExprI32Const, 2,
      kExprI32LeS,  // i < 2 ?
      kExprBrIf, 0, // --> return i
      kExprI32Const, 1, kExprI32Sub,  // i - 1
      kExprCallFunction, 0, // fib(i - 1)
      kExprGetLocal, 0, kExprI32Const, 2, kExprI32Sub,  // i - 2
      kExprCallFunction, 0, // fib(i - 2)
      kExprI32Add
    ])
    .exportFunc();
let module_bytes = builder.toArray();

function compile(bytes) {
  let buffer = new ArrayBuffer(bytes.length);
  let view = new Uint8Array(buffer);
  for (var i = 0; i < bytes.length; i++) {
    view[i] = bytes[i] | 0;
  }
  let module = new WebAssembly.Module(buffer);
  let instance = new WebAssembly.Instance(module);
  return instance;
}

function checkError(message)
{
  if (message.error) {
    InspectorTest.log("Error: ");
    InspectorTest.logMessage(message);
    InspectorTest.completeTest();
  }
}

(async function test() {
  Protocol.Profiler.enable();
  checkError(await Protocol.Profiler.start());
  let found_fib_in_profile = false;
  let finished_profiles = 0;
  Protocol.Profiler.onConsoleProfileFinished(e => {
    ++finished_profiles;
    if (e.params.profile.nodes.some(n => n.callFrame.functionName === 'fib'))
      found_fib_in_profile = true;
  });
  InspectorTest.log('Compiling wasm.');
  checkError(await Protocol.Runtime.evaluate({
    expression: 'const instance = (' + compile + ')(' +
        JSON.stringify(module_bytes) + ');'
  }));
  InspectorTest.log(
      'Running fib with increasing input until it shows up in the profile.');
  for (let i = 1; !found_fib_in_profile; ++i) {
    checkError(await Protocol.Runtime.evaluate(
        {expression: 'console.profile(\'profile\');'}));
    checkError(await Protocol.Runtime.evaluate(
        {expression: 'instance.exports.fib(' + i + ');'}));
    checkError(await Protocol.Runtime.evaluate(
        {expression: 'console.profileEnd(\'profile\');'}));
    if (finished_profiles != i) {
      InspectorTest.log(
          'Missing consoleProfileFinished message (expected ' + i + ', got ' +
          finished_profiles + ')');
    }
  }
  InspectorTest.log('Found fib in profile.');
  InspectorTest.completeTest();
})().catch(e => InspectorTest.log('caught: ' + e));