Skip to content

Per-request perf log ​

Instrumentation is opt-in: it is installed only when PIXIVFE_INSTRUMENT is set to any non-empty value. Without it, RequestPerf is not registered and trace.Phase records nothing, so Server-Timing keeps its previous shape.

With PIXIVFE_INSTRUMENT=1, middleware.RequestPerf writes one structured line per request:

WRN bytes=1653078 cpu_ms=197.6 dur=184.3 goroutines=24 method=GET
    phases=["build index data=163ms@+0ms", "fetch landing page=4ms@+0ms",
            "GET www.pixiv.net/ajax/top/illust?mode=all=4ms@+0ms",
            "parse landing page=95ms@+3ms", "populate thumbnails=51ms@+98ms",
            "build personalized sections=6ms@+149ms", "fetch rankings=8ms@+155ms",
            "render index template=20ms@+163ms", "write response body=1ms@+183ms"]
    request_id=... sched_ms=0.48 status=200 sys=perf url=/?l=1
  • Routine requests log at debug; requests at or over one second log at warn, so the default info level shows only slow ones.
  • phases is label=duration@offset from the start of the request, oldest first. It contains the labelled Server-Timing metrics of the request.
  • cpu_ms is process user Go CPU consumed during the request (not the request's goroutine alone). sched_ms is the summed run-queue latency of goroutines that ran in the window. Both are process-wide, so concurrent requests overlap; on a quiet server they are a fair approximation.
  • On streamed HTML routes such as GET /, response headers (including Server-Timing) are committed when the shell flushes before upstream fetches run, so post-flush phases appear in this perf log rather than Server-Timing.
  • Enable the debug lines with PIXIVFE_LOG_LEVEL=debug (both variables are read at startup).

Phase names ​

Application phases are recorded with trace.Phase:

NameWhere
index.dataroutes.IndexPage, whole data build
index.landingcore.GetIndex, landing fetch (cache hit or upstream)
index.parselanding JSON unmarshal
index.thumbnailsthumbnail URL rewriting for every artwork
index.sectionslogged-in personalized sections
index.rankingsfetchRankings
ranking.listranking list fetch
ranking.detailsranking detail batch fetch and unmarshal
index.rendertempl render into the response buffer
response.writewriting the buffered body to the client

Upstream requests carry a label such as GET www.pixiv.net/ajax/top/illust?mode=all instead of the base64 Server-Timing name. Queries longer than 48 bytes are elided.

Profiling ​

The existing debug routes are enabled by PIXIVFE_DEV=true:

sh
curl 'http://localhost:8282/debug/pprof/profile?seconds=30' -o cpu.pprof
go tool pprof -http=:8080 cpu.pprof
curl http://localhost:8282/debug/flight -o trace.out   # past runtime trace
go tool trace trace.out

See pprof and trace.