Watch
1
0
Fork
You've already forked pkg-proxy
1
mirror of https://github.com/git-pkgs/proxy.git synced 2026-11-09 07:39:19 -05:00
pkg-proxy/internal/metadata/npm/stream_bench_test.go
Simon Chrzanowski 44afe6997a Read jsonstream objects with jsontext
CopyObject takes the reader and decodes it with jsontext, accepting
repeated names and invalid UTF-8 as encoding/json did. Keys are written
with the bytes they were read with instead of being re-encoded. Member
gets Skip, which reads past a value without keeping it, and npm's
RewriteStream skips dropped versions and duplicates with it.

conda-forge noarch repodata.json (195 MB): filtering 0.89 to 0.62 s and
379 to 77 MB allocated, the timestamp index 0.84 to 0.61 s. The next
packument (32 MB): RewriteStream 69 to 34 ms and 38 to 3.5 MB.
2026-11-08 19:03:21 +01:00

59 lines
1.4 KiB
Go

package npm
import (
"bytes"
"io"
"os"
"testing"
"github.com/git-pkgs/proxy/internal/metadata/npm/npmbench"
)
func BenchmarkScanPackument(b *testing.B) {
body := npmbench.Packument(5000)
b.SetBytes(int64(len(body)))
b.ReportAllocs()
for b.Loop() {
if _, err := ScanPackument(bytes.NewReader(body)); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkLookupVersion(b *testing.B) {
body := npmbench.Packument(5000)
b.SetBytes(int64(len(body)))
b.ReportAllocs()
for b.Loop() {
entry, err := LookupVersion(bytes.NewReader(body), "1.0.2500")
if err != nil || entry.Entry == nil || entry.Published == "" {
b.Fatalf("LookupVersion = %+v, %v", entry, err)
}
}
}
// BenchmarkRewriteStream times RewriteStream's second pass, the copy, over
// the 5,000-version packument, or the one NPM_PACKUMENT_BENCH names.
func BenchmarkRewriteStream(b *testing.B) {
body := npmbench.Packument(5000)
name := "big"
if path := os.Getenv("NPM_PACKUMENT_BENCH"); path != "" {
var err error
if body, err = os.ReadFile(path); err != nil {
b.Fatal(err)
}
name = os.Getenv("NPM_PACKUMENT_BENCH_NAME")
}
scan, err := ScanPackument(bytes.NewReader(body))
if err != nil {
b.Fatal(err)
}
options := Options{ProxyURL: "http://proxy.example"}
b.SetBytes(int64(len(body)))
b.ReportAllocs()
for b.Loop() {
if _, err := RewriteStream(name, scan, bytes.NewReader(body), io.Discard, options); err != nil {
b.Fatal(err)
}
}
}