mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-12-03 18:43:38 +01:00
Precompute canonical registry sort keys once per row, compare immutable JSON object fields before decoding unchanged nested metadata, and reuse one exact baseline-to-current frame within a serial dispatch. Each client's hydration, frame limit, queue acceptance and tenant selection remain authoritative. No cross-broadcast conversion or content-hash cache is introduced. Reported connected-path evidence: Pulse #2199, AndyProsser comment 6013106749. Contract-Neutral: Same canonical ordering, exact-number JSON merge semantics and per-client/tenant state protocol; only transient computation and allocations change. Change-source: pulse-maintainer
70 lines
2.1 KiB
Go
70 lines
2.1 KiB
Go
package unifiedresources
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
"reflect"
|
|
"sort"
|
|
"testing"
|
|
)
|
|
|
|
func referenceSortResources(rows []Resource) {
|
|
sort.SliceStable(rows, func(i, j int) bool { return CompareResourcesByCanonicalName(rows[i], rows[j]) < 0 })
|
|
}
|
|
func sortKeyFixture(count int) []Resource {
|
|
r := rand.New(rand.NewSource(2199))
|
|
rows := make([]Resource, count)
|
|
names := []string{" Mixed-Case ", "mixed-case", "Σίσυφος", " İSTANBUL ", "", "ZZZ", "aaa"}
|
|
for i := range rows {
|
|
rows[i] = Resource{ID: fmt.Sprintf("%d", r.Intn(count/2+1)), Name: names[r.Intn(len(names))], Type: ResourceType(fmt.Sprintf("kind-%d", r.Intn(4))), Uptime: int64(i)}
|
|
}
|
|
return rows
|
|
}
|
|
func TestPrecomputedResourceSortMatchesCanonicalStableOrder(t *testing.T) {
|
|
for _, count := range []int{0, 1, 2, 17, 1236} {
|
|
rows := sortKeyFixture(count)
|
|
want := append([]Resource(nil), rows...)
|
|
referenceSortResources(want)
|
|
sortResourcesByName(rows)
|
|
if !reflect.DeepEqual(rows, want) && len(rows) > 0 {
|
|
t.Fatalf("canonical/stable order changed at size %d", count)
|
|
}
|
|
}
|
|
}
|
|
|
|
type sortTestView struct {
|
|
name, id string
|
|
sequence int
|
|
}
|
|
|
|
func (v sortTestView) Name() string { return v.name }
|
|
func (v sortTestView) ID() string { return v.id }
|
|
func TestPrecomputedViewSortMatchesCanonicalStableOrder(t *testing.T) {
|
|
rows := []sortTestView{{" B ", "z", 0}, {"b", "a", 1}, {"b", "a", 2}, {"A", "c", 3}, {"", "d", 4}, {"İ", "e", 5}}
|
|
want := append([]sortTestView(nil), rows...)
|
|
sort.SliceStable(want, func(i, j int) bool {
|
|
return compareResourceNameIdentity(want[i].Name(), "", want[i].ID(), want[j].Name(), "", want[j].ID()) < 0
|
|
})
|
|
sortNamedResourceViewsByName(rows)
|
|
if !reflect.DeepEqual(rows, want) {
|
|
t.Fatalf("view order changed: %v", rows)
|
|
}
|
|
}
|
|
func BenchmarkCanonicalResourceSort(b *testing.B) {
|
|
seed := sortKeyFixture(1236)
|
|
for _, impl := range []struct {
|
|
name string
|
|
sort func([]Resource)
|
|
}{{"reference", referenceSortResources}, {"precomputed", sortResourcesByName}} {
|
|
b.Run(impl.name, func(b *testing.B) {
|
|
rows := make([]Resource, len(seed))
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
copy(rows, seed)
|
|
impl.sort(rows)
|
|
}
|
|
})
|
|
}
|
|
}
|