1- const {
2- kStatisticalHistogramRecord,
3- StatisticalHistogram,
4- kStatisticalHistogramFinish,
5- } = require ( "../histogram" ) ;
1+ const { StatisticalHistogram } = require ( "../histogram" ) ;
62
73/**
84 * Formats a byte value into a human-readable string with appropriate units (B, Kb, MB, GB)
@@ -34,17 +30,13 @@ class MemoryPlugin {
3430 /**
3531 * @type {StatisticalHistogram }
3632 */
37- #heapUsedHistogram;
38-
39- constructor ( ) {
40- this . reset ( ) ;
41- }
33+ #heapUsedHistogram = new StatisticalHistogram ( ) ;
4234
4335 isSupported ( ) {
4436 return typeof globalThis . gc === "function" ;
4537 }
4638
47- beforeClockTemplate ( { managed, globalThisVar , contextVar } ) {
39+ beforeClockTemplate ( { managed, context } ) {
4840 if ( managed && ! MemoryPlugin . #WARNING_REPORTED) {
4941 MemoryPlugin . #WARNING_REPORTED = true ;
5042 process . emitWarning (
@@ -54,22 +46,21 @@ class MemoryPlugin {
5446
5547 let code = "" ;
5648
57- code += `${ contextVar } .${ MemoryPlugin . MEMORY_BEFORE_RUN } = 0;\n` ;
58- code += `${ contextVar } .${ MemoryPlugin . MEMORY_AFTER_RUN } = 0;\n` ;
59- code += ` ${ globalThisVar } .gc();\n` ;
60- code += `${ contextVar } .${ MemoryPlugin . MEMORY_BEFORE_RUN } = ${ globalThisVar } .process.memoryUsage();\n` ;
49+ code += `${ context } .${ MemoryPlugin . MEMORY_BEFORE_RUN } = 0;\n` ;
50+ code += `${ context } .${ MemoryPlugin . MEMORY_AFTER_RUN } = 0;\n` ;
51+ code += "globalThis .gc();\n" ;
52+ code += `${ context } .${ MemoryPlugin . MEMORY_BEFORE_RUN } = globalThis .process.memoryUsage();\n` ;
6153
62- return code ;
54+ return [ code ] ;
6355 }
6456
65- afterClockTemplate ( { globalThisVar, contextVar } ) {
66- return `${ contextVar } .${ MemoryPlugin . MEMORY_AFTER_RUN } = ${ globalThisVar } .process.memoryUsage();\n` ;
57+ afterClockTemplate ( { context } ) {
58+ return [
59+ `${ context } .${ MemoryPlugin . MEMORY_AFTER_RUN } = globalThis.process.memoryUsage();\n` ,
60+ ] ;
6761 }
6862
69- onCompleteClock ( result ) {
70- const realIterations = result [ 1 ] ;
71- const context = result [ 2 ] ;
72-
63+ onCompleteBenchmark ( [ , realIterations , context ] ) {
7364 const heapUsed =
7465 context [ MemoryPlugin . MEMORY_AFTER_RUN ] . heapUsed -
7566 context [ MemoryPlugin . MEMORY_BEFORE_RUN ] . heapUsed ;
@@ -80,20 +71,16 @@ class MemoryPlugin {
8071 const memoryAllocated = ( heapUsed + externalUsed ) / realIterations ;
8172
8273 // below 0, we just coerce to be zero
83- this . #heapUsedHistogram[ kStatisticalHistogramRecord ] (
84- Math . max ( 0 , memoryAllocated ) ,
85- ) ;
86- }
87-
88- onCompleteBenchmark ( ) {
89- this . #heapUsedHistogram[ kStatisticalHistogramFinish ] ( ) ;
74+ this . #heapUsedHistogram. record ( Math . max ( 0 , memoryAllocated ) ) ;
9075 }
9176
9277 toString ( ) {
9378 return "MemoryPlugin" ;
9479 }
9580
9681 getReport ( ) {
82+ this . #heapUsedHistogram. finish ( ) ;
83+
9784 return `heap usage=${ formatBytes ( this . #heapUsedHistogram. mean ) } (${ formatBytes ( this . #heapUsedHistogram. min ) } ... ${ formatBytes ( this . #heapUsedHistogram. max ) } )` ;
9885 }
9986
@@ -104,6 +91,10 @@ class MemoryPlugin {
10491 histogram : this . #heapUsedHistogram,
10592 } ;
10693 }
94+
95+ reset ( ) {
96+ this . #heapUsedHistogram = new StatisticalHistogram ( ) ;
97+ }
10798}
10899
109100module . exports = {
0 commit comments