Skip to content

Comments

feat(perf): add TTFB and output rate tracking (#421)#427

Merged
ding113 merged 1 commit intodevfrom
feat/issue-421-perf-data
Dec 24, 2025
Merged

feat(perf): add TTFB and output rate tracking (#421)#427
ding113 merged 1 commit intodevfrom
feat/issue-421-perf-data

Conversation

@ding113
Copy link
Owner

@ding113 ding113 commented Dec 24, 2025

Summary

  • Add ttfb_ms field to message_request table for Time To First Byte tracking
  • Record TTFB at first chunk arrival (streaming) or use durationMs fallback (non-streaming)
  • Add performance section to ErrorDetailsDialog showing TTFB, duration, and output rate (tok/s)
  • Update provider leaderboard: replace avgResponseTime with avgTtfbMs and avgTokensPerSecond
  • Add i18n support for all 5 locales

Changes

Category Files
Schema src/drizzle/schema.ts, drizzle/0040_bored_venus.sql
Types src/types/message.ts
Repository src/repository/message.ts, src/repository/leaderboard.ts, src/repository/usage-logs.ts
Proxy src/app/v1/_lib/proxy/session.ts, src/app/v1/_lib/proxy/response-handler.ts
UI leaderboard-view.tsx, error-details-dialog.tsx, usage-logs-table.tsx, virtualized-logs-table.tsx
i18n messages/{en,zh-CN,zh-TW,ja,ru}/dashboard.json

Test plan

  • bun run typecheck passes
  • bun run lint passes
  • bun run test -- tests/unit passes (30/30)
  • Run bun run db:migrate on target environment
  • Verify TTFB recording on streaming requests
  • Verify performance section in request details dialog
  • Verify provider leaderboard new columns

Closes #421

🤖 Generated with Claude Code

- Add ttfb_ms field to message_request table for Time To First Byte tracking
- Record TTFB at first chunk arrival (streaming) or use durationMs fallback (non-streaming)
- Add performance section to ErrorDetailsDialog showing TTFB, duration, and output rate
- Update provider leaderboard: replace avgResponseTime with avgTtfbMs and avgTokensPerSecond
- Add i18n support for all 5 locales (en, zh-CN, zh-TW, ja, ru)

Implementation details:
- ProxySession.recordTtfb() method with idempotent design
- Gemini passthrough records TTFB at response received time
- Output rate calculated as outputTokens / ((durationMs - ttfbMs) / 1000)
- Display "-" for null/zero values instead of misleading "0"

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @ding113, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the application's performance monitoring capabilities by introducing Time To First Byte (TTFB) and output rate tracking. It involves a full-stack implementation, from updating the database schema to store these new metrics, to integrating the logic for their calculation and persistence in the backend, and finally, presenting them in a user-friendly manner within the dashboard's error details and provider leaderboard. These changes provide deeper insights into API request performance, enabling better analysis and optimization.

Highlights

  • Database Schema Update: A new ttfb_ms (Time To First Byte in milliseconds) column has been added to the message_request table to store performance data.
  • TTFB Tracking Logic: Implemented logic to record TTFB, capturing it upon the arrival of the first data chunk for streaming responses, and falling back to the total durationMs for non-streaming requests.
  • Enhanced UI Performance Metrics: The ErrorDetailsDialog now includes a dedicated "Performance" section, displaying TTFB, total duration, and calculated output rate (tokens per second).
  • Updated Provider Leaderboard: The provider leaderboard has been revamped to replace avgResponseTime with avgTtfbMs and avgTokensPerSecond, offering more granular performance insights.
  • Internationalization Support: New UI texts for performance metrics have been added across five locales (English, Simplified Chinese, Traditional Chinese, Japanese, and Russian).
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions
Copy link
Contributor

🧪 测试结果

测试类型 状态
代码质量
单元测试
集成测试
API 测试

总体结果: ✅ 所有测试通过

@github-actions github-actions bot added the size/XL Extra Large PR (> 1000 lines) label Dec 24, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request is a great enhancement, adding valuable performance tracking metrics like Time To First Byte (TTFB) and output rate. The changes are comprehensive, spanning the database schema, backend logic for recording metrics, and UI updates for displaying them. The implementation for handling both streaming and non-streaming requests is solid, and the new additions to the provider leaderboard and request details dialog are very insightful. Overall, this is a high-quality contribution that significantly improves performance monitoring capabilities.

Comment on lines +116 to +123
outputTokens === null ||
outputTokens === undefined ||
outputTokens <= 0 ||
durationMs === null ||
durationMs === undefined ||
ttfbMs === null ||
ttfbMs === undefined ||
ttfbMs >= durationMs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The null and undefined checks can be made more concise. Using == null is a common practice in TypeScript to check for both null and undefined at the same time, which improves readability.

        outputTokens == null ||
        outputTokens <= 0 ||
        durationMs == null ||
        ttfbMs == null ||
        ttfbMs >= durationMs

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review Summary

This PR adds comprehensive TTFB (Time To First Byte) and output rate tracking to the performance monitoring capabilities. The implementation is clean, consistent, and follows existing patterns in the codebase.

PR Size: XL

  • Lines changed: 2,157 (2,145 additions, 12 deletions)
  • Files changed: 19

Note: The large line count is primarily due to the auto-generated Drizzle snapshot file (0040_snapshot.json ~1,951 lines). The actual code changes are approximately 200 lines across 12 source files.

Issues Found

Category Critical High Medium Low
Logic/Bugs 0 0 0 0
Security 0 0 0 0
Error Handling 0 0 0 0
Types 0 0 0 0
Comments/Docs 0 0 0 0
Tests 0 0 0 0
Simplification 0 0 0 0

Analysis Notes

Perspective 1: Comment Analyzer

  • Comments are accurate and helpful (e.g., // ⭐ 同步记录 TTFB,与首字节超时口径一致)
  • JSDoc on recordTtfb() method clearly explains the intended behavior

Perspective 2: Test Analyzer

  • No new tests added for the TTFB feature. However, the PR description indicates manual test plan items are present and unit tests pass (30/30)
  • The feature is primarily additive (new optional field) and integrates into existing well-tested flows

Perspective 3: Silent Failure Hunter

  • TTFB recording uses appropriate fallback: session.ttfbMs ?? duration ensures non-streaming requests get a reasonable value
  • The recordTtfb() method is idempotent (returns existing value if already recorded)
  • All error paths in response-handler.ts properly include ttfbMs in persistence

Perspective 4: Type Design Auditor

  • ttfbMs is correctly typed as number | null throughout the codebase
  • Type consistency maintained across schema, repository, and UI layers
  • No unsafe any usage introduced

Perspective 5: General Code Reviewer

  • Logic: The TTFB calculation Math.max(0, Date.now() - this.startTime) correctly handles edge cases
  • Security: No concerns - no user input handling changes
  • Performance: The avgTokensPerSecond SQL query uses appropriate CASE WHEN guards to avoid division by zero
  • Standards: Follows existing patterns (field naming, i18n structure, repository patterns)

Perspective 6: Code Simplifier

  • The outputTokensPerSecond calculation in error-details-dialog.tsx is appropriately defensive with clear null/edge case handling
  • The complex conditional (ttfbMs >= durationMs) guard prevents invalid rate calculations

Validated Design Decisions

  1. TTFB Fallback for Non-Streaming: Using durationMs as fallback for non-streaming requests is correct since there's no "first byte" distinction
  2. Gemini Passthrough: TTFB is recorded when clearResponseTimeout is called for Gemini passthrough, maintaining consistency
  3. SQL Aggregation: The avgTokensPerSecond query correctly excludes invalid cases (zero output tokens, null ttfb, ttfb >= duration)

Review Coverage

  • Logic and correctness - Clean
  • Security (OWASP Top 10) - Clean
  • Error handling - Clean
  • Type safety - Clean
  • Documentation accuracy - Clean
  • Test coverage - Manual test plan present
  • Code clarity - Good

Automated review by Claude AI

if (sessionWithCleanup.clearResponseTimeout) {
sessionWithCleanup.clearResponseTimeout();
// ⭐ 同步记录 TTFB,与首字节超时口径一致
session.recordTtfb();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] [LOGIC-BUG] Gemini passthrough records ttfbMs before the first body chunk

Why this is a problem: ProxySession.recordTtfb() is documented as "Definition: first body chunk received." (src/app/v1/_lib/proxy/session.ts:249), but Gemini passthrough calls it as soon as the Response exists (src/app/v1/_lib/proxy/response-handler.ts:578). This makes ttfbMs inconsistent across streaming paths and skews avgTokensPerSecond (it uses durationMs - ttfbMs).

Suggested fix:

// Keep clearing the first-byte timeout here…
sessionWithCleanup.clearResponseTimeout();

// …but don't record TTFB until the first body chunk (already done below in the reader loop).
// session.recordTtfb(); // remove

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review Summary

This PR is XL and mixes database/schema changes, proxy instrumentation, analytics queries, UI updates, and i18n, which makes review and rollback harder. One high-priority correctness issue was found that can skew the new performance metrics.

PR Size: XL

  • Lines changed: 2157
  • Files changed: 19
  • Split suggestion: separate into (1) DB migration/schema/types, (2) proxy TTFB instrumentation, (3) dashboard/leaderboard UI + i18n.

Issues Found

Category Critical High Medium Low
Logic/Bugs 0 1 0 0
Security 0 0 0 0
Error Handling 0 0 0 0
Types 0 0 0 0
Comments/Docs 0 0 0 0
Tests 0 0 0 0
Simplification 0 0 0 0

Critical Issues (Must Fix)

None.

High Priority Issues (Should Fix)

  • [HIGH] [LOGIC-BUG] Gemini passthrough records ttfbMs before the first body chunk (src/app/v1/_lib/proxy/response-handler.ts:578).

Review Coverage

  • Logic and correctness
  • Security (OWASP Top 10)
  • Error handling
  • Type safety
  • Documentation accuracy
  • Test coverage
  • Code clarity

Automated review by Codex AI

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Reviewed PR #427 (feat(perf): add TTFB and output rate tracking (#421)), applied label size/XL.
  • Left 1 inline comment: src/app/v1/_lib/proxy/response-handler.ts:578 — Gemini passthrough records ttfbMs before first body chunk, which can skew the new perf metrics; included a concrete code change suggestion.
  • Submitted the full review summary via gh pr review.

@ding113 ding113 merged commit 4005c94 into dev Dec 24, 2025
16 checks passed
@github-project-automation github-project-automation bot moved this from Backlog to Done in Claude Code Hub Roadmap Dec 24, 2025
@ding113 ding113 deleted the feat/issue-421-perf-data branch December 30, 2025 17:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:i18n area:statistics area:UI enhancement New feature or request size/XL Extra Large PR (> 1000 lines)

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant