Skip to content

chore(#124): STG レイテンシー計測スクリプトを追加 - #139

Draft
bobtaroh wants to merge 1 commit into
developfrom
perf/issue-124-staging-measurement
Draft

chore(#124): STG レイテンシー計測スクリプトを追加#139
bobtaroh wants to merge 1 commit into
developfrom
perf/issue-124-staging-measurement

Conversation

@bobtaroh

Copy link
Copy Markdown
Collaborator

概要

Issue #124 のボトルネック特定・改善検証に使う計測スクリプトを scripts/ 配下に追加する。
クエリ最適化 PR / DB 移行 PR の効果検証で再利用する想定で draft として置いておく。

詳細

  • scripts/measure_staging.py
    • httpx + asyncio で url_id から trip_id / page_id / block_id を引いて Cookie を確立
    • 副作用の無い GET 6 エンドポイント (/health, /trips/url/{url_id}, /trips/{trip_id}, /trips/{trip_id}/pages, /pages/{page_id}/blocks, /blocks/{block_id}) を直列に N 回叩く
    • クライアント側 total_ms を p50 / p95 / avg / min / max で集計し、サンプル全件を JSONL で書き出して Cloud Logging との突き合わせを可能にする
  • scripts/fetch_latency_logs.sh
    • gcloud logging read で Cloud Run の jsonPayload.type="request" ログを期間指定で取得して JSONL に整形

動作確認

2026-05-23 に STG で実測実施。同じスクリプトで us-east-1 (現状) と Singapore (検証用) の両方を計測し、Cloud Logging の計装ログと突き合わせて 理論通りの RTT 半減 を確認した。

route 別 p50/p95 比較(n=30、ウォームアップ 5)

route tot_p50 us-east1 tot_p50 sg tot_p95 us-east1 tot_p95 sg 削減率 (p50)
/trips/url/{url_id} 1477ms 727ms 1607ms 821ms -49%
/trips/{trip_id} 1481ms 728ms 1651ms 808ms -51%
/trips/{trip_id}/pages 1313ms 647ms 1473ms 725ms -51%
/pages/{page_id}/blocks 1310ms 646ms 1630ms 725ms -51%
/blocks/{block_id} 1147ms 644ms 1478ms 723ms -44%

計装内訳の比較

  • db_max_ms (PK SELECT 1 本) p50: us-east1=325.7ms → sg=159.6ms(≒ RTT 半減)
  • 1 クエリあたり平均: us-east1=210ms → sg=108ms(同上)
  • 計装外 DB 時間 (total_ms - db_total_ms) p50: us-east1=660ms → sg=325ms(同上)。pool_pre_pingSELECT 1 や接続確立、prepare phase など middleware で測れていない部分も RTT 律速だったことが判明
  • /health p50: us-east1=37ms → sg=23ms(≒アプリ純粋オーバーヘッド)

使い方

# クライアント側計測
uv run --with httpx --no-project scripts/measure_staging.py \
    --base-url https://tabi-share-api-staging-jsmfpzrd6q-an.a.run.app \
    --url-id <STG の url_id> \
    --warmup 5 --iters 30 \
    --jsonl /tmp/samples.jsonl

# Cloud Logging から計装ログを取得
GCP_PROJECT_ID=tabi-share-8ef6b ./scripts/fetch_latency_logs.sh staging \
    "2026-05-23T14:20:00Z" "2026-05-23T14:23:00Z" > /tmp/logs.jsonl

確認項目

Issue #124 のボトルネック特定のため、計装ログを取得 / 突き合わせる
2 種のスクリプトを追加する。

- scripts/measure_staging.py:
    httpx + asyncio で url_id から trip_id / page_id / block_id を引いて
    Cookie を確立し、副作用の無い GET 6 エンドポイントをウォームアップ後に
    N 回ずつ直列に叩く。クライアント側 total_ms を p50 / p95 / avg / min / max
    で集計し、サンプル全件を JSONL で書き出して Cloud Logging と突き合わせ可能にする。
- scripts/fetch_latency_logs.sh:
    gcloud logging read で staging / production の Cloud Run jsonPayload.type="request"
    ログを期間指定で取得し、JSONL で stdout に出す。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 introduces a set of utility scripts designed to benchmark and analyze API latency in the staging environment. These tools facilitate bottleneck identification and performance validation for upcoming query and database optimization tasks by providing both client-side measurement and server-side log aggregation.

Highlights

  • Latency Measurement Script: Added scripts/measure_staging.py to perform automated latency benchmarking on staging endpoints using httpx and asyncio.
  • Log Retrieval Utility: Added scripts/fetch_latency_logs.sh to extract and format Cloud Run request logs from Google Cloud Logging for performance analysis.
New Features

🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

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 the 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 counterproductive. 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.

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
Copy Markdown

Visit the preview URL for this PR (updated for commit 585c833):

https://tabi-share-8ef6b--pr139-perf-issue-124-stagi-jpch50ex.web.app

(expires Sat, 30 May 2026 14:46:29 GMT)

🔥 via Firebase Hosting GitHub Action 🌎

Sign: 9f2a87ede127df7673322845e34cf22c1372d720

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

Copy link
Copy Markdown
Contributor

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 introduces two new scripts for performance monitoring: a shell script to fetch latency logs from Google Cloud Logging and a Python script to measure endpoint latency on the staging environment. The review feedback suggests improving code quality by replacing a lambda assignment with a formal function definition and adding input validation for the iteration count to prevent potential runtime errors during statistical analysis.

async def _run(args: argparse.Namespace) -> None:
base_url = args.base_url.rstrip("/")
started_at = datetime.now(UTC).isoformat()
log = lambda msg: print(msg, file=sys.stderr) # noqa: E731

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

PEP 8 では、lambda 式を直接変数に代入するのではなく、def を使用して関数を定義することが推奨されています。可読性と保守性の向上のため、ローカル関数として定義することをお勧めします。これにより noqa: E731 の指定も不要になります。

Suggested change
log = lambda msg: print(msg, file=sys.stderr) # noqa: E731
def log(msg: str) -> None:
print(msg, file=sys.stderr)

help="サンプル全件を書き出す JSONL ファイルパス (省略時は書き出さない)",
)
args = parser.parse_args()
asyncio.run(_run(args))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

--iters に 0 以下の値が指定された場合、統計計算(statistics.fmean, min, max など)において StatisticsErrorValueError が発生し、スクリプトがクラッシュします。実行前にバリデーションを追加して、1 以上の値を強制するようにしてください。

Suggested change
asyncio.run(_run(args))
if args.iters < 1:
parser.error("--iters must be at least 1")
asyncio.run(_run(args))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant