Building the chat / How fast it is
How fast it is
How long does the model take to answer, and is it slower today than last
week? How many replies fail? This chapter counts replies and times each model
call, then reads the numbers back as rates, averages and percentiles. It uses
the Metrics engine, which keeps samples in metrics.db exactly as they were
measured.
Open the instruments
const replies = store.metrics.counter<'model' | 'outcome'>('chat_replies_total', { help: 'Replies written.' })
const modelTime = store.metrics.timer<'model'>('chat_model_ms', {
buckets: [250, 500, 1000, 2500, 5000, 10000, 30000],
})
const waiting = store.metrics.gauge('chat_replies_waiting', { unit: 'jobs' })replies = store.metrics.counter("chat_replies_total", help="Replies written.")
model_time = store.metrics.timer("chat_model_ms", buckets=[250, 500, 1000, 2500, 5000, 10000, 30000])
waiting = store.metrics.gauge("chat_replies_waiting", unit="jobs")replies, err := metrics.OpenCounter(store, "chat_replies_total", metrics.Help("Replies written."))
modelTime, err := metrics.OpenTimer(store, "chat_model_ms", metrics.Buckets(250, 500, 1000, 2500, 5000, 10000, 30000))
waiting, err := metrics.OpenGauge(store, "chat_replies_waiting", metrics.Unit("jobs"))let stats = store.metrics()?;
let replies = stats.counter("chat_replies_total").help("Replies written.").open()?;
let model_time = stats
.timer("chat_model_ms")
.buckets(Buckets::Bounds(vec![250.0, 500.0, 1000.0, 2500.0, 5000.0, 10000.0, 30000.0]))
.open()?;
let waiting = stats.gauge("chat_replies_waiting").unit("jobs").open()?;| Instrument | Keeps | In the chat |
|---|---|---|
| counter | a total that only goes up | replies written, by model and outcome |
| timer | how many things took how long: count, sum, longest, and spread | each model call's duration |
| gauge | a value that goes up and down | replies waiting in the queue |
| histogram | how many values fell in each range: count, sum, spread | attachment sizes, below |
A timer's buckets are the upper bounds of its spread in milliseconds. They
make percentiles possible: "95% of model calls took at most 2.5 seconds".
Measure
In the reply worker:
const answer = await modelTime.with({ model: 'haiku' }).measure(() => model.answer(history))
replies.with({ model: 'haiku', outcome: 'ok' }).inc()
waiting.set(await replyQueueLength())async with model_time.labels(model="haiku").measure():
answer = await model.answer(history)
replies.labels(model="haiku", outcome="ok").inc()
waiting.set(await reply_queue_length())with (labels in Python) picks the series by its labels: each set of
labels is a series of its own. A call never waits and never writes: it
changes a number in memory.
Four web processes and a worker that count into chat_replies_total count
into one total. A program on Bun, Node, Python or Go sends what changed
every second, and the store adds it to the totals it writes every 15
seconds. So a process that crashes loses about its last second, and a crash
of the store's own process its last 15 seconds (One total across
processes).
Sizes, not durations
A histogram is a timer of numbers that are not durations. The chat records the size of each attachment, to see how large uploads really are:
const sizes = store.metrics.histogram('chat_attachment_bytes', {
unit: 'bytes',
buckets: [10_000, 100_000, 1_000_000, 5_000_000, 20_000_000],
})
sizes.record(file.size)sizes = store.metrics.histogram(
"chat_attachment_bytes", unit="bytes", buckets=[10_000, 100_000, 1_000_000, 5_000_000, 20_000_000]
)
sizes.record(info.size)sizes, err := metrics.OpenHistogram(store, "chat_attachment_bytes",
metrics.Unit("bytes"), metrics.Buckets(10_000, 100_000, 1_000_000, 5_000_000, 20_000_000))
sizes.Record(float64(info.Size))A histogram keeps the same series a timer does: a count, a sum and a count
per bucket, under chat_attachment_bytes_count, _sum and _bucket, so
its percentiles and averages read the same way. A negative value is
refused.
Read the numbers
How many replies failed in the last hour, by outcome:
const { groups: outcomes } = await store.metrics.aggregate({
name: 'chat_replies_total',
since: '1h',
op: 'increase',
by: ['outcome'],
})
// [{ labels: { outcome: 'failed' }, buckets: [{ value: 1, … }] }, { labels: { outcome: 'ok' }, buckets: [{ value: 19, … }] }]outcomes = (await store.metrics.aggregate(name="chat_replies_total", since="1h", op="increase", by=["outcome"])).groupsHow slow the model is, every five minutes:
const { groups: p95 } = await store.metrics.aggregate({ name: 'chat_model_ms', since: '1h', every: '5m', op: 'p95' })
// [{ labels: {}, buckets: [{ at, lower: 1000, upper: 2500, samples: 312 }, …] }]
const { groups: mean } = await store.metrics.aggregate({ name: 'chat_model_ms', since: '1h', op: 'avg' })p95 = (await store.metrics.aggregate(name="chat_model_ms", since="1h", every="5m", op="p95")).groups
mean = (await store.metrics.aggregate(name="chat_model_ms", since="1h", op="avg")).groupsAn aggregate cuts the range into buckets of every, counted from 1970 in
UTC, and answers one exact value per bucket: Ops
lists what each op answers. A timer's percentile answers with the two
bounds of the bucket that holds it: lower: 1000, upper: 2500 means "the
95th percentile lies between one and two and a half seconds", as
Percentiles explains.
The store's own metrics
Open the store with selfMetrics, and it measures itself into the same
metrics.db: its commits and how long each write waited, its files, its
memory, its disk and each engine's own numbers, under names that start with
tinystore_. A program may not write such a name itself. The store's own
metrics shows how to turn it on and
lists every name.
Why this design?
Why a sample is kept bit for bit. Whoever draws the chart may round, but nobody can unround, as How logs and metrics pack shows.
Why an aggregate is exact and rounded once. An average is the sum of all samples over their count, never an average of averages, so the same question gives the same answer, however the data was packed. A percentile of a timer is two bounds, because a number between them would look measured but be a guess, and buckets of any width add up to the range: see Aggregates.
Why instruments send changes, not totals. If each process wrote its own total, four processes would make four series, and a restart would look like a reset. Each process sends what changed since its last send, and the store keeps one total. A send that a lost connection repeats is counted once.
See also
- Replies in the background: the worker the timer measures.
- What it is doing: logs and events beside the numbers.
- Limits and defaults: every
bound of Metrics, from a name's
keepto the buckets of an aggregate. - Metrics API reference: every instrument, read and aggregate.