mirror of
https://github.com/stablyai/orca.git
synced 2026-09-22 00:02:31 +00:00
perf(browser): reuse decoded single-chunk upload buffers (#18960)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
|
||||
import type { BrowserClientHostCommandEvent } from '../../shared/browser-client-host-protocol'
|
||||
import {
|
||||
@@ -102,3 +102,38 @@ describe('readBrowserClientUploadPaths', () => {
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
it.each([0, 1, 128 * 1024])(
|
||||
'avoids recopying 16 single-chunk uploads of %i bytes',
|
||||
async (size) => {
|
||||
const source = Buffer.alloc(size, 171)
|
||||
const response = {
|
||||
contentBase64: source.toString('base64'),
|
||||
bytesRead: size,
|
||||
totalBytes: size,
|
||||
eof: true
|
||||
}
|
||||
const remotePaths = Array.from({ length: 16 }, (_, i) => `file-${i}.bin`)
|
||||
const request = vi.fn(async () => response)
|
||||
const concat = vi.spyOn(Buffer, 'concat')
|
||||
let copies = 0
|
||||
let files: Awaited<ReturnType<typeof fetchBrowserClientUploadFiles>>
|
||||
try {
|
||||
files = await fetchBrowserClientUploadFiles({ request, event, remotePaths })
|
||||
copies = concat.mock.calls.length
|
||||
} finally {
|
||||
concat.mockRestore()
|
||||
}
|
||||
expect(copies).toBe(0)
|
||||
expect(request).toHaveBeenCalledTimes(16)
|
||||
expect(files.map((file) => file.remotePath)).toEqual(remotePaths)
|
||||
for (const file of files) {
|
||||
expect(file.contents).toEqual(source)
|
||||
}
|
||||
if (size > 0) {
|
||||
files[0].contents[0] = 0
|
||||
expect(files[1].contents[0]).toBe(171)
|
||||
expect(source[0]).toBe(171)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
@@ -74,7 +74,7 @@ export async function fetchBrowserClientUploadFiles(options: {
|
||||
throw new Error('browser_client_upload_transfer_stalled')
|
||||
}
|
||||
}
|
||||
files.push({ remotePath, contents: Buffer.concat(chunks) })
|
||||
files.push({ remotePath, contents: chunks.length === 1 ? chunks[0] : Buffer.concat(chunks) })
|
||||
}
|
||||
return files
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user