|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +import assert from 'assert' |
| 6 | +import { WorkspaceFolder } from 'vscode' |
| 7 | +import { ExportResultArchiveCommandInput } from '@amzn/codewhisperer-streaming' |
| 8 | +import * as sinon from 'sinon' |
| 9 | +import path from 'path' |
| 10 | +import { fs, getRandomString } from '../../shared' |
| 11 | +import { createTestWorkspace } from '../../test/testUtil' |
| 12 | +import { performanceTest } from '../../shared/performance/performance' |
| 13 | +import { downloadExportResultArchive } from '../../shared/utilities/download' |
| 14 | + |
| 15 | +interface SetupResult { |
| 16 | + workspace: WorkspaceFolder |
| 17 | + exportCommandInput: ExportResultArchiveCommandInput |
| 18 | + writeFileStub: sinon.SinonStub |
| 19 | + cwStreaming: any |
| 20 | +} |
| 21 | + |
| 22 | +interface FakeCommandOutput { |
| 23 | + body: { binaryPayloadEvent: { bytes: Buffer } }[] |
| 24 | +} |
| 25 | + |
| 26 | +function generateCommandOutput(numPieces: number, pieceSize: number): FakeCommandOutput { |
| 27 | + const body = Array.from({ length: numPieces }, (_, i) => { |
| 28 | + return { |
| 29 | + binaryPayloadEvent: { |
| 30 | + bytes: Buffer.from(getRandomString(pieceSize)), |
| 31 | + }, |
| 32 | + } |
| 33 | + }) |
| 34 | + return { body } |
| 35 | +} |
| 36 | + |
| 37 | +async function setup(pieces: number, pieceSize: number): Promise<SetupResult> { |
| 38 | + // Force VSCode to find test workspace only to keep test contained and controlled. |
| 39 | + const workspace = await createTestWorkspace(1, {}) |
| 40 | + const exportCommandInput = {} as ExportResultArchiveCommandInput |
| 41 | + // Manutally stub the CodeWhispererStreaming to avoid constructor call. |
| 42 | + const cwStreaming = { exportResultArchive: () => generateCommandOutput(pieces, pieceSize) } |
| 43 | + |
| 44 | + const writeFileStub = sinon.stub(fs, 'writeFile') |
| 45 | + return { workspace, exportCommandInput, writeFileStub, cwStreaming } |
| 46 | +} |
| 47 | + |
| 48 | +function perfTest(pieces: number, pieceSize: number, label: string) { |
| 49 | + return performanceTest( |
| 50 | + { |
| 51 | + testRuns: 10, |
| 52 | + linux: { |
| 53 | + userCpuUsage: 200, |
| 54 | + systemCpuUsage: 35, |
| 55 | + heapTotal: 4, |
| 56 | + }, |
| 57 | + darwin: { |
| 58 | + userCpuUsage: 200, |
| 59 | + systemCpuUsage: 35, |
| 60 | + heapTotal: 4, |
| 61 | + }, |
| 62 | + win32: { |
| 63 | + userCpuUsage: 200, |
| 64 | + systemCpuUsage: 35, |
| 65 | + heapTotal: 4, |
| 66 | + }, |
| 67 | + }, |
| 68 | + label, |
| 69 | + function () { |
| 70 | + return { |
| 71 | + setup: async () => await setup(pieces, pieceSize), |
| 72 | + execute: async ({ workspace, cwStreaming, exportCommandInput, writeFileStub }: SetupResult) => { |
| 73 | + await downloadExportResultArchive( |
| 74 | + cwStreaming, |
| 75 | + exportCommandInput, |
| 76 | + path.join(workspace.uri.fsPath, 'result') |
| 77 | + ) |
| 78 | + }, |
| 79 | + verify: async (setup: SetupResult) => { |
| 80 | + assert.ok(setup.writeFileStub.calledOnce) |
| 81 | + assert.ok((setup.writeFileStub.firstCall.args[1] as Buffer).length === pieces * pieceSize) |
| 82 | + }, |
| 83 | + } |
| 84 | + } |
| 85 | + ) |
| 86 | +} |
| 87 | + |
| 88 | +describe('downloadExportResultArchive', function () { |
| 89 | + describe('performanceTests', function () { |
| 90 | + afterEach(function () { |
| 91 | + sinon.restore() |
| 92 | + }) |
| 93 | + perfTest(1, 1000, '1x1KB') |
| 94 | + perfTest(10, 100, '10x100B') |
| 95 | + perfTest(100, 10, '100x10B') |
| 96 | + perfTest(1000, 1, '1000x1B') |
| 97 | + }) |
| 98 | +}) |
0 commit comments