benchmark tests

This commit is contained in:
Michael Yang
2025-06-12 10:56:23 -07:00
parent 4383a3ab7a
commit 12e13573a8
2 changed files with 87 additions and 0 deletions

View File

@@ -4,6 +4,8 @@ import (
"bytes" "bytes"
"math/rand/v2" "math/rand/v2"
"os" "os"
"slices"
"strconv"
"strings" "strings"
"testing" "testing"
@@ -81,3 +83,47 @@ func TestWriteGGUF(t *testing.T) {
}) })
} }
} }
func BenchmarkReadArray(b *testing.B) {
b.ReportAllocs()
create := func(tb testing.TB, kv KV) string {
tb.Helper()
f, err := os.CreateTemp(b.TempDir(), "")
if err != nil {
b.Fatal(err)
}
defer f.Close()
if err := WriteGGUF(f, kv, nil); err != nil {
b.Fatal(err)
}
return f.Name()
}
cases := map[string]any{
"int32": slices.Repeat([]int32{42}, 1_000_000),
"uint32": slices.Repeat([]uint32{42}, 1_000_000),
"float32": slices.Repeat([]float32{42.}, 1_000_000),
"string": slices.Repeat([]string{"42"}, 1_000_000),
}
for name, bb := range cases {
for _, maxArraySize := range []int{-1, 0, 1024} {
b.Run(name+"-maxArraySize="+strconv.Itoa(maxArraySize), func(b *testing.B) {
p := create(b, KV{"array": bb})
for b.Loop() {
f, err := os.Open(p)
if err != nil {
b.Fatal(err)
}
if _, err := Decode(f, maxArraySize); err != nil {
b.Fatal(err)
}
f.Close()
}
})
}
}
}

View File

@@ -3,6 +3,7 @@ package gguf_test
import ( import (
"bytes" "bytes"
"os" "os"
"slices"
"strconv" "strconv"
"strings" "strings"
"testing" "testing"
@@ -247,3 +248,43 @@ func BenchmarkRead(b *testing.B) {
f.Close() f.Close()
} }
} }
func BenchmarkReadArray(b *testing.B) {
b.ReportAllocs()
create := func(tb testing.TB, kv ggml.KV) string {
tb.Helper()
f, err := os.CreateTemp(b.TempDir(), "")
if err != nil {
b.Fatal(err)
}
defer f.Close()
if err := ggml.WriteGGUF(f, kv, nil); err != nil {
b.Fatal(err)
}
return f.Name()
}
cases := map[string]any{
"int32": slices.Repeat([]int32{42}, 1_000_000),
"uint32": slices.Repeat([]uint32{42}, 1_000_000),
"float32": slices.Repeat([]float32{42.}, 1_000_000),
"string": slices.Repeat([]string{"42"}, 1_000_000),
}
for name, bb := range cases {
b.Run(name, func(b *testing.B) {
p := create(b, ggml.KV{"array": bb})
for b.Loop() {
f, err := gguf.Open(p)
if err != nil {
b.Fatal(err)
}
_ = f.KeyValue("array")
f.Close()
}
})
}
}