|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, it, beforeAll, afterAll, afterEach } from 'vitest'; |
| 8 | +import { TestRig, MemoryTestHarness } from '@google/gemini-cli-test-utils'; |
| 9 | +import { join, dirname } from 'node:path'; |
| 10 | +import { fileURLToPath } from 'node:url'; |
| 11 | + |
| 12 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 13 | +const BASELINES_PATH = join(__dirname, 'baselines.json'); |
| 14 | +const UPDATE_BASELINES = process.env['UPDATE_MEMORY_BASELINES'] === 'true'; |
| 15 | +const TOLERANCE_PERCENT = 10; |
| 16 | + |
| 17 | +// Fake API key for tests using fake responses |
| 18 | +const TEST_ENV = { GEMINI_API_KEY: 'fake-memory-test-key' }; |
| 19 | + |
| 20 | +describe('Memory Usage Tests', () => { |
| 21 | + let harness: MemoryTestHarness; |
| 22 | + let rig: TestRig; |
| 23 | + |
| 24 | + beforeAll(() => { |
| 25 | + harness = new MemoryTestHarness({ |
| 26 | + baselinesPath: BASELINES_PATH, |
| 27 | + defaultTolerancePercent: TOLERANCE_PERCENT, |
| 28 | + gcCycles: 3, |
| 29 | + gcDelayMs: 100, |
| 30 | + sampleCount: 3, |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + afterEach(async () => { |
| 35 | + await rig.cleanup(); |
| 36 | + }); |
| 37 | + |
| 38 | + afterAll(async () => { |
| 39 | + // Generate the summary report after all tests |
| 40 | + await harness.generateReport(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('idle-session-startup: memory usage within baseline', async () => { |
| 44 | + rig = new TestRig(); |
| 45 | + rig.setup('memory-idle-startup', { |
| 46 | + fakeResponsesPath: join(__dirname, 'memory.idle-startup.responses'), |
| 47 | + }); |
| 48 | + |
| 49 | + const result = await harness.runScenario( |
| 50 | + 'idle-session-startup', |
| 51 | + async (recordSnapshot) => { |
| 52 | + await rig.run({ |
| 53 | + args: ['hello'], |
| 54 | + timeout: 120000, |
| 55 | + env: TEST_ENV, |
| 56 | + }); |
| 57 | + |
| 58 | + await recordSnapshot('after-startup'); |
| 59 | + }, |
| 60 | + ); |
| 61 | + |
| 62 | + if (UPDATE_BASELINES) { |
| 63 | + harness.updateScenarioBaseline(result); |
| 64 | + console.log( |
| 65 | + `Updated baseline for idle-session-startup: ${(result.finalHeapUsed / (1024 * 1024)).toFixed(1)} MB`, |
| 66 | + ); |
| 67 | + } else { |
| 68 | + harness.assertWithinBaseline(result); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + it('simple-prompt-response: memory usage within baseline', async () => { |
| 73 | + rig = new TestRig(); |
| 74 | + rig.setup('memory-simple-prompt', { |
| 75 | + fakeResponsesPath: join(__dirname, 'memory.simple-prompt.responses'), |
| 76 | + }); |
| 77 | + |
| 78 | + const result = await harness.runScenario( |
| 79 | + 'simple-prompt-response', |
| 80 | + async (recordSnapshot) => { |
| 81 | + await rig.run({ |
| 82 | + args: ['What is the capital of France?'], |
| 83 | + timeout: 120000, |
| 84 | + env: TEST_ENV, |
| 85 | + }); |
| 86 | + |
| 87 | + await recordSnapshot('after-response'); |
| 88 | + }, |
| 89 | + ); |
| 90 | + |
| 91 | + if (UPDATE_BASELINES) { |
| 92 | + harness.updateScenarioBaseline(result); |
| 93 | + console.log( |
| 94 | + `Updated baseline for simple-prompt-response: ${(result.finalHeapUsed / (1024 * 1024)).toFixed(1)} MB`, |
| 95 | + ); |
| 96 | + } else { |
| 97 | + harness.assertWithinBaseline(result); |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + it('multi-turn-conversation: memory remains stable over turns', async () => { |
| 102 | + rig = new TestRig(); |
| 103 | + rig.setup('memory-multi-turn', { |
| 104 | + fakeResponsesPath: join(__dirname, 'memory.multi-turn.responses'), |
| 105 | + }); |
| 106 | + |
| 107 | + const prompts = [ |
| 108 | + 'Hello, what can you help me with?', |
| 109 | + 'Tell me about JavaScript', |
| 110 | + 'How is TypeScript different?', |
| 111 | + 'Can you write a simple TypeScript function?', |
| 112 | + 'What are some TypeScript best practices?', |
| 113 | + ]; |
| 114 | + |
| 115 | + const result = await harness.runScenario( |
| 116 | + 'multi-turn-conversation', |
| 117 | + async (recordSnapshot) => { |
| 118 | + // Run through all turns as a piped sequence |
| 119 | + const stdinContent = prompts.join('\n'); |
| 120 | + await rig.run({ |
| 121 | + stdin: stdinContent, |
| 122 | + timeout: 120000, |
| 123 | + env: TEST_ENV, |
| 124 | + }); |
| 125 | + |
| 126 | + // Take snapshots after the conversation completes |
| 127 | + await recordSnapshot('after-all-turns'); |
| 128 | + }, |
| 129 | + ); |
| 130 | + |
| 131 | + if (UPDATE_BASELINES) { |
| 132 | + harness.updateScenarioBaseline(result); |
| 133 | + console.log( |
| 134 | + `Updated baseline for multi-turn-conversation: ${(result.finalHeapUsed / (1024 * 1024)).toFixed(1)} MB`, |
| 135 | + ); |
| 136 | + } else { |
| 137 | + harness.assertWithinBaseline(result); |
| 138 | + } |
| 139 | + }); |
| 140 | + |
| 141 | + it('multi-function-call-repo-search: memory after tool use', async () => { |
| 142 | + rig = new TestRig(); |
| 143 | + rig.setup('memory-multi-func-call', { |
| 144 | + fakeResponsesPath: join( |
| 145 | + __dirname, |
| 146 | + 'memory.multi-function-call.responses', |
| 147 | + ), |
| 148 | + }); |
| 149 | + |
| 150 | + // Create directories first, then files in the workspace so the tools have targets |
| 151 | + rig.mkdir('packages/core/src/telemetry'); |
| 152 | + rig.createFile( |
| 153 | + 'packages/core/src/telemetry/memory-monitor.ts', |
| 154 | + 'export class MemoryMonitor { constructor() {} }', |
| 155 | + ); |
| 156 | + rig.createFile( |
| 157 | + 'packages/core/src/telemetry/metrics.ts', |
| 158 | + 'export function recordMemoryUsage() {}', |
| 159 | + ); |
| 160 | + |
| 161 | + const result = await harness.runScenario( |
| 162 | + 'multi-function-call-repo-search', |
| 163 | + async (recordSnapshot) => { |
| 164 | + await rig.run({ |
| 165 | + args: [ |
| 166 | + 'Search this repository for MemoryMonitor and tell me what it does', |
| 167 | + ], |
| 168 | + timeout: 120000, |
| 169 | + env: TEST_ENV, |
| 170 | + }); |
| 171 | + |
| 172 | + await recordSnapshot('after-tool-calls'); |
| 173 | + }, |
| 174 | + ); |
| 175 | + |
| 176 | + if (UPDATE_BASELINES) { |
| 177 | + harness.updateScenarioBaseline(result); |
| 178 | + console.log( |
| 179 | + `Updated baseline for multi-function-call-repo-search: ${(result.finalHeapUsed / (1024 * 1024)).toFixed(1)} MB`, |
| 180 | + ); |
| 181 | + } else { |
| 182 | + harness.assertWithinBaseline(result); |
| 183 | + } |
| 184 | + }); |
| 185 | +}); |
0 commit comments