Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Make collecting memory statistic a promise.
Browse files Browse the repository at this point in the history
  • Loading branch information
jodator committed Jan 21, 2019
1 parent 782f26a commit f8230e9
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions tests/_utils/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* For licensing, see LICENSE.md.
*/

/* global window, document */
/* global window, document, setTimeout */

/**
* @param {Function} callback Callback with test suit body
Expand Down Expand Up @@ -47,9 +47,11 @@ function runTest( editorCreator ) {
// First editor creation needed to load all editor code,css into the memory (it is reported as used heap memory)
.then( createEditor )
.then( editor => {
memoryAfterFirstStart = collectMemoryStats();
return collectMemoryStats().then( mem => {
memoryAfterFirstStart = mem;

return editor;
return editor;
} );
} )
.then( destroy )
// Run create-wait-destroy multiple times. Multiple runs to grow memory significantly even on smaller leaks.
Expand All @@ -64,11 +66,14 @@ function runTest( editorCreator ) {
.then( testAndDestroy )
.then( testAndDestroy )
.then( () => {
const memory = collectMemoryStats();

const memoryDifference = memory.usedJSHeapSize - memoryAfterFirstStart.usedJSHeapSize;

expect( memoryDifference, 'used heap size should not grow' ).to.be.at.most( 0 );
return new Promise( resolve => {
collectMemoryStats().then( memory => {
const memoryDifference = memory.usedJSHeapSize - memoryAfterFirstStart.usedJSHeapSize;

expect( memoryDifference, 'used heap size should not grow' ).to.be.at.most( 0 );
resolve();
} );
} );
} );

function testAndDestroy() {
Expand Down Expand Up @@ -102,16 +107,20 @@ function destroy( editor ) {
}

function collectMemoryStats() {
// Enforce garbage collection before recording memory stats.
window.gc();

const memeInfo = window.performance.memory;

return {
totalJSHeapSize: memeInfo.totalJSHeapSize,
usedJSHeapSize: memeInfo.usedJSHeapSize,
jsHeapSizeLimit: memeInfo.jsHeapSizeLimit
};
return new Promise( resolve => {
// Enforce garbage collection before recording memory stats.
window.gc();

setTimeout( () => {
const memeInfo = window.performance.memory;

resolve( {
totalJSHeapSize: memeInfo.totalJSHeapSize,
usedJSHeapSize: memeInfo.usedJSHeapSize,
jsHeapSizeLimit: memeInfo.jsHeapSizeLimit
} );
}, 500 );
} );
}

// Will skip test suite if not in compatible browser.
Expand Down

0 comments on commit f8230e9

Please sign in to comment.