Runs list carries per-run token usage; app shows tokens beside size, Most tokens sort, Summary and stage tokens
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// TestListRunsCarriesRunUsageAggregate proves the run list carries each run's
|
||||
// usage total from the grouped aggregate, equal to the detail's TotalUsage over
|
||||
// the same rows, marks a partial total by its stage counts, leaves a run with
|
||||
// no recorded usage absent rather than zero, and stays correct across list
|
||||
// batches.
|
||||
func TestListRunsCarriesRunUsageAggregate(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store, err := Open(filepath.Join(t.TempDir(), "list-usage.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
if _, err := store.Submit(ctx, "list-usage", Template([]string{"one"}, true)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items, _ := store.List(ctx)
|
||||
definition, _ := store.Get(ctx, items[0].ID, 1)
|
||||
create := func(key string) int64 {
|
||||
id, _, err := store.CreateRun(ctx, definition, key, "/repo", false, nil, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
full := create("full")
|
||||
finishUsageStep(t, store, full, 1, "one", &protocol.StageUsage{TokensIn: 20_000_000, TokensOut: 2_400_000, TokensTotal: 22_400_000, TokensKnown: true, SplitKnown: true, CostUSD: 1.5, CostKnown: true})
|
||||
finishUsageStep(t, store, full, 2, "one", &protocol.StageUsage{TokensIn: 10, TokensOut: 5, TokensTotal: 15, TokensKnown: true, SplitKnown: true, CostUSD: .25, CostKnown: true})
|
||||
partial := create("partial")
|
||||
finishUsageStep(t, store, partial, 1, "one", &protocol.StageUsage{TokensTotal: 9, TokensKnown: true})
|
||||
finishUsageStep(t, store, partial, 2, "one", &protocol.StageUsage{CostUSD: .1, CostKnown: true})
|
||||
none := create("none")
|
||||
finishUsageStep(t, store, none, 1, "one", nil)
|
||||
// Enough further runs to push the first ones past one list batch.
|
||||
const fillerRuns = 105
|
||||
for i := range fillerRuns {
|
||||
filler := create(fmt.Sprintf("filler-%d", i))
|
||||
finishUsageStep(t, store, filler, 1, "one", &protocol.StageUsage{TokensTotal: int64(i + 1), TokensKnown: true})
|
||||
}
|
||||
|
||||
runs, err := store.ListRuns(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
byID := map[int64]protocol.RunSummary{}
|
||||
for _, run := range runs {
|
||||
byID[run.ID] = run
|
||||
}
|
||||
for _, id := range []int64{full, partial} {
|
||||
detail, err := store.GetRun(ctx, id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
listed := byID[id].Usage
|
||||
if listed == nil || detail.Usage == nil || *listed != *detail.Usage {
|
||||
t.Fatalf("run %d list usage %+v != detail usage %+v", id, listed, detail.Usage)
|
||||
}
|
||||
}
|
||||
if got := byID[full].Usage; got.TokensTotal != 22_400_015 || got.TokensIn != 20_000_010 || got.TokensOut != 2_400_005 || !got.SplitKnown || got.CostUSD != 1.75 || got.TokensKnownStages != 2 || got.Stages != 2 {
|
||||
t.Fatalf("full run usage = %+v", got)
|
||||
}
|
||||
if got := byID[partial].Usage; got.TokensTotal != 9 || got.TokensKnownStages != 1 || got.Stages != 2 || got.SplitKnown || got.CostKnownStages != 1 || got.CostUnknownStages != 1 {
|
||||
t.Fatalf("partial run usage = %+v", got)
|
||||
}
|
||||
if got := byID[none].Usage; got != nil {
|
||||
t.Fatalf("run without usage listed a total: %+v", got)
|
||||
}
|
||||
var fillers int64
|
||||
for _, run := range runs {
|
||||
if run.ID == full || run.ID == partial || run.ID == none {
|
||||
continue
|
||||
}
|
||||
if run.Usage == nil {
|
||||
t.Fatalf("filler run %d lost its usage", run.ID)
|
||||
}
|
||||
fillers += run.Usage.TokensTotal
|
||||
}
|
||||
if fillers != fillerRuns*(fillerRuns+1)/2 {
|
||||
t.Fatalf("filler totals = %d", fillers)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user