Aggregate reasoning usage and derive cache hit ratios
This commit is contained in:
53
cache_hit_contract_test.go
Normal file
53
cache_hit_contract_test.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package promptkit_test
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
func TestCacheHitRatioUsesCompleteConsistentCounts(t *testing.T) {
|
||||
known := func(v int) promptkit.ObservedTokenCount {
|
||||
return promptkit.ObservedTokenCount{Value: v, Known: true, Complete: true}
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
input, reads promptkit.ObservedTokenCount
|
||||
ratio float64
|
||||
ok bool
|
||||
}{
|
||||
{name: "fraction", input: known(8), reads: known(3), ratio: .375, ok: true},
|
||||
{name: "zero reads", input: known(8), reads: known(0), ok: true},
|
||||
{name: "zero input", input: known(0), reads: known(0)},
|
||||
{name: "negative reads", input: known(8), reads: known(-1)},
|
||||
{name: "reads exceed input", input: known(8), reads: known(9)},
|
||||
{name: "unknown", input: known(8)},
|
||||
{name: "incomplete", input: known(8), reads: promptkit.ObservedTokenCount{Value: 3, Known: true}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
usage := promptkit.ObservedTokenUsage{PromptTokens: tc.input, CachedTokens: tc.reads}
|
||||
got, ok := usage.CacheHitRatio()
|
||||
if ok != tc.ok || math.Abs(got-tc.ratio) > 1e-12 {
|
||||
t.Fatalf("ratio=%v available=%t", got, ok)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGroupCacheRatioUsesSummedTokensAndDeduplicatesTerminals(t *testing.T) {
|
||||
known := func(v int) promptkit.ObservedTokenCount {
|
||||
return promptkit.ObservedTokenCount{Value: v, Known: true, Complete: true}
|
||||
}
|
||||
var group promptkit.ExecutionGroup
|
||||
first := promptkit.ExecutionEvent{RunID: "a", Kind: promptkit.ExecutionFinished, Usage: promptkit.ObservedTokenUsage{PromptTokens: known(10), CachedTokens: known(10), ReasoningTokens: known(1)}}
|
||||
second := promptkit.ExecutionEvent{RunID: "b", Kind: promptkit.ExecutionFinished, Usage: promptkit.ObservedTokenUsage{PromptTokens: known(90), CachedTokens: known(0), ReasoningTokens: known(2)}}
|
||||
group.Observe(first)
|
||||
group.Observe(first)
|
||||
group.Observe(second)
|
||||
snapshot := group.Snapshot()
|
||||
ratio, ok := snapshot.Usage.CacheHitRatio()
|
||||
if !ok || math.Abs(ratio-.1) > 1e-12 || snapshot.Usage.ReasoningTokens != known(3) {
|
||||
t.Fatalf("summary=%+v ratio=%v available=%t", snapshot, ratio, ok)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user