github.com/lxt1045/json@v0.0.0-20231013032136-54d6b1d6e525/tire_tree_test.go (about) 1 package json 2 3 import ( 4 "testing" 5 ) 6 7 func Test_binTree(t *testing.T) { 8 9 t.Run("1", func(t *testing.T) { 10 tags := []*TagInfo{ 11 {TagName: "profile_sidebar_fill_color"}, 12 {TagName: "profile_sidebar_border_color"}, 13 {TagName: "profile_background_tile"}, 14 {TagName: "name"}, 15 {TagName: "profile_image_url"}, 16 {TagName: "created_at"}, 17 {TagName: "location"}, 18 {TagName: "follow_request_sent"}, 19 {TagName: "profile_link_color"}, 20 {TagName: "is_translator"}, 21 {TagName: "id_str"}, 22 {TagName: "entities"}, 23 {TagName: "default_profile"}, 24 {TagName: "contributors_enabled"}, 25 {TagName: "favourites_count"}, 26 {TagName: "url"}, 27 {TagName: "profile_image_url_https"}, 28 {TagName: "utc_offset"}, 29 {TagName: "id"}, 30 {TagName: "profile_use_background_image"}, 31 {TagName: "listed_count"}, 32 {TagName: "profile_text_color"}, 33 {TagName: "lang"}, 34 {TagName: "followers_count"}, 35 {TagName: "protected"}, 36 {TagName: "notifications"}, 37 {TagName: "profile_background_image_url_https"}, 38 {TagName: "profile_background_color"}, 39 {TagName: "verified"}, 40 {TagName: "geo_enabled"}, 41 {TagName: "time_zone"}, 42 {TagName: "description"}, 43 {TagName: "default_profile_image"}, 44 {TagName: "profile_background_image_url"}, 45 {TagName: "statuses_count"}, 46 {TagName: "friends_count"}, 47 {TagName: "following"}, 48 {TagName: "show_all_inline_media"}, 49 {TagName: "screen_name"}, 50 } 51 bintree, err := NewTireTree(tags) 52 if err != nil { 53 t.Fatal(err) 54 } 55 // t.Logf("%+v", bintree) 56 57 for i, k := range tags { 58 tag := bintree.Get(k.TagName + `"`) 59 if tag == nil { 60 t.Fatal(k.TagName) 61 } 62 t.Logf("%d:%+v\n", i, tag.TagName) 63 tag = bintree.Get2(k.TagName + `"`) 64 if tag == nil { 65 t.Fatal(k.TagName) 66 } 67 t.Logf("%d:%+v\n", i, tag.TagName) 68 } 69 }) 70 // return 71 t.Run("1", func(t *testing.T) { 72 tags := []*TagInfo{ 73 {TagName: "id"}, 74 {TagName: "ids"}, 75 {TagName: "title"}, 76 {TagName: "titles"}, 77 {TagName: "price"}, 78 {TagName: "prices"}, 79 {TagName: "hot"}, 80 {TagName: "hots"}, 81 {TagName: "author"}, 82 {TagName: "authors"}, 83 {TagName: "weights"}, 84 } 85 bintree, err := NewTireTree(tags) 86 if err != nil { 87 t.Fatal(err) 88 } 89 // t.Logf("%+v", bintree) 90 91 for i, k := range tags { 92 tag := bintree.Get(k.TagName + `"`) 93 if tag == nil { 94 t.Fatal(k.TagName) 95 } 96 t.Logf("%d:%+v\n", i, tag.TagName) 97 tag = bintree.Get2(k.TagName + `"`) 98 if tag == nil { 99 t.Fatal(k.TagName) 100 } 101 t.Logf("%d:%+v\n", i, tag.TagName) 102 } 103 }) 104 } 105 106 /* 107 go test -benchmem -run=^$ -bench ^Benchmark_binTree$ github.com/lxt1045/json -count=1 -v -cpuprofile cpu.prof -c 108 go test -benchmem -run=^$ -bench ^Benchmark_binTree$ github.com/lxt1045/json -count=1 -v -memprofile cpu.prof -c 109 go tool pprof ./json.test cpu.prof 110 */ 111 func Benchmark_binTree(b *testing.B) { 112 113 tags := []*TagInfo{ 114 {TagName: "id"}, 115 {TagName: "ids"}, 116 {TagName: "title"}, 117 {TagName: "titles"}, 118 {TagName: "price"}, 119 {TagName: "prices"}, 120 {TagName: "hot"}, 121 {TagName: "hots"}, 122 {TagName: "author"}, 123 {TagName: "authors"}, 124 {TagName: "weights"}, 125 } 126 bintree, err := NewTireTree(tags) 127 if err != nil { 128 b.Fatal(err) 129 } 130 // b.Logf("%+v", bintree) 131 132 // b.Run("NewTireTree", func(b *testing.B) { 133 // b.ReportAllocs() 134 // b.ResetTimer() 135 // for i := 0; i < b.N; i++ { 136 // NewTireTree(tags) 137 // } 138 // b.StopTimer() 139 // b.SetBytes(int64(b.N)) 140 // }) 141 142 b.Run("binTree", func(b *testing.B) { 143 b.ReportAllocs() 144 b.ResetTimer() 145 for i := 0; i < b.N; i++ { 146 bintree.Get("authors\"") 147 } 148 b.StopTimer() 149 b.SetBytes(int64(b.N)) 150 }) 151 // return 152 b.Run("binTree-Get2", func(b *testing.B) { 153 b.ReportAllocs() 154 b.ResetTimer() 155 for i := 0; i < b.N; i++ { 156 bintree.Get2("authors\"") 157 } 158 b.StopTimer() 159 b.SetBytes(int64(b.N)) 160 }) 161 // return 162 b.Run("binTree-id", func(b *testing.B) { 163 b.ReportAllocs() 164 b.ResetTimer() 165 for i := 0; i < b.N; i++ { 166 bintree.Get("weights\"") 167 } 168 b.StopTimer() 169 b.SetBytes(int64(b.N)) 170 }) 171 // return 172 b.Run("binTree-id-Get2", func(b *testing.B) { 173 b.ReportAllocs() 174 b.ResetTimer() 175 for i := 0; i < b.N; i++ { 176 bintree.Get2("weights\"") 177 } 178 b.StopTimer() 179 b.SetBytes(int64(b.N)) 180 }) 181 182 b.Run("map", func(b *testing.B) { 183 m := make(map[string]*TagInfo) 184 for _, tag := range tags { 185 m[tag.TagName] = tag 186 } 187 b.ReportAllocs() 188 b.ResetTimer() 189 for i := 0; i < b.N; i++ { 190 key := "author\"" 191 for i := range []byte(key) { 192 if key[i] == '"' { 193 break 194 } 195 } 196 _ = m["author"] 197 } 198 b.StopTimer() 199 b.SetBytes(int64(b.N)) 200 }) 201 } 202 203 func Benchmark_binTree2(b *testing.B) { 204 205 tags := []*TagInfo{ 206 {TagName: "profile_sidebar_fill_color"}, 207 {TagName: "profile_sidebar_border_color"}, 208 {TagName: "profile_background_tile"}, 209 {TagName: "name"}, 210 {TagName: "profile_image_url"}, 211 {TagName: "created_at"}, 212 {TagName: "location"}, 213 {TagName: "follow_request_sent"}, 214 {TagName: "profile_link_color"}, 215 {TagName: "is_translator"}, 216 {TagName: "id_str"}, 217 {TagName: "entities"}, 218 {TagName: "default_profile"}, 219 {TagName: "contributors_enabled"}, 220 {TagName: "favourites_count"}, 221 {TagName: "url"}, 222 {TagName: "profile_image_url_https"}, 223 {TagName: "utc_offset"}, 224 {TagName: "id"}, 225 {TagName: "profile_use_background_image"}, 226 {TagName: "listed_count"}, 227 {TagName: "profile_text_color"}, 228 {TagName: "lang"}, 229 {TagName: "followers_count"}, 230 {TagName: "protected"}, 231 {TagName: "notifications"}, 232 {TagName: "profile_background_image_url_https"}, 233 {TagName: "profile_background_color"}, 234 {TagName: "verified"}, 235 {TagName: "geo_enabled"}, 236 {TagName: "time_zone"}, 237 {TagName: "description"}, 238 {TagName: "default_profile_image"}, 239 {TagName: "profile_background_image_url"}, 240 {TagName: "statuses_count"}, 241 {TagName: "friends_count"}, 242 {TagName: "following"}, 243 {TagName: "show_all_inline_media"}, 244 {TagName: "screen_name"}, 245 } 246 247 bintree, err := NewTireTree(tags) 248 if err != nil { 249 b.Fatal(err) 250 } 251 // b.Logf("%+v", bintree) 252 253 // b.Run("NewTireTree", func(b *testing.B) { 254 // b.ReportAllocs() 255 // b.ResetTimer() 256 // for i := 0; i < b.N; i++ { 257 // NewTireTree(tags) 258 // } 259 // b.StopTimer() 260 // b.SetBytes(int64(b.N)) 261 // }) 262 263 b.Run("binTree", func(b *testing.B) { 264 b.ReportAllocs() 265 b.ResetTimer() 266 for i := 0; i < b.N; i++ { 267 bintree.Get("profile_sidebar_border_color\"") 268 } 269 b.StopTimer() 270 b.SetBytes(int64(b.N)) 271 }) 272 // return 273 b.Run("binTree-Get2", func(b *testing.B) { 274 b.ReportAllocs() 275 b.ResetTimer() 276 for i := 0; i < b.N; i++ { 277 bintree.Get2("profile_sidebar_border_color\"") 278 } 279 b.StopTimer() 280 b.SetBytes(int64(b.N)) 281 }) 282 // return 283 b.Run("binTree-time_zone", func(b *testing.B) { 284 b.ReportAllocs() 285 b.ResetTimer() 286 for i := 0; i < b.N; i++ { 287 bintree.Get("time_zone\"") 288 } 289 b.StopTimer() 290 b.SetBytes(int64(b.N)) 291 }) 292 // return 293 b.Run("binTree-time_zone-Get2", func(b *testing.B) { 294 b.ReportAllocs() 295 b.ResetTimer() 296 for i := 0; i < b.N; i++ { 297 bintree.Get2("time_zone\"") 298 } 299 b.StopTimer() 300 b.SetBytes(int64(b.N)) 301 }) 302 303 b.Run("map", func(b *testing.B) { 304 m := make(map[string]*TagInfo) 305 for _, tag := range tags { 306 m[tag.TagName] = tag 307 } 308 b.ReportAllocs() 309 b.ResetTimer() 310 for i := 0; i < b.N; i++ { 311 key := "profile_sidebar_border_color\"" 312 for i := range []byte(key) { 313 if key[i] == '"' { 314 break 315 } 316 } 317 _ = m["profile_sidebar_border_color"] 318 } 319 b.StopTimer() 320 b.SetBytes(int64(b.N)) 321 }) 322 }