@platformatic/wattpm-pprof-capture 0.0.1 → 3.0.0-alpha.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,196 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity granting the License.
13
+
14
+ "Legal Entity" shall mean the union of the acting entity and all
15
+ other entities that control, are controlled by, or are under common
16
+ control with that entity. For the purposes of this definition,
17
+ "control" means (i) the power, direct or indirect, to cause the
18
+ direction or management of such entity, whether by contract or
19
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
20
+ outstanding shares, or (iii) beneficial ownership of such entity.
21
+
22
+ "You" (or "Your") shall mean an individual or Legal Entity
23
+ exercising permissions granted by this License.
24
+
25
+ "Source" shall mean the preferred form for making modifications,
26
+ including but not limited to software source code, documentation
27
+ source, and configuration files.
28
+
29
+ "Object" shall mean any form resulting from mechanical
30
+ transformation or translation of a Source form, including but
31
+ not limited to compiled object code, generated documentation,
32
+ and conversions to other media types.
33
+
34
+ "Work" shall mean the work of authorship, whether in Source or
35
+ Object form, made available under the License, as indicated by a
36
+ copyright notice that is included in or attached to the work
37
+ (which shall not include communications that are clearly marked or
38
+ otherwise designated in writing by the author as "Not a License").
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based upon (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and derivative works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control
57
+ systems, and issue tracking systems that are managed by, or on behalf
58
+ of, the Licensor for the purpose of discussing and improving the Work,
59
+ but excluding communication that is clearly marked or otherwise
60
+ designated in writing by the author as "Not a License".
61
+
62
+ 2. Grant of Copyright License. Subject to the terms and conditions of
63
+ this License, each Contributor hereby grants to You a perpetual,
64
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
65
+ copyright license to use, reproduce, modify, display, perform,
66
+ sublicense, and distribute the Work and derivative works thereof in
67
+ Source and Object form.
68
+
69
+ 3. Grant of Patent License. Subject to the terms and conditions of
70
+ this License, each Contributor hereby grants to You a perpetual,
71
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
72
+ (except as stated in this section) patent license to make, have made,
73
+ use, offer to sell, sell, import, and otherwise transfer the Work,
74
+ where such license applies only to those patent claims licensable
75
+ by such Contributor that are necessarily infringed by their
76
+ Contribution(s) alone or by combination of their Contribution(s)
77
+ with the Work to which such Contribution(s) was submitted. If You
78
+ institute patent litigation against any entity (including a
79
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
80
+ or a Contribution incorporated within the Work constitutes direct
81
+ or contributory patent infringement, then any patent licenses
82
+ granted to You under this License for that Work shall terminate
83
+ as of the date such litigation is filed.
84
+
85
+ 4. Redistribution. You may reproduce and distribute copies of the
86
+ Work or Derivative Works thereof in any medium, with or without
87
+ modifications, and in Source or Object form, provided that You
88
+ meet the following conditions:
89
+
90
+ (a) You must give any other recipients of the Work or
91
+ Derivative Works a copy of this License; and
92
+
93
+ (b) You must cause any modified files to carry prominent notices
94
+ stating that You changed the files; and
95
+
96
+ (c) You must retain, in the Source form of any Derivative Works
97
+ that You distribute, all copyright, trademark, patent,
98
+ and attribution notices from the Source form of the Work,
99
+ excluding those notices that do not pertain to any part of
100
+ the Derivative Works; and
101
+
102
+ (d) If the Work includes a "NOTICE" text file as part of its
103
+ distribution, then any Derivative Works that You distribute must
104
+ include a readable copy of the attribution notices contained
105
+ within such NOTICE file, excluding those notices that do not
106
+ pertain to any part of the Derivative Works, in at least one
107
+ of the following places: within a NOTICE text file distributed
108
+ as part of the Derivative Works; within the Source form or
109
+ documentation, if provided along with the Derivative Works; or,
110
+ within a display generated by the Derivative Works, if and
111
+ wherever such third-party notices normally appear. The contents
112
+ of the NOTICE file are for informational purposes only and
113
+ do not modify the License. You may add Your own attribution
114
+ notices within Derivative Works that You distribute, alongside
115
+ or as an addendum to the NOTICE text from the Work, provided
116
+ that such additional attribution notices cannot be construed
117
+ as modifying the License.
118
+
119
+ You may add Your own copyright notice to Your modifications and
120
+ may provide additional or different license terms and conditions
121
+ for use, reproduction, or distribution of Your modifications, or
122
+ for any such Derivative Works as a whole, provided Your use,
123
+ reproduction, and distribution of the Work otherwise complies with
124
+ the conditions stated in this License.
125
+
126
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
127
+ any Contribution intentionally submitted for inclusion in the Work
128
+ by You to the Licensor shall be under the terms and conditions of
129
+ this License, without any additional terms or conditions.
130
+ Notwithstanding the above, nothing herein shall supersede or modify
131
+ the terms of any separate license agreement you may have executed
132
+ with Licensor regarding such Contributions.
133
+
134
+ 6. Trademarks. This License does not grant permission to use the trade
135
+ names, trademarks, service marks, or product names of the Licensor,
136
+ except as required for reasonable and customary use in describing the
137
+ origin of the Work and reproducing the content of the NOTICE file.
138
+
139
+ 7. Disclaimer of Warranty. Unless required by applicable law or
140
+ agreed to in writing, Licensor provides the Work (and each
141
+ Contributor provides its Contributions) on an "AS IS" BASIS,
142
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
143
+ implied, including, without limitation, any warranties or conditions
144
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
145
+ PARTICULAR PURPOSE. You are solely responsible for determining the
146
+ appropriateness of using or redistributing the Work and assume any
147
+ risks associated with Your exercise of permissions under this License.
148
+
149
+ 8. Limitation of Liability. In no event and under no legal theory,
150
+ whether in tort (including negligence), contract, or otherwise,
151
+ unless required by applicable law (such as deliberate and grossly
152
+ negligent acts) or agreed to in writing, shall any Contributor be
153
+ liable to You for damages, including any direct, indirect, special,
154
+ incidental, or consequential damages of any character arising as a
155
+ result of this License or out of the use or inability to use the
156
+ Work (including but not limited to damages for loss of goodwill,
157
+ work stoppage, computer failure or malfunction, or any and all
158
+ other commercial damages or losses), even if such Contributor
159
+ has been advised of the possibility of such damages.
160
+
161
+ 9. Accepting Warranty or Support. When redistributing the Work or
162
+ Derivative Works thereof, You may choose to offer, and charge a fee
163
+ for, acceptance of support, warranty, indemnity, or other liability
164
+ obligations and/or rights consistent with this License. However, in
165
+ accepting such obligations, You may act only on Your own behalf and on
166
+ Your sole responsibility, not on behalf of any other Contributor, and
167
+ only if You agree to indemnify, defend, and hold each Contributor
168
+ harmless for any liability incurred by, or claims asserted against,
169
+ such Contributor by reason of your accepting any such warranty or support.
170
+
171
+ END OF TERMS AND CONDITIONS
172
+
173
+ APPENDIX: How to apply the Apache License to your work.
174
+
175
+ To apply the Apache License to your work, attach the following
176
+ boilerplate notice, with the fields enclosed by brackets "[]"
177
+ replaced with your own identifying information. (Don't include
178
+ the brackets!) The text should be enclosed by the appropriate
179
+ comment syntax for the file format. We also recommend that a
180
+ file or class name and description of purpose be included on the
181
+ same page as the copyright notice for easier identification within
182
+ third-party archives.
183
+
184
+ Copyright 2022-2024 Platformatic Inc. and contributors
185
+
186
+ Licensed under the Apache License, Version 2.0 (the "License");
187
+ you may not use this file except in compliance with the License.
188
+ You may obtain a copy of the License at
189
+
190
+ http://www.apache.org/licenses/LICENSE-2.0
191
+
192
+ Unless required by applicable law or agreed to in writing, software
193
+ distributed under the License is distributed on an "AS IS" BASIS,
194
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
195
+ See the License for the specific language governing permissions and
196
+ limitations under the License.
package/NOTICE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright 2022-2024 Platformatic Inc. and contributors
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
@@ -0,0 +1,3 @@
1
+ import neostandard from 'neostandard'
2
+
3
+ export default neostandard({})
package/index.js CHANGED
@@ -1 +1,69 @@
1
- throw new Error('This is a stub for new packages. Do not use.')
1
+ import { time } from '@datadog/pprof'
2
+ import { NoProfileAvailableError, ProfilingAlreadyStartedError, ProfilingNotStartedError } from './lib/errors.js'
3
+
4
+ const kITC = Symbol.for('plt.runtime.itc')
5
+
6
+ let isCapturing = false
7
+ let latestProfile = null
8
+ let captureInterval = null
9
+
10
+ // Keep trying until ITC is available. This is needed because preloads run
11
+ // before the app thread initialization, so globalThis.platformatic.messaging
12
+ // and ITC don't exist yet.
13
+ const registerInterval = setInterval(() => {
14
+ if (globalThis[kITC]) {
15
+ globalThis[kITC].handle('getLastProfile', getLastProfile)
16
+ globalThis[kITC].handle('startProfiling', startProfiling)
17
+ globalThis[kITC].handle('stopProfiling', stopProfiling)
18
+ clearInterval(registerInterval)
19
+ }
20
+ }, 10)
21
+
22
+ function rotateProfile () {
23
+ // `true` immediately restarts profiling after stopping
24
+ latestProfile = time.stop(true)
25
+ }
26
+
27
+ export function startProfiling (options = {}) {
28
+ if (isCapturing) {
29
+ throw new ProfilingAlreadyStartedError()
30
+ }
31
+ isCapturing = true
32
+
33
+ time.start(options)
34
+
35
+ // Set up profile window rotation if durationMillis is provided
36
+ const timeout = options.durationMillis
37
+ if (timeout) {
38
+ captureInterval = setInterval(rotateProfile, timeout)
39
+ captureInterval.unref()
40
+ }
41
+ }
42
+
43
+ export function stopProfiling () {
44
+ if (!isCapturing) {
45
+ throw new ProfilingNotStartedError()
46
+ }
47
+ isCapturing = false
48
+
49
+ clearInterval(captureInterval)
50
+ captureInterval = null
51
+
52
+ latestProfile = time.stop()
53
+ return latestProfile.encode()
54
+ }
55
+
56
+ export function getLastProfile () {
57
+ // TODO: Should it be allowed to get last profile after stopping?
58
+ if (!isCapturing) {
59
+ throw new ProfilingNotStartedError()
60
+ }
61
+
62
+ if (latestProfile == null) {
63
+ throw new NoProfileAvailableError()
64
+ }
65
+
66
+ return latestProfile.encode()
67
+ }
68
+
69
+ export * as errors from './lib/errors.js'
package/lib/errors.js ADDED
@@ -0,0 +1,21 @@
1
+ import createError from '@fastify/error'
2
+
3
+ export const ERROR_PREFIX = 'PLT_PPROF'
4
+
5
+ export const ProfilingAlreadyStartedError = createError(
6
+ `${ERROR_PREFIX}_PROFILING_ALREADY_STARTED`,
7
+ 'Profiling is already started',
8
+ 400
9
+ )
10
+
11
+ export const ProfilingNotStartedError = createError(
12
+ `${ERROR_PREFIX}_PROFILING_NOT_STARTED`,
13
+ 'Profiling not started - call startProfiling() first',
14
+ 400
15
+ )
16
+
17
+ export const NoProfileAvailableError = createError(
18
+ `${ERROR_PREFIX}_NO_PROFILE_AVAILABLE`,
19
+ 'No profile available - wait for profiling to complete or trigger manual capture',
20
+ 400
21
+ )
package/package.json CHANGED
@@ -1,10 +1,9 @@
1
1
  {
2
2
  "name": "@platformatic/wattpm-pprof-capture",
3
- "version": "0.0.1",
3
+ "version": "3.0.0-alpha.8",
4
+ "description": "pprof profiling capture for wattpm",
4
5
  "main": "index.js",
5
- "files": [
6
- "index.js"
7
- ],
6
+ "type": "module",
8
7
  "repository": {
9
8
  "type": "git",
10
9
  "url": "git+https://github.com/platformatic/platformatic.git"
@@ -14,5 +13,23 @@
14
13
  "bugs": {
15
14
  "url": "https://github.com/platformatic/platformatic/issues"
16
15
  },
17
- "homepage": "https://github.com/platformatic/platformatic#readme"
16
+ "homepage": "https://github.com/platformatic/platformatic#readme",
17
+ "dependencies": {
18
+ "@datadog/pprof": "^5.3.0",
19
+ "@fastify/error": "^4.0.0",
20
+ "undici": "^6.0.0"
21
+ },
22
+ "devDependencies": {
23
+ "eslint": "9",
24
+ "neostandard": "^0.12.0",
25
+ "@platformatic/foundation": "3.0.0-alpha.8",
26
+ "@platformatic/service": "3.0.0-alpha.8"
27
+ },
28
+ "engines": {
29
+ "node": ">=22.18.0"
30
+ },
31
+ "scripts": {
32
+ "test": "node --test --test-reporter=cleaner-spec-reporter --test-concurrency=1 --test-timeout=2000000 test/*.test.js test/**/*.test.js",
33
+ "lint": "eslint"
34
+ }
18
35
  }
@@ -0,0 +1,15 @@
1
+ {
2
+ "$schema": "https://schemas.platformatic.dev/@platformatic/runtime/2.0.0.json",
3
+ "entrypoint": "service",
4
+ "preload": "../../../index.js",
5
+ "autoload": {
6
+ "path": "./"
7
+ },
8
+ "server": {
9
+ "hostname": "127.0.0.1",
10
+ "port": 0
11
+ },
12
+ "logger": {
13
+ "level": "error"
14
+ }
15
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "name": "test-service",
3
+ "version": "1.0.0",
4
+ "type": "commonjs"
5
+ }
@@ -0,0 +1,14 @@
1
+ {
2
+ "$schema": "https://schemas.platformatic.dev/@platformatic/service/2.0.0.json",
3
+ "server": {
4
+ "logger": {
5
+ "level": "warn"
6
+ }
7
+ },
8
+ "plugins": {
9
+ "paths": [{
10
+ "path": "plugin.js",
11
+ "encapsulate": false
12
+ }]
13
+ }
14
+ }
@@ -0,0 +1,13 @@
1
+ 'use strict'
2
+
3
+ module.exports = async function (app) {
4
+ // Simple hello world service
5
+ app.get('/', async () => {
6
+ return { message: 'Hello World' }
7
+ })
8
+
9
+ // Health check endpoint
10
+ app.get('/health', async () => {
11
+ return { status: 'ok' }
12
+ })
13
+ }
@@ -0,0 +1,202 @@
1
+ import assert from 'node:assert'
2
+ import { resolve } from 'node:path'
3
+ import test from 'node:test'
4
+ import { request } from 'undici'
5
+ import { createRuntime } from '../../runtime/test/helpers.js'
6
+
7
+ async function createApp (t) {
8
+ const configFile = resolve(import.meta.dirname, 'fixtures/runtime-test/platformatic.json')
9
+ const app = await createRuntime(configFile)
10
+
11
+ t.after(async () => {
12
+ await app.close()
13
+ })
14
+
15
+ const url = await app.start()
16
+ // Wait for services and handlers to register
17
+ await new Promise(resolve => setTimeout(resolve, 200))
18
+
19
+ return { app, url }
20
+ }
21
+
22
+ test('basic service functionality should work with wattpm-pprof-capture', async t => {
23
+ const { url } = await createApp(t)
24
+
25
+ // Test that the hello world service is working (verifying preload didn't break anything)
26
+ const res = await request(`${url}/`)
27
+ const json = await res.body.json()
28
+
29
+ assert.strictEqual(res.statusCode, 200)
30
+ assert.strictEqual(json.message, 'Hello World')
31
+
32
+ // Test health endpoint
33
+ const healthRes = await request(`${url}/health`)
34
+ const healthJson = await healthRes.body.json()
35
+
36
+ assert.strictEqual(healthRes.statusCode, 200)
37
+ assert.strictEqual(healthJson.status, 'ok')
38
+ })
39
+
40
+ test('getLastProfile should throw error when profiling not started', async t => {
41
+ const { app } = await createApp(t)
42
+
43
+ await assert.rejects(
44
+ () => app.sendCommandToApplication('service', 'getLastProfile'),
45
+ { code: 'PLT_PPROF_PROFILING_NOT_STARTED' },
46
+ 'Should throw ProfilingNotStartedError when profiling not started'
47
+ )
48
+ })
49
+
50
+ test('error types should be distinguishable throughout lifecycle', async t => {
51
+ const { app } = await createApp(t)
52
+
53
+ // Test ProfilingNotStartedError before starting
54
+ await assert.rejects(
55
+ () => app.sendCommandToApplication('service', 'getLastProfile'),
56
+ { code: 'PLT_PPROF_PROFILING_NOT_STARTED' },
57
+ 'Should throw ProfilingNotStartedError (not NoProfileAvailableError)'
58
+ )
59
+
60
+ // Start profiling with profile rotation
61
+ await app.sendCommandToApplication('service', 'startProfiling', { durationMillis: 500 })
62
+
63
+ // Test NoProfileAvailableError (before any rotation happens)
64
+ await assert.rejects(
65
+ () => app.sendCommandToApplication('service', 'getLastProfile'),
66
+ { code: 'PLT_PPROF_NO_PROFILE_AVAILABLE' },
67
+ 'Should throw NoProfileAvailableError (not ProfilingNotStartedError)'
68
+ )
69
+
70
+ // Wait for the profile to be captured through rotation
71
+ await new Promise(resolve => setTimeout(resolve, 600))
72
+
73
+ // Get the profile (should succeed now)
74
+ const profile = await app.sendCommandToApplication('service', 'getLastProfile')
75
+ assert.ok(profile instanceof Uint8Array, 'Profile should be Uint8Array from ITC')
76
+ assert.ok(profile.length > 0, 'Profile should have content')
77
+
78
+ // Test stopping profiling
79
+ const stopResult = await app.sendCommandToApplication('service', 'stopProfiling')
80
+ assert.ok(stopResult instanceof Uint8Array, 'stopProfiling should return final profile')
81
+ assert.ok(stopResult.length > 0, 'Final profile should have content')
82
+
83
+ // Test ProfilingNotStartedError after stopping
84
+ await assert.rejects(
85
+ () => app.sendCommandToApplication('service', 'getLastProfile'),
86
+ { code: 'PLT_PPROF_PROFILING_NOT_STARTED' },
87
+ 'Should throw ProfilingNotStartedError after stopping'
88
+ )
89
+ })
90
+
91
+ test('profiling without rotation should only provide profile on stop', async t => {
92
+ const { app } = await createApp(t)
93
+
94
+ // Start profiling without durationMillis (no rotation)
95
+ await app.sendCommandToApplication('service', 'startProfiling', {})
96
+
97
+ // Wait some time - should still get NoProfileAvailableError since no rotation
98
+ await new Promise(resolve => setTimeout(resolve, 500))
99
+ await assert.rejects(
100
+ () => app.sendCommandToApplication('service', 'getLastProfile'),
101
+ { code: 'PLT_PPROF_NO_PROFILE_AVAILABLE' },
102
+ 'Should throw NoProfileAvailableError without rotation'
103
+ )
104
+
105
+ // Stop profiling should return the profile
106
+ const finalProfile = await app.sendCommandToApplication('service', 'stopProfiling')
107
+ assert.ok(finalProfile instanceof Uint8Array, 'Should get final profile from stopProfiling')
108
+ assert.ok(finalProfile.length > 0, 'Final profile should have content')
109
+ })
110
+
111
+ test('multiple start attempts should throw error', async t => {
112
+ const { app } = await createApp(t)
113
+
114
+ // Start profiling
115
+ await app.sendCommandToApplication('service', 'startProfiling', { durationMillis: 500 })
116
+
117
+ // Try to start again - should throw ProfilingAlreadyStartedError
118
+ await assert.rejects(
119
+ () => app.sendCommandToApplication('service', 'startProfiling', { durationMillis: 500 }),
120
+ { code: 'PLT_PPROF_PROFILING_ALREADY_STARTED' },
121
+ 'Should throw ProfilingAlreadyStartedError'
122
+ )
123
+
124
+ // Should still be able to get profile after first rotation
125
+ await new Promise(resolve => setTimeout(resolve, 600))
126
+ const profile = await app.sendCommandToApplication('service', 'getLastProfile')
127
+ assert.ok(profile instanceof Uint8Array, 'Should get Uint8Array from ITC')
128
+
129
+ await app.sendCommandToApplication('service', 'stopProfiling')
130
+ })
131
+
132
+ test('multiple stop attempts should throw error', async t => {
133
+ const { app } = await createApp(t)
134
+
135
+ // Start and stop profiling
136
+ await app.sendCommandToApplication('service', 'startProfiling', { durationMillis: 500 })
137
+ await app.sendCommandToApplication('service', 'stopProfiling')
138
+
139
+ // Try to stop again - should throw ProfilingNotStartedError
140
+ await assert.rejects(
141
+ () => app.sendCommandToApplication('service', 'stopProfiling'),
142
+ { code: 'PLT_PPROF_PROFILING_NOT_STARTED' },
143
+ 'Should throw ProfilingNotStartedError on double stop'
144
+ )
145
+ })
146
+
147
+ test('stopProfiling should throw error when called without starting', async t => {
148
+ const { app } = await createApp(t)
149
+
150
+ // Try to stop without starting
151
+ await assert.rejects(
152
+ () => app.sendCommandToApplication('service', 'stopProfiling'),
153
+ { code: 'PLT_PPROF_PROFILING_NOT_STARTED' },
154
+ 'Should throw ProfilingNotStartedError'
155
+ )
156
+ })
157
+
158
+ test('profile rotation should update available profiles', async t => {
159
+ const { app } = await createApp(t)
160
+
161
+ // Start with short rotation interval
162
+ await app.sendCommandToApplication('service', 'startProfiling', { durationMillis: 200 })
163
+
164
+ // Wait for first rotation
165
+ await new Promise(resolve => setTimeout(resolve, 250))
166
+ const profile1 = await app.sendCommandToApplication('service', 'getLastProfile')
167
+ assert.ok(profile1 instanceof Uint8Array)
168
+
169
+ // Wait for second rotation
170
+ await new Promise(resolve => setTimeout(resolve, 250))
171
+ const profile2 = await app.sendCommandToApplication('service', 'getLastProfile')
172
+ assert.ok(profile2 instanceof Uint8Array)
173
+
174
+ // Profiles should be the latest captured (may or may not be different)
175
+ // The important thing is that we can get profiles after each rotation
176
+
177
+ await app.sendCommandToApplication('service', 'stopProfiling')
178
+ })
179
+
180
+ test('getLastProfile should return same profile until next rotation', async t => {
181
+ const { app } = await createApp(t)
182
+
183
+ await app.sendCommandToApplication('service', 'startProfiling', { durationMillis: 500 })
184
+
185
+ // Wait for first rotation
186
+ await new Promise(resolve => setTimeout(resolve, 600))
187
+
188
+ // Multiple getLastProfile calls should return the same profile
189
+ const profile1 = await app.sendCommandToApplication('service', 'getLastProfile')
190
+ const profile2 = await app.sendCommandToApplication('service', 'getLastProfile')
191
+ const profile3 = await app.sendCommandToApplication('service', 'getLastProfile')
192
+
193
+ assert.ok(profile1 instanceof Uint8Array)
194
+ assert.ok(profile2 instanceof Uint8Array)
195
+ assert.ok(profile3 instanceof Uint8Array)
196
+
197
+ // All should be the same profile (latest captured)
198
+ assert.deepStrictEqual(profile1, profile2, 'Should return same profile')
199
+ assert.deepStrictEqual(profile2, profile3, 'Should return same profile')
200
+
201
+ await app.sendCommandToApplication('service', 'stopProfiling')
202
+ })