Debian Codegraph @

internal/encoder/map_sort_test.go

raw
1 package encoder
2
3 import (
4 "math/rand"
5 "slices"
6 "strconv"
7 "testing"
8 )
9
10 // The keys of the objects of the Twitter payload of the benchmarks, which most of the keys of a JSON object are
11 // like: many share their first bytes.
12 var twitterObjectKeys = [][]string{
13 {"max_id", "since_id", "refresh_url", "next_results", "count", "completed_in", "since_id_str", "query", "max_id_str"},
14 {"coordinates", "favorited", "truncated", "created_at", "id_str", "entities", "in_reply_to_user_id_str", "contributors",
15 "text", "metadata", "retweet_count", "in_reply_to_status_id_str", "id", "geo", "retweeted", "in_reply_to_user_id",
16 "place", "user", "in_reply_to_screen_name", "source", "in_reply_to_status_id"},
17 {"profile_sidebar_fill_color", "profile_sidebar_border_color", "profile_background_tile", "name", "profile_image_url",
18 "created_at", "location", "follow_request_sent", "profile_link_color", "is_translator", "id_str", "entities",
19 "default_profile", "contributors_enabled", "favourites_count", "url", "profile_image_url_https", "utc_offset", "id",
20 "profile_use_background_image", "listed_count", "profile_text_color", "lang", "followers_count", "protected",
21 "notifications", "profile_background_image_url_https", "profile_background_color", "verified", "geo_enabled",
22 "time_zone", "description", "default_profile_image", "profile_background_image_url", "statuses_count",
23 "friends_count", "following", "show_all_inline_media", "screen_name"},
24 }
25
26 // sortKeysInputs returns key sets of every size, in random orders as a map is ranged over: the keys of the Twitter
27 // payload, keys which share their first eight bytes or all of them, keys of zeros, and many keys.
28 func sortKeysInputs() [][]string {
29 r := rand.New(rand.NewSource(1))
30 var sets [][]string
31 sets = append(sets, twitterObjectKeys...)
32 for _, n := range []int{0, 1, 2, 3, 15, 16, 17, 40, 100, 1000} {
33 var same, prefixed, zeros []string
34 for i := 0; i < n; i++ {
35 same = append(same, "key_of_the_same_prefix_"+strconv.Itoa(r.Intn(1<<20)))
36 prefixed = append(prefixed, strconv.Itoa(r.Intn(1<<30)))
37 zeros = append(zeros, string(make([]byte, r.Intn(12)))+strconv.Itoa(i))
38 }
39 sets = append(sets, same, prefixed, zeros)
40 }
41 var inputs [][]string
42 for _, set := range sets {
43 for i := 0; i < 4; i++ {
44 keys := slices.Clone(set)
45 r.Shuffle(len(keys), func(a, b int) { keys[a], keys[b] = keys[b], keys[a] })
46 inputs = append(inputs, keys)
47 }
48 }
49 return inputs
50 }
51
52 // SortKeys puts the entries in the order of their keys, as a sort of the strings does, for keys of any size and
53 // any first bytes, in any order.
54 func TestSortKeys(t *testing.T) {
55 for _, keys := range sortKeysInputs() {
56 c := &MapContext{Keys: keys}
57 c.SortKeys()
58 got := make([]string, len(keys))
59 for i, e := range c.Order {
60 got[i] = keys[e]
61 }
62 want := slices.Clone(keys)
63 slices.Sort(want)
64 if !slices.Equal(got, want) {
65 t.Fatalf("keys %q:\n got %q\nwant %q", keys, got, want)
66 }
67 }
68 }
69
70 func BenchmarkSortKeys(b *testing.B) {
71 r := rand.New(rand.NewSource(2))
72 for i, keys := range twitterObjectKeys {
73 var orders [][]string
74 for j := 0; j < 8; j++ {
75 k := slices.Clone(keys)
76 r.Shuffle(len(k), func(a, b int) { k[a], k[b] = k[b], k[a] })
77 orders = append(orders, k)
78 }
79 b.Run("twitter"+strconv.Itoa(i)+"/"+strconv.Itoa(len(keys)), func(b *testing.B) {
80 c := &MapContext{}
81 for n := 0; n < b.N; n++ {
82 c.Keys = orders[n%len(orders)]
83 c.SortKeys()
84 }
85 })
86 }
87 }