|
| 1 | +// Copyright 2019 the V8 project authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +let {session, contextGroup, Protocol} = InspectorTest.start( |
| 6 | + 'Test RunTimeCallStats collection using Profiler.getRuntimeCallStats.'); |
| 7 | + |
| 8 | +var source = |
| 9 | +` |
| 10 | +function fib(x) { |
| 11 | + if (x < 2) return 1; |
| 12 | + return fib(x-1) + fib(x-2); |
| 13 | +} |
| 14 | +fib(5); |
| 15 | +`; |
| 16 | + |
| 17 | +function buildCounterMap(result) { |
| 18 | + let counterMap = new Map(); |
| 19 | + |
| 20 | + let counters = result.result.result; |
| 21 | + for (const {name, value} of counters) { |
| 22 | + counterMap.set(name, value); |
| 23 | + } |
| 24 | + |
| 25 | + return counterMap; |
| 26 | +} |
| 27 | + |
| 28 | +function compareCounterMaps(counterMap, counterMap2) { |
| 29 | + // Check for counters that are present in the first map but are not found |
| 30 | + // in the the second map |
| 31 | + for (let counter of counterMap.keys()) { |
| 32 | + if (!counterMap2.has(counter)) { |
| 33 | + InspectorTest.log(`Counter ${counter} is missing`); |
| 34 | + return false; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // Check for the counter value changes |
| 39 | + let counterValueIncreased = false; |
| 40 | + for (let [counter, value2] of counterMap2) { |
| 41 | + let value = counterMap.get(counter); |
| 42 | + if (value !== undefined) { |
| 43 | + if (value2 < value) { |
| 44 | + InspectorTest.log(`Counter ${counter} value decreased: ${value} -> ${value2}`); |
| 45 | + return false; |
| 46 | + } |
| 47 | + if (value2 > value) { |
| 48 | + counterValueIncreased = true; |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + if (!counterValueIncreased && counterMap.size === counterMap2.size) { |
| 54 | + InspectorTest.log(`No counter values has increased or added`); |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + return true; |
| 59 | +} |
| 60 | + |
| 61 | +(async function test() { |
| 62 | + await Protocol.Runtime.ensable(); |
| 63 | + await Protocol.Profiler.enableRuntimeCallStats(); |
| 64 | + |
| 65 | + let counterMap = buildCounterMap(await Protocol.Profiler.getRuntimeCallStats()); |
| 66 | + |
| 67 | + await Protocol.Runtime.evaluate({ expression: source, sourceURL: arguments.callee.name, persistScript: true }); |
| 68 | + |
| 69 | + let counterMap2 = buildCounterMap(await Protocol.Profiler.getRuntimeCallStats()); |
| 70 | + const check1 = compareCounterMaps(counterMap, counterMap2); |
| 71 | + |
| 72 | + await Protocol.Runtime.evaluate({ expression: source, sourceURL: arguments.callee.name, persistScript: true }); |
| 73 | + |
| 74 | + let counterMap3 = buildCounterMap(await Protocol.Profiler.getRuntimeCallStats()); |
| 75 | + const check2 = compareCounterMaps(counterMap2, counterMap3); |
| 76 | + |
| 77 | + await Protocol.Profiler.disableRuntimeCallStats(); |
| 78 | + await Protocol.Runtime.disable(); |
| 79 | + |
| 80 | + InspectorTest.log(check1 && check2 ? 'PASSED' : 'FAILED'); |
| 81 | + |
| 82 | + InspectorTest.completeTest(); |
| 83 | +})().catch(e => InspectorTest.log('caught: ' + e)); |
0 commit comments