github.com/bytedance/gopkg@v0.0.0-20240514070511-01b2cbcf35e1/cloud/metainfo/kvstore_test.go (about) 1 // Copyright 2023 ByteDance Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package metainfo 16 17 import ( 18 "fmt" 19 "testing" 20 ) 21 22 func TestKVStore(t *testing.T) { 23 store := newKVStore() 24 store["a"] = "a" 25 store["a"] = "b" 26 if store["a"] != "b" { 27 t.Fatal() 28 } 29 store.recycle() 30 if store["a"] == "b" { 31 t.Fatal() 32 } 33 store = newKVStore() 34 if store["a"] == "b" { 35 t.Fatal() 36 } 37 } 38 39 func BenchmarkMap(b *testing.B) { 40 for keys := 1; keys <= 1000; keys *= 10 { 41 b.Run(fmt.Sprintf("keys=%d", keys), func(b *testing.B) { 42 b.ReportAllocs() 43 for i := 0; i < b.N; i++ { 44 m := make(map[string]string) 45 for idx := 0; idx < 1000; idx++ { 46 m[fmt.Sprintf("key-%d", idx)] = string('a' + byte(idx%26)) 47 } 48 } 49 }) 50 } 51 } 52 53 func BenchmarkKVStore(b *testing.B) { 54 for keys := 1; keys <= 1000; keys *= 10 { 55 b.Run(fmt.Sprintf("keys=%d", keys), func(b *testing.B) { 56 b.ReportAllocs() 57 for i := 0; i < b.N; i++ { 58 m := newKVStore() 59 for idx := 0; idx < 1000; idx++ { 60 m[fmt.Sprintf("key-%d", idx)] = string('a' + byte(idx%26)) 61 } 62 m.recycle() 63 } 64 }) 65 } 66 }