Skip to content

Commit

Permalink
measure shuffle distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
myaaaaaaaaa committed Dec 16, 2024
1 parent 04ad5af commit 6bf919c
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions gojqx_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jqx

import (
"math"
"slices"
"testing"
)
Expand Down Expand Up @@ -70,3 +71,37 @@ func TestShuffle(t *testing.T) {
query = new(State).Compile(`def seq(f): range(4) | [range(.)] | [shuffle(range(30)|f)]; seq(.),seq(5) | unique | length`)
assertString(t, slices.Collect(query(nil)), `[1 1 2 6 1 1 1 1]`)
}
func BenchmarkShuffle(b *testing.B) {
lists := []string{
`[1,2]`,
`[1,2,3]`,
}

for _, list := range lists {
b.Run(list, func(b *testing.B) {
query := new(State).Compile(`
def fac($n): if $n==0 then 1 else $n*fac($n-1) end;
. as $n | ` + constString(list) + `
| reduce shuffle(range($n*fac(length))) as $perm (
{};
.[$perm|join("")] |= .+1
)
`)

ss := slices.Collect(query(b.N))
if len(ss) != 1 {
panic(len(ss))
}

buckets := ss[0].(map[string]any)
for k, v := range buckets {
v := float64(v.(int))
b.ReportMetric(v, k)
v /= float64(b.N)
v -= 1
v *= 100
b.ReportMetric(math.Abs(v), "%-err-"+k)
}
})
}
}

0 comments on commit 6bf919c

Please sign in to comment.