test_utils-get-snapshot-totals.js (2807B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 /** 7 * Tests that we use the correct snapshot aggregate value 8 * in `utils.getSnapshotTotals(snapshot)` 9 */ 10 11 const { 12 censusDisplays, 13 viewState, 14 censusState, 15 } = require("resource://devtools/client/memory/constants.js"); 16 const { 17 getSnapshotTotals, 18 } = require("resource://devtools/client/memory/utils.js"); 19 const { 20 takeSnapshotAndCensus, 21 } = require("resource://devtools/client/memory/actions/snapshot.js"); 22 const { 23 setCensusDisplayAndRefresh, 24 } = require("resource://devtools/client/memory/actions/census-display.js"); 25 const { 26 changeView, 27 } = require("resource://devtools/client/memory/actions/view.js"); 28 29 add_task(async function () { 30 const front = new StubbedMemoryFront(); 31 const heapWorker = new HeapAnalysesClient(); 32 await front.attach(); 33 const store = Store(); 34 const { getState, dispatch } = store; 35 36 dispatch(changeView(viewState.CENSUS)); 37 38 await dispatch( 39 setCensusDisplayAndRefresh(heapWorker, censusDisplays.allocationStack) 40 ); 41 42 dispatch(takeSnapshotAndCensus(front, heapWorker)); 43 await waitUntilCensusState(store, s => s.census, [censusState.SAVED]); 44 45 ok( 46 !getState().snapshots[0].census.display.inverted, 47 "Snapshot is not inverted" 48 ); 49 50 const census = getState().snapshots[0].census; 51 let result = aggregate(census.report); 52 const totalBytes = result.bytes; 53 const totalCount = result.count; 54 55 Assert.greater(totalBytes, 0, "counted up bytes in the census"); 56 Assert.greater(totalCount, 0, "counted up count in the census"); 57 58 result = getSnapshotTotals(getState().snapshots[0].census); 59 equal( 60 totalBytes, 61 result.bytes, 62 "getSnapshotTotals reuslted in correct bytes" 63 ); 64 equal( 65 totalCount, 66 result.count, 67 "getSnapshotTotals reuslted in correct count" 68 ); 69 70 dispatch( 71 setCensusDisplayAndRefresh( 72 heapWorker, 73 censusDisplays.invertedAllocationStack 74 ) 75 ); 76 77 await waitUntilCensusState(store, s => s.census, [censusState.SAVING]); 78 await waitUntilCensusState(store, s => s.census, [censusState.SAVED]); 79 ok(getState().snapshots[0].census.display.inverted, "Snapshot is inverted"); 80 81 result = getSnapshotTotals(getState().snapshots[0].census); 82 equal( 83 totalBytes, 84 result.bytes, 85 "getSnapshotTotals reuslted in correct bytes when inverted" 86 ); 87 equal( 88 totalCount, 89 result.count, 90 "getSnapshotTotals reuslted in correct count when inverted" 91 ); 92 }); 93 94 function aggregate(report) { 95 let totalBytes = report.bytes; 96 let totalCount = report.count; 97 for (const child of report.children || []) { 98 const { bytes, count } = aggregate(child); 99 totalBytes += bytes; 100 totalCount += count; 101 } 102 return { bytes: totalBytes, count: totalCount }; 103 }