summaryrefslogtreecommitdiff
path: root/deps/v8/test/inspector/type-profiler/type-profile-start-stop.js
blob: be4e0bdfd934041ddb62e9dd5e6e7bbaa37a05b6 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
166
167
168
169
170
171
172
173
174
175
176
177
178
// 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.

// Flags: --type-profile

const source1 =
  `
function g(a, b, c) {
  return 'first';
};
g({}, [], null);
`;

const source2 =
  `
function f(a) {
  return 'second';
};
f(null);
`;

let {session, contextGroup, Protocol} = InspectorTest.start("Turn " +
  "Profiler.startTypeProfile on and off.");

InspectorTest.runAsyncTestSuite([
  async function testTypeProfile() {
    Protocol.Runtime.enable();
    let {result: {scriptId}} = await Protocol.Runtime.compileScript({
      expression: source1,
      sourceURL: arguments.callee.name, persistScript: true
    });
    await Protocol.Profiler.enable();

    // Start, run, take.
    await Protocol.Profiler.startTypeProfile();
    Protocol.Runtime.runScript({scriptId});

    let typeProfiles = await Protocol.Profiler.takeTypeProfile();
    await session.logTypeProfile(typeProfiles.result.result[0],
      source1);

    Protocol.Profiler.stopTypeProfile();
    Protocol.Profiler.disable();
    await Protocol.Runtime.disable();
  },
  async function testTypeProfileFromDifferentSource() {
    Protocol.Runtime.enable();
    let {result: {scriptId}} = await Protocol.Runtime.compileScript({
      expression: source2,
      sourceURL: arguments.callee.name, persistScript: true
    });
    await Protocol.Profiler.enable();

    // Start, run different script, take.
    await Protocol.Profiler.startTypeProfile();
    Protocol.Runtime.runScript({scriptId});

    let typeProfiles = await Protocol.Profiler.takeTypeProfile();
    await session.logTypeProfile(typeProfiles.result.result[0],
      source2);

    Protocol.Profiler.stopTypeProfile();
    Protocol.Profiler.disable();
    await Protocol.Runtime.disable();
  },
  async function testStopTypeProfileDeletesFeedback() {
    Protocol.Runtime.enable();
    let {result: {scriptId}} = await Protocol.Runtime.compileScript({
      expression: source1,
      sourceURL: arguments.callee.name, persistScript: true
    });
    await Protocol.Profiler.enable();

    // Start, run, stop.
    await Protocol.Profiler.startTypeProfile();
    Protocol.Runtime.runScript({scriptId});
    await Protocol.Profiler.stopTypeProfile();

    // Start, take. Should be empty, because no code was run.
    await Protocol.Profiler.startTypeProfile();
    let typeProfiles = await Protocol.Profiler.takeTypeProfile();
    InspectorTest.logMessage(typeProfiles.result.result);
    await Protocol.Profiler.stopTypeProfile();

    Protocol.Profiler.disable();
    await Protocol.Runtime.disable();
  },
  async function testTypeProfileWithoutStartingItFirst() {
    Protocol.Runtime.enable();
    let {result: {scriptId}} = await Protocol.Runtime.compileScript({ expression: source1,
      sourceURL: arguments.callee.name, persistScript: true });
    Protocol.Runtime.runScript({ scriptId });
    await Protocol.Profiler.enable();

    // This should return an error because type profile was never started.
    let typeProfiles = await Protocol.Profiler.takeTypeProfile();
    InspectorTest.logObject(typeProfiles.error.message);

    Protocol.Profiler.disable();
    await Protocol.Runtime.disable();
  },
  async function testTypeProfileAfterStoppingIt() {
    Protocol.Runtime.enable();
    let {result: {scriptId}} = await Protocol.Runtime.compileScript({ expression: source1,
      sourceURL: arguments.callee.name, persistScript: true });
    Protocol.Runtime.runScript({ scriptId });
    await Protocol.Profiler.enable();
    await Protocol.Profiler.startTypeProfile();

    // Make sure that this turns off type profile.
    await Protocol.Profiler.stopTypeProfile();

    // This should return an error because type profile was stopped.
    let typeProfiles = await Protocol.Profiler.takeTypeProfile();
    InspectorTest.logObject(typeProfiles.error.message);

    Protocol.Profiler.disable();
    await Protocol.Runtime.disable();
  },
  async function testStartTypeProfileAfterRunning() {
    Protocol.Runtime.enable();
    let {result: {scriptId}} = await Protocol.Runtime.compileScript({
      expression: source1,
      sourceURL: arguments.callee.name, persistScript: true
    });
    Protocol.Runtime.runScript({scriptId});

    await Protocol.Profiler.enable();
    await Protocol.Profiler.startTypeProfile();

    let typeProfiles = await Protocol.Profiler.takeTypeProfile();

    // This should be empty because type profile was started after compilation.
    // Only the outer script is annotated with return value "string" because
    // that does not depend on runScript().
    InspectorTest.logMessage(typeProfiles);

    Protocol.Profiler.stopTypeProfile();
    Protocol.Profiler.disable();
    await Protocol.Runtime.disable();
  },
  async function testTypeProfileForTwoSources() {
    Protocol.Runtime.enable();
    let {result: {scriptId: scriptId1}} = await Protocol.Runtime.compileScript({
      expression: source1,
      sourceURL: arguments.callee.name, persistScript: true
    });
    let {result: {scriptId: scriptId2}} = await Protocol.Runtime.compileScript({
      expression: source2,
      sourceURL: arguments.callee.name, persistScript: true
    });
    await Protocol.Profiler.enable();

    // Start, run different script, take.
    await Protocol.Profiler.startTypeProfile();
    Protocol.Runtime.runScript({scriptId: scriptId1});
    Protocol.Runtime.runScript({scriptId: scriptId2});

    let typeProfiles = await Protocol.Profiler.takeTypeProfile();
    await session.logTypeProfile(typeProfiles.result.result[0],
      source1);
    await session.logTypeProfile(typeProfiles.result.result[1],
      source2);

    Protocol.Profiler.stopTypeProfile();
    Protocol.Profiler.disable();
    await Protocol.Runtime.disable();
  },
  async function testStopTwice() {
    Protocol.Runtime.enable();
    await Protocol.Profiler.enable();
    await Protocol.Profiler.stopTypeProfile();
    await Protocol.Profiler.stopTypeProfile();
    Protocol.Profiler.disable();
    await Protocol.Runtime.disable();
  },
]);