mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-26 21:14:35 -07:00
refactor(ui): Optimize rendering performance (#8239)
This commit is contained in:
@@ -113,6 +113,88 @@ describe('SessionStatsContext', () => {
|
||||
expect(stats?.lastPromptTokenCount).toBe(100);
|
||||
});
|
||||
|
||||
it('should not update metrics if the data is the same', () => {
|
||||
const contextRef: MutableRefObject<
|
||||
ReturnType<typeof useSessionStats> | undefined
|
||||
> = { current: undefined };
|
||||
|
||||
let renderCount = 0;
|
||||
const CountingTestHarness = () => {
|
||||
contextRef.current = useSessionStats();
|
||||
renderCount++;
|
||||
return null;
|
||||
};
|
||||
|
||||
render(
|
||||
<SessionStatsProvider>
|
||||
<CountingTestHarness />
|
||||
</SessionStatsProvider>,
|
||||
);
|
||||
|
||||
expect(renderCount).toBe(1);
|
||||
|
||||
const metrics: SessionMetrics = {
|
||||
models: {
|
||||
'gemini-pro': {
|
||||
api: { totalRequests: 1, totalErrors: 0, totalLatencyMs: 100 },
|
||||
tokens: {
|
||||
prompt: 10,
|
||||
candidates: 20,
|
||||
total: 30,
|
||||
cached: 0,
|
||||
thoughts: 0,
|
||||
tool: 0,
|
||||
},
|
||||
},
|
||||
},
|
||||
tools: {
|
||||
totalCalls: 0,
|
||||
totalSuccess: 0,
|
||||
totalFail: 0,
|
||||
totalDurationMs: 0,
|
||||
totalDecisions: { accept: 0, reject: 0, modify: 0 },
|
||||
byName: {},
|
||||
},
|
||||
};
|
||||
|
||||
act(() => {
|
||||
uiTelemetryService.emit('update', { metrics, lastPromptTokenCount: 10 });
|
||||
});
|
||||
|
||||
expect(renderCount).toBe(2);
|
||||
|
||||
act(() => {
|
||||
uiTelemetryService.emit('update', { metrics, lastPromptTokenCount: 10 });
|
||||
});
|
||||
|
||||
expect(renderCount).toBe(2);
|
||||
|
||||
const newMetrics = {
|
||||
...metrics,
|
||||
models: {
|
||||
'gemini-pro': {
|
||||
api: { totalRequests: 2, totalErrors: 0, totalLatencyMs: 200 },
|
||||
tokens: {
|
||||
prompt: 20,
|
||||
candidates: 40,
|
||||
total: 60,
|
||||
cached: 0,
|
||||
thoughts: 0,
|
||||
tool: 0,
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
act(() => {
|
||||
uiTelemetryService.emit('update', {
|
||||
metrics: newMetrics,
|
||||
lastPromptTokenCount: 20,
|
||||
});
|
||||
});
|
||||
|
||||
expect(renderCount).toBe(3);
|
||||
});
|
||||
|
||||
it('should throw an error when useSessionStats is used outside of a provider', () => {
|
||||
// Suppress console.error for this test since we expect an error
|
||||
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
||||
|
||||
Reference in New Issue
Block a user