-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathindex.js
More file actions
186 lines (165 loc) · 4.59 KB
/
index.js
File metadata and controls
186 lines (165 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import fs from 'node:fs'
import path from 'node:path'
import v8 from 'node:v8'
import server from 'server'
const { get } = server.router
const { json, status } = server.reply
import {
getPackageStats,
getPackageExportSizes,
getAllPackageExports,
} from './build/index.js'
const PORT = 3000
const activeRequests = new Map()
let nextRequestId = 0
const formatMiB = bytes => Number((bytes / 1024 / 1024).toFixed(1))
const getMemorySnapshot = () => {
const usage = process.memoryUsage()
const heap = v8.getHeapStatistics()
return {
rssMiB: formatMiB(usage.rss),
heapUsedMiB: formatMiB(usage.heapUsed),
heapTotalMiB: formatMiB(usage.heapTotal),
externalMiB: formatMiB(usage.external),
arrayBuffersMiB: formatMiB(usage.arrayBuffers),
mallocedMiB: formatMiB(heap.malloced_memory),
peakMallocedMiB: formatMiB(heap.peak_malloced_memory),
nativeContextCount: heap.number_of_native_contexts,
detachedContextCount: heap.number_of_detached_contexts,
activeRequests: activeRequests.size,
}
}
const logMemory = (label, extra = {}) => {
console.log(
'[MEMORY]',
JSON.stringify({
ts: new Date().toISOString(),
pid: process.pid,
label,
...getMemorySnapshot(),
...extra,
}),
)
}
const getOldestRequest = () =>
[...activeRequests.values()].sort((a, b) => a.startedAt - b.startedAt)[0]
const withMemoryLogging = (label, handler) => async ctx => {
const requestId = ++nextRequestId
const startedAt = Date.now()
const before = getMemorySnapshot()
activeRequests.set(requestId, {
label,
path: ctx.url,
startedAt,
query: ctx.query,
})
try {
return await handler(ctx)
} finally {
activeRequests.delete(requestId)
const after = getMemorySnapshot()
const durationMs = Date.now() - startedAt
const rssDeltaMiB = Number((after.rssMiB - before.rssMiB).toFixed(1))
const heapDeltaMiB = Number(
(after.heapUsedMiB - before.heapUsedMiB).toFixed(1),
)
if (durationMs >= 1500 || Math.abs(rssDeltaMiB) >= 40) {
logMemory('request-finished', {
requestId,
route: label,
path: ctx.url,
package: ctx.query?.p ?? null,
durationMs,
rssDeltaMiB,
heapDeltaMiB,
})
}
}
}
const heapSnapshotDir = path.join(process.cwd(), 'reports', 'heap-debug')
fs.mkdirSync(heapSnapshotDir, { recursive: true })
setInterval(() => {
const oldestRequest = getOldestRequest()
logMemory('interval', {
oldestRequestAgeMs: oldestRequest
? Date.now() - oldestRequest.startedAt
: 0,
oldestRequestPath: oldestRequest?.path ?? null,
oldestRequestLabel: oldestRequest?.label ?? null,
})
}, 30000).unref()
console.log(`Starting at port ${PORT}`)
logMemory('startup')
server({ port: PORT }, [
get(
'/size',
withMemoryLogging('/size', async ctx => {
const packageString = decodeURIComponent(ctx.query.p)
try {
const result = await getPackageStats(packageString, {
...ctx.query,
})
return json(result)
} catch (err) {
console.log(err)
return status(500).send({
statusCode: 500,
body: JSON.stringify(err),
})
}
}),
),
get(
'/export-sizes',
withMemoryLogging('/export-sizes', async ctx => {
const packageString = decodeURIComponent(ctx.query.p)
try {
const result = await getPackageExportSizes(packageString, {
debug: !!ctx.query.debug,
minifier: ctx.query.minifier,
})
return json(result)
} catch (err) {
console.log(err)
return status(500).send({
statusCode: 500,
body: JSON.stringify(err),
})
}
}),
),
get(
'/exports',
withMemoryLogging('/exports', async ctx => {
const packageString = decodeURIComponent(ctx.query.p)
try {
const result = await getAllPackageExports(packageString, {
debug: !!ctx.query.debug,
})
return json(result)
} catch (err) {
console.log(err)
return status(500).send({
statusCode: 500,
body: JSON.stringify(err),
})
}
}),
),
get('/__debug/memory', async () => json(getMemorySnapshot())),
get('/__debug/heapdump', async () => {
const filePath = path.join(
heapSnapshotDir,
`heap-${Date.now()}-${process.pid}.heapsnapshot`,
)
if (typeof global.gc === 'function') {
global.gc()
}
const snapshotPath = v8.writeHeapSnapshot(filePath)
logMemory('heapdump', { snapshotPath })
return json({
snapshotPath,
...getMemorySnapshot(),
})
}),
])