@@ -25,11 +25,13 @@ test.beforeEach((ctx) => {
2525
2626 const cpuProfiler = createProfiler ( { sandbox, name : 'cpu' } )
2727 const heapProfiler = createProfiler ( { sandbox, name : 'heap' } )
28+ const profilingManager = new ProfilingManager ( { agent, samplingInterval : 60_000 } , { logger } )
2829 ctx . nr = {
2930 agent,
3031 cpuProfiler,
3132 heapProfiler,
3233 logger,
34+ profilingManager,
3335 sandbox
3436 }
3537} )
@@ -41,34 +43,29 @@ test.afterEach((ctx) => {
4143
4244describe ( 'constructor' , ( ) => {
4345 test ( 'should initialize with agent config' , ( t ) => {
44- const { agent } = t . nr
45- const profilingManager = new ProfilingManager ( agent )
46+ const { profilingManager } = t . nr
4647
4748 assert . ok ( profilingManager . config , 'should have config' )
4849 assert . deepStrictEqual ( profilingManager . config , t . nr . agent . config . profiling , 'should store agent config' )
4950 } )
5051
5152 test ( 'should initialize empty profilers map' , ( t ) => {
52- const { agent } = t . nr
53- const profilingManager = new ProfilingManager ( agent )
53+ const { profilingManager } = t . nr
5454
5555 assert . strictEqual ( profilingManager . profilers . size , 0 , 'profilers map should be empty' )
5656 } )
5757} )
5858
5959describe ( 'register' , ( ) => {
6060 test ( 'should be a no-op by default' , ( t ) => {
61- const { agent } = t . nr
62- const profilingManager = new ProfilingManager ( agent )
63-
61+ const { profilingManager } = t . nr
6462 profilingManager . register ( )
6563
6664 assert . strictEqual ( profilingManager . profilers . size , 0 , 'should not add any profilers' )
6765 } )
6866
6967 test ( 'should register the heap and cpu profilers only once' , ( t ) => {
70- const { agent } = t . nr
71- const profilingManager = new ProfilingManager ( agent )
68+ const { profilingManager } = t . nr
7269 profilingManager . config . include = [ 'heap' ]
7370
7471 profilingManager . register ( )
@@ -85,8 +82,7 @@ describe('register', () => {
8582
8683describe ( 'start' , ( ) => {
8784 test ( 'should warn when no profilers are registered' , ( t ) => {
88- const { agent, logger } = t . nr
89- const profilingManager = new ProfilingManager ( agent , { logger } )
85+ const { profilingManager, logger } = t . nr
9086
9187 const started = profilingManager . start ( )
9288 assert . equal ( started , false )
@@ -100,8 +96,7 @@ describe('start', () => {
10096 } )
10197
10298 test ( 'should start all registered profilers' , ( t ) => {
103- const { agent, cpuProfiler, heapProfiler, logger } = t . nr
104- const profilingManager = new ProfilingManager ( agent , { logger } )
99+ const { cpuProfiler, heapProfiler, profilingManager, logger } = t . nr
105100 profilingManager . profilers . set ( 'cpu' , cpuProfiler )
106101 profilingManager . profilers . set ( 'heap' , heapProfiler )
107102 const started = profilingManager . start ( )
@@ -122,8 +117,7 @@ describe('start', () => {
122117
123118describe ( 'stop' , ( ) => {
124119 test ( 'should warn when no profilers are registered' , ( t ) => {
125- const { agent, logger } = t . nr
126- const profilingManager = new ProfilingManager ( agent , { logger } )
120+ const { logger, profilingManager } = t . nr
127121 profilingManager . stop ( )
128122
129123 assert . equal ( logger . warn . callCount , 1 )
@@ -135,8 +129,7 @@ describe('stop', () => {
135129 } )
136130
137131 test ( 'should stop all registered profilers' , ( t ) => {
138- const { agent, cpuProfiler, heapProfiler, logger } = t . nr
139- const profilingManager = new ProfilingManager ( agent , { logger } )
132+ const { cpuProfiler, heapProfiler, logger, profilingManager } = t . nr
140133 profilingManager . profilers . set ( 'cpu' , cpuProfiler )
141134 profilingManager . profilers . set ( 'heap' , heapProfiler )
142135 profilingManager . stop ( )
@@ -148,8 +141,7 @@ describe('stop', () => {
148141 } )
149142
150143 test ( 'should log supportability metric for profiling duration' , ( t ) => {
151- const { agent, cpuProfiler, heapProfiler, logger, sandbox } = t . nr
152- const profilingManager = new ProfilingManager ( agent , { logger } )
144+ const { agent, profilingManager, cpuProfiler, heapProfiler, sandbox } = t . nr
153145 profilingManager . profilers . set ( 'cpu' , cpuProfiler )
154146 profilingManager . profilers . set ( 'heap' , heapProfiler )
155147
@@ -168,8 +160,7 @@ describe('stop', () => {
168160
169161describe ( 'collect' , ( t ) => {
170162 test ( 'should warn and return empty array when no profilers are registered' , async ( t ) => {
171- const { agent, logger } = t . nr
172- const profilingManager = new ProfilingManager ( agent , { logger } )
163+ const { profilingManager, logger } = t . nr
173164
174165 const results = await profilingManager . collect ( )
175166 assert . equal ( results . length , 0 , 'should return empty array' )
@@ -182,8 +173,7 @@ describe('collect', (t) => {
182173 } )
183174
184175 test ( 'should collect data from all registered profilers' , async ( t ) => {
185- const { agent, cpuProfiler, heapProfiler, logger } = t . nr
186- const profilingManager = new ProfilingManager ( agent , { logger } )
176+ const { profilingManager, cpuProfiler, heapProfiler, logger } = t . nr
187177
188178 const expectedCpuData = { type : 'cpu' , data : Buffer . from ( 'cpu-profile-data' ) }
189179 const expectedHeapData = { type : 'heap' , data : Buffer . from ( 'heap-profile-data' ) }
@@ -203,8 +193,7 @@ describe('collect', (t) => {
203193 } )
204194
205195 test ( 'should handle profilers returning undefined or null' , async ( t ) => {
206- const { agent, cpuProfiler, heapProfiler, logger } = t . nr
207- const profilingManager = new ProfilingManager ( agent , { logger } )
196+ const { profilingManager, cpuProfiler, heapProfiler, logger } = t . nr
208197
209198 const expectedCpuData = undefined
210199 const expectedHeapData = null
0 commit comments