Skip to content
This repository was archived by the owner on Dec 21, 2021. It is now read-only.

Commit 750d62e

Browse files
committed
perf(pipeline): Improve memory usage stability.
1 parent 8a857c0 commit 750d62e

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

src/utils/iterators.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,7 @@ export function CancelableGenerator(iterable, onFinally = () => {}, { timeout =
219219

220220
cancelSignal.once('cancel', onCancel)
221221
return Promise.race([
222-
iterator.next(...args).finally(() => {
223-
cancelSignal.off('cancel', onCancel)
224-
}),
222+
iterator.next(...args),
225223
p,
226224
])
227225
},

test/flakey/LongResend.test.ts

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function logMemory() {
1515
heapTotal: prettyBytes(res.heapTotal),
1616
heapUsed: prettyBytes(res.heapUsed),
1717
external: prettyBytes(res.external),
18-
arrayBuffers: res.arrayBuffers
18+
arrayBuffers: prettyBytes(res.arrayBuffers),
1919
}
2020
}
2121

@@ -75,7 +75,7 @@ describe('LongResend', () => {
7575
100,
7676
1000,
7777
10000,
78-
25000,
78+
25000, // will be ignored, max is 10,000
7979
]
8080

8181
const MAX_RESEND_SIZE = 10000
@@ -99,39 +99,60 @@ describe('LongResend', () => {
9999
} else {
100100
expect(count).toBe(10000)
101101
}
102-
// @ts-expect-error
103102
}, Math.max(10000, size))
104103
})
105104

106-
test('can get big resend', async () => {
105+
test.only('can get big resend', async () => {
107106
let count = 0
108-
const today = 1616527054932
109-
const yesterday = 1616440654932
107+
const MAX_MESSAGES = 60000 // 60k
108+
const end = 1616509054932
109+
const start = end - (1 * 60 * 60 * 1000) // 1 hour
110+
const rssValues: number[] = []
111+
let total = 0
110112
const sub = await client.resend({
111113
stream: stream.id,
112114
resend: {
113115
from: {
114-
timestamp: yesterday,
116+
timestamp: start,
115117
},
116118
to: {
117-
timestamp: today,
119+
timestamp: end,
118120
}
119121
},
120-
}, (msg) => {
122+
}, (msg, streamMessage) => {
123+
total += Buffer.byteLength(streamMessage.serializedContent, 'utf8')
121124
if (count % 1000 === 0) {
122-
console.log({
125+
const { rss } = process.memoryUsage()
126+
rssValues.push(rss)
127+
console.info({
123128
msg,
124129
count,
125-
memory: logMemory()
130+
memory: logMemory(),
131+
total: prettyBytes(total)
126132
})
127133
}
128-
count += 1
134+
135+
if (count === MAX_MESSAGES) {
136+
sub.unsubscribe()
137+
} else {
138+
count += 1
139+
}
129140
})
130141
await sub.onDone()
131-
console.log('done', {
142+
const max = rssValues.reduce((a, b) => Math.max(a, b), 0)
143+
const min = rssValues.reduce((a, b) => Math.min(a, b), Infinity)
144+
const mean = rssValues.reduce((a, b) => a + b, 0) / rssValues.length
145+
const median = rssValues[Math.floor(rssValues.length / 2)]
146+
const variance = Math.sqrt(rssValues.reduce((a, b) => a + ((b - mean) ** 2), 0) / rssValues.length)
147+
console.info('done', {
148+
max: prettyBytes(max),
149+
min: prettyBytes(min),
150+
mean: prettyBytes(mean),
151+
median: prettyBytes(median),
152+
variance: prettyBytes(variance),
132153
count,
133-
memory: logMemory()
154+
memory: logMemory(),
155+
total: prettyBytes(total)
134156
})
135-
// @ts-expect-error
136157
}, 1000000)
137158
})

0 commit comments

Comments
 (0)