├── .github └── workflows │ └── go.yml ├── LICENSE ├── README.md ├── caches_bench_test.go ├── caches_gc_overhead_comparison.go ├── go.mod └── go.sum /.github/workflows/go.yml: -------------------------------------------------------------------------------- 1 | name: Go 2 | on: [push, pull_request] 3 | jobs: 4 | 5 | build: 6 | name: Build 7 | runs-on: ubuntu-latest 8 | strategy: 9 | fail-fast: true 10 | max-parallel: 2 11 | matrix: 12 | go: ["stable", "oldstable"] 13 | steps: 14 | - name: Set up Go ${{matrix.go}} 15 | uses: actions/setup-go@v3 16 | with: 17 | go-version: ${{matrix.go}} 18 | check-latest: true 19 | id: go 20 | - name: Check out code into the Go module directory 21 | uses: actions/checkout@v3 22 | - name: Lint code 23 | run: | 24 | gofiles=$(find ./ -name '*.go') && [ -z "$gofiles" ] || unformatted=$(goimports -l $gofiles) && [ -z "$unformatted" ] || (echo >&2 "Go files must be formatted with gofmt. Following files has problem: $unformatted" && true); 25 | diff <(echo -n) <(gofmt -s -d .) 26 | export PATH=$PATH:$(go env GOPATH)/bin # temporary fix. See https://github.com/actions/setup-go/issues/14 27 | go install golang.org/x/lint/golint@latest 28 | golint ./... 29 | - name: Static code check 30 | run: | 31 | export PATH=$PATH:$(go env GOPATH)/bin 32 | go install github.com/gordonklaus/ineffassign@latest 33 | ineffassign ./... 34 | go vet ./... 35 | - name: Build 36 | run: go build -v . 37 | 38 | test: 39 | name: Test 40 | runs-on: ubuntu-latest 41 | strategy: 42 | fail-fast: true 43 | max-parallel: 2 44 | matrix: 45 | go: ["stable", "oldstable"] 46 | steps: 47 | - name: Set up Go ${{matrix.go}} 48 | uses: actions/setup-go@v3 49 | with: 50 | go-version: ${{matrix.go}} 51 | check-latest: true 52 | id: go 53 | - name: Check out code into the Go module directory 54 | uses: actions/checkout@v3 55 | - name: Run Test 56 | run: go test -v ./... 57 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # bigcache-bench 2 | 3 | Benchmarks for BigCache project 4 | 5 | ### GC pause time 6 | ``` 7 | # go version 8 | go version go1.20.2 linux/amd64 9 | 10 | # go run caches_gc_overhead_comparison.go -cache bigcache 11 | Cache: bigcache 12 | Number of entries: 20000000 13 | Number of repeats: 50 14 | Value size: 100 15 | GC pause for startup: 3.591222ms 16 | GC pause for bigcache: 273.027743ms 17 | # go run caches_gc_overhead_comparison.go -cache stdmap 18 | Cache: stdmap 19 | Number of entries: 20000000 20 | Number of repeats: 50 21 | Value size: 100 22 | GC pause for startup: 2.893986ms 23 | GC pause for stdmap: 111.621318ms 24 | 25 | ``` 26 | 27 | ### Writes and reads 28 | ``` 29 | # go version 30 | go version go1.20.2 linux/amd64 31 | # go test -bench=. -benchmem -benchtime=4s ./... -timeout 30m 32 | goos: linux 33 | goarch: amd64 34 | pkg: github.com/allegro/bigcache-bench 35 | cpu: Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz 36 | BenchmarkMapSetForStruct-8 2523 1887529 ns/op 696961 B/op 19746 allocs/op 37 | BenchmarkSyncMapSetForStruct-8 934 4957356 ns/op 1851193 B/op 69774 allocs/op 38 | BenchmarkOracamanMapSetForStruct-8 1513 2870550 ns/op 1226702 B/op 20195 allocs/op 39 | BenchmarkFreeCacheSetForStruct-8 688 6652622 ns/op 6983119 B/op 40287 allocs/op 40 | BenchmarkBigCacheSetForStruct-8 760 5973545 ns/op 3772362 B/op 42204 allocs/op 41 | BenchmarkMapSetForBytes-8 1708 2826723 ns/op 2096269 B/op 29749 allocs/op 42 | BenchmarkSyncMapSetForBytes-8 814 5821909 ns/op 3133981 B/op 80034 allocs/op 43 | BenchmarkOracamanMapSetForBytes-8 1372 3579288 ns/op 2976597 B/op 30276 allocs/op 44 | BenchmarkFreeCacheSetForBytes-8 852 5575751 ns/op 7864114 B/op 30538 allocs/op 45 | BenchmarkBigCacheSetForBytes-8 906 5206221 ns/op 4653427 B/op 32457 allocs/op 46 | BenchmarkMapGetForStruct-8 23807222 201.1 ns/op 23 B/op 1 allocs/op 47 | BenchmarkSyncMapGetForStruct-8 17040469 238.3 ns/op 23 B/op 1 allocs/op 48 | BenchmarkOracamanMapGetForStruct-8 18912418 238.2 ns/op 23 B/op 1 allocs/op 49 | BenchmarkFreeCacheGetForStruct-8 5201450 935.8 ns/op 295 B/op 9 allocs/op 50 | BenchmarkBigCacheGetForStruct-8 5397729 875.0 ns/op 287 B/op 9 allocs/op 51 | BenchmarkMapGetForBytes-8 23130621 207.2 ns/op 23 B/op 1 allocs/op 52 | BenchmarkSyncMapGetForBytes-8 18846595 237.4 ns/op 23 B/op 1 allocs/op 53 | BenchmarkOracamanMapGetForBytes-8 19344432 246.4 ns/op 23 B/op 1 allocs/op 54 | BenchmarkFreeCacheGetForBytes-8 11845938 395.3 ns/op 159 B/op 3 allocs/op 55 | BenchmarkBigCacheGetForBytes-8 12868870 346.8 ns/op 151 B/op 3 allocs/op 56 | BenchmarkSyncMapSetParallelForStruct-8 6717847 694.8 ns/op 70 B/op 5 allocs/op 57 | BenchmarkOracamanMapSetParallelForStruct-8 21306747 218.4 ns/op 31 B/op 2 allocs/op 58 | BenchmarkFreeCacheSetParallelForStruct-8 18417992 266.4 ns/op 54 B/op 4 allocs/op 59 | BenchmarkBigCacheSetParallelForStruct-8 17324250 290.5 ns/op 203 B/op 4 allocs/op 60 | BenchmarkSyncMapSetParallelForBytes-8 6361482 773.0 ns/op 199 B/op 6 allocs/op 61 | BenchmarkOracamanMapSetParallelForBytes-8 20362628 234.1 ns/op 139 B/op 3 allocs/op 62 | BenchmarkFreeCacheSetParallelForBytes-8 19983202 237.1 ns/op 143 B/op 3 allocs/op 63 | BenchmarkBigCacheSetParallelForBytes-8 17371614 280.4 ns/op 443 B/op 3 allocs/op 64 | BenchmarkSyncMapGetParallelForStruct-8 24168936 193.5 ns/op 23 B/op 1 allocs/op 65 | BenchmarkOracamanMapGetParallelForStruct-8 21529862 186.5 ns/op 23 B/op 1 allocs/op 66 | BenchmarkFreeCacheGetParallelForStruct-8 14119288 350.2 ns/op 295 B/op 9 allocs/op 67 | BenchmarkBigCacheGetParallelForStruct-8 13292924 368.0 ns/op 287 B/op 9 allocs/op 68 | BenchmarkSyncMapGetParallelForBytes-8 25061154 188.6 ns/op 23 B/op 1 allocs/op 69 | BenchmarkOracamanMapGetParallelForBytes-8 25262446 190.0 ns/op 23 B/op 1 allocs/op 70 | BenchmarkFreeCacheGetParallelForBytes-8 20815525 232.5 ns/op 159 B/op 3 allocs/op 71 | BenchmarkBigCacheGetParallelForBytes-8 21059220 220.7 ns/op 151 B/op 3 allocs/op 72 | PASS 73 | ok github.com/allegro/bigcache-bench 183.467s 74 | ``` 75 | -------------------------------------------------------------------------------- /caches_bench_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "math/rand" 7 | "sync" 8 | "testing" 9 | "time" 10 | 11 | "github.com/allegro/bigcache/v3" 12 | "github.com/coocood/freecache" 13 | cmap "github.com/orcaman/concurrent-map/v2" 14 | ) 15 | 16 | const maxEntrySize = 256 17 | const maxEntryCount = 10000 18 | 19 | type myStruct struct { 20 | Id int `json:"id"` 21 | } 22 | 23 | type constructor[T any] interface { 24 | Get(int) T 25 | Parse([]byte) (T, error) 26 | ToBytes(T) ([]byte, error) 27 | } 28 | 29 | type byteConstructor []byte 30 | 31 | func (bc byteConstructor) Get(n int) []byte { 32 | return value() 33 | } 34 | 35 | func (bc byteConstructor) Parse(data []byte) ([]byte, error) { 36 | return data, nil 37 | } 38 | 39 | func (bc byteConstructor) ToBytes(v []byte) ([]byte, error) { 40 | return v, nil 41 | } 42 | 43 | type structConstructor struct { 44 | } 45 | 46 | func (sc structConstructor) Get(n int) myStruct { 47 | return myStruct{Id: n} 48 | } 49 | 50 | func (sc structConstructor) Parse(data []byte) (myStruct, error) { 51 | var s myStruct 52 | err := json.Unmarshal(data, &s) 53 | return s, err 54 | } 55 | 56 | func (sc structConstructor) ToBytes(v myStruct) ([]byte, error) { 57 | return json.Marshal(v) 58 | } 59 | 60 | func MapSet[T any](cs constructor[T], b *testing.B) { 61 | for i := 0; i < b.N; i++ { 62 | m := make(map[string]T, maxEntryCount) 63 | for n := 0; n < maxEntryCount; n++ { 64 | m[key(n)] = cs.Get(n) 65 | } 66 | } 67 | } 68 | 69 | func SyncMapSet[T any](cs constructor[T], b *testing.B) { 70 | for i := 0; i < b.N; i++ { 71 | var m sync.Map 72 | for n := 0; n < maxEntryCount; n++ { 73 | m.Store(key(n), cs.Get(n)) 74 | } 75 | } 76 | } 77 | 78 | func OracamanMapSet[T any](cs constructor[T], b *testing.B) { 79 | for i := 0; i < b.N; i++ { 80 | m := cmap.New[T]() 81 | for n := 0; n < maxEntryCount; n++ { 82 | m.Set(key(n), cs.Get(n)) 83 | } 84 | } 85 | } 86 | 87 | func FreeCacheSet[T any](cs constructor[T], b *testing.B) { 88 | for i := 0; i < b.N; i++ { 89 | cache := freecache.NewCache(maxEntryCount * maxEntrySize) 90 | for n := 0; n < maxEntryCount; n++ { 91 | data, _ := cs.ToBytes(cs.Get(n)) 92 | cache.Set([]byte(key(n)), data, 0) 93 | } 94 | } 95 | } 96 | 97 | func BigCacheSet[T any](cs constructor[T], b *testing.B) { 98 | for i := 0; i < b.N; i++ { 99 | cache := initBigCache(maxEntryCount) 100 | for n := 0; n < maxEntryCount; n++ { 101 | data, _ := cs.ToBytes(cs.Get(n)) 102 | cache.Set(key(n), data) 103 | } 104 | } 105 | } 106 | 107 | func BenchmarkMapSetForStruct(b *testing.B) { 108 | MapSet[myStruct](structConstructor{}, b) 109 | } 110 | 111 | func BenchmarkSyncMapSetForStruct(b *testing.B) { 112 | SyncMapSet[myStruct](structConstructor{}, b) 113 | } 114 | 115 | func BenchmarkOracamanMapSetForStruct(b *testing.B) { 116 | OracamanMapSet[myStruct](structConstructor{}, b) 117 | } 118 | 119 | func BenchmarkFreeCacheSetForStruct(b *testing.B) { 120 | FreeCacheSet[myStruct](structConstructor{}, b) 121 | } 122 | 123 | func BenchmarkBigCacheSetForStruct(b *testing.B) { 124 | BigCacheSet[myStruct](structConstructor{}, b) 125 | } 126 | 127 | func BenchmarkMapSetForBytes(b *testing.B) { 128 | MapSet[[]byte](byteConstructor{}, b) 129 | } 130 | 131 | func BenchmarkSyncMapSetForBytes(b *testing.B) { 132 | SyncMapSet[[]byte](byteConstructor{}, b) 133 | } 134 | 135 | func BenchmarkOracamanMapSetForBytes(b *testing.B) { 136 | OracamanMapSet[[]byte](byteConstructor{}, b) 137 | } 138 | 139 | func BenchmarkFreeCacheSetForBytes(b *testing.B) { 140 | FreeCacheSet[[]byte](byteConstructor{}, b) 141 | } 142 | 143 | func BenchmarkBigCacheSetForBytes(b *testing.B) { 144 | BigCacheSet[[]byte](byteConstructor{}, b) 145 | } 146 | 147 | func MapGet[T any](cs constructor[T], b *testing.B) { 148 | b.StopTimer() 149 | m := make(map[string]T) 150 | for n := 0; n < maxEntryCount; n++ { 151 | m[key(n)] = cs.Get(n) 152 | } 153 | b.StartTimer() 154 | 155 | hitCount := 0 156 | for i := 0; i < b.N; i++ { 157 | id := rand.Intn(maxEntryCount) 158 | if e, ok := m[key(id)]; ok { 159 | _ = (T)(e) 160 | hitCount++ 161 | } 162 | } 163 | } 164 | 165 | func SyncMapGet[T any](cs constructor[T], b *testing.B) { 166 | b.StopTimer() 167 | var m sync.Map 168 | for n := 0; n < maxEntryCount; n++ { 169 | m.Store(key(n), cs.Get(n)) 170 | } 171 | b.StartTimer() 172 | 173 | hitCounter := 0 174 | for i := 0; i < b.N; i++ { 175 | id := rand.Intn(maxEntryCount) 176 | e, ok := m.Load(key(id)) 177 | if ok { 178 | _ = (T)(e.(T)) 179 | hitCounter++ 180 | } 181 | } 182 | } 183 | 184 | func OracamanMapGet[T any](cs constructor[T], b *testing.B) { 185 | b.StopTimer() 186 | m := cmap.New[T]() 187 | for n := 0; n < maxEntryCount; n++ { 188 | m.Set(key(n), cs.Get(n)) 189 | } 190 | b.StartTimer() 191 | 192 | hitCounter := 0 193 | for i := 0; i < b.N; i++ { 194 | id := rand.Intn(maxEntryCount) 195 | e, ok := m.Get(key(id)) 196 | if ok { 197 | _ = (T)(e) 198 | hitCounter++ 199 | } 200 | } 201 | } 202 | 203 | func FreeCacheGet[T any](cs constructor[T], b *testing.B) { 204 | b.StopTimer() 205 | cache := freecache.NewCache(maxEntryCount * maxEntrySize) 206 | for n := 0; n < maxEntryCount; n++ { 207 | data, _ := cs.ToBytes(cs.Get(n)) 208 | cache.Set([]byte(key(n)), data, 0) 209 | } 210 | b.StartTimer() 211 | 212 | hitCounter := 0 213 | for i := 0; i < b.N; i++ { 214 | id := rand.Intn(maxEntryCount) 215 | data, _ := cache.Get([]byte(key(id))) 216 | v, _ := cs.Parse(data) 217 | _ = (T)(v) 218 | hitCounter++ 219 | } 220 | } 221 | 222 | func BigCacheGet[T any](cs constructor[T], b *testing.B) { 223 | b.StopTimer() 224 | cache := initBigCache(maxEntryCount) 225 | for n := 0; n < maxEntryCount; n++ { 226 | data, _ := cs.ToBytes(cs.Get(n)) 227 | cache.Set(key(n), data) 228 | } 229 | b.StartTimer() 230 | 231 | hitCount := 0 232 | for i := 0; i < b.N; i++ { 233 | id := rand.Intn(maxEntryCount) 234 | data, _ := cache.Get(key(id)) 235 | v, _ := cs.Parse(data) 236 | _ = (T)(v) 237 | hitCount++ 238 | } 239 | } 240 | 241 | func BenchmarkMapGetForStruct(b *testing.B) { 242 | MapGet[myStruct](structConstructor{}, b) 243 | } 244 | 245 | func BenchmarkSyncMapGetForStruct(b *testing.B) { 246 | SyncMapGet[myStruct](structConstructor{}, b) 247 | } 248 | 249 | func BenchmarkOracamanMapGetForStruct(b *testing.B) { 250 | OracamanMapGet[myStruct](structConstructor{}, b) 251 | } 252 | 253 | func BenchmarkFreeCacheGetForStruct(b *testing.B) { 254 | FreeCacheGet[myStruct](structConstructor{}, b) 255 | } 256 | 257 | func BenchmarkBigCacheGetForStruct(b *testing.B) { 258 | BigCacheGet[myStruct](structConstructor{}, b) 259 | } 260 | 261 | func BenchmarkMapGetForBytes(b *testing.B) { 262 | MapGet[[]byte](byteConstructor{}, b) 263 | } 264 | 265 | func BenchmarkSyncMapGetForBytes(b *testing.B) { 266 | SyncMapGet[[]byte](byteConstructor{}, b) 267 | } 268 | 269 | func BenchmarkOracamanMapGetForBytes(b *testing.B) { 270 | OracamanMapGet[[]byte](byteConstructor{}, b) 271 | } 272 | 273 | func BenchmarkFreeCacheGetForBytes(b *testing.B) { 274 | FreeCacheGet[[]byte](byteConstructor{}, b) 275 | } 276 | 277 | func BenchmarkBigCacheGetForBytes(b *testing.B) { 278 | BigCacheGet[[]byte](byteConstructor{}, b) 279 | } 280 | 281 | func SyncMapSetParallel[T any](cs constructor[T], b *testing.B) { 282 | var m sync.Map 283 | b.RunParallel(func(pb *testing.PB) { 284 | thread := rand.Intn(1000) 285 | for pb.Next() { 286 | id := rand.Intn(maxEntryCount) 287 | m.Store(parallelKey(thread, id), cs.Get(id)) 288 | } 289 | }) 290 | } 291 | 292 | func OracamanMapSetParallel[T any](cs constructor[T], b *testing.B) { 293 | m := cmap.New[T]() 294 | 295 | b.RunParallel(func(pb *testing.PB) { 296 | thread := rand.Intn(1000) 297 | for pb.Next() { 298 | id := rand.Intn(maxEntryCount) 299 | m.Set(parallelKey(thread, id), cs.Get(id)) 300 | } 301 | }) 302 | } 303 | 304 | func FreeCacheSetParallel[T any](cs constructor[T], b *testing.B) { 305 | cache := freecache.NewCache(maxEntryCount * maxEntrySize) 306 | 307 | b.RunParallel(func(pb *testing.PB) { 308 | thread := rand.Intn(1000) 309 | for pb.Next() { 310 | id := rand.Intn(maxEntryCount) 311 | data, _ := cs.ToBytes(cs.Get(id)) 312 | cache.Set([]byte(parallelKey(thread, id)), data, 0) 313 | } 314 | }) 315 | } 316 | 317 | func BigCacheSetParallel[T any](cs constructor[T], b *testing.B) { 318 | cache := initBigCache(maxEntryCount) 319 | 320 | b.RunParallel(func(pb *testing.PB) { 321 | thread := rand.Intn(1000) 322 | for pb.Next() { 323 | id := rand.Intn(maxEntryCount) 324 | data, _ := cs.ToBytes(cs.Get(id)) 325 | cache.Set(parallelKey(thread, id), data) 326 | } 327 | }) 328 | } 329 | 330 | func BenchmarkSyncMapSetParallelForStruct(b *testing.B) { 331 | SyncMapSetParallel[myStruct](structConstructor{}, b) 332 | } 333 | 334 | func BenchmarkOracamanMapSetParallelForStruct(b *testing.B) { 335 | OracamanMapSetParallel[myStruct](structConstructor{}, b) 336 | } 337 | 338 | func BenchmarkFreeCacheSetParallelForStruct(b *testing.B) { 339 | FreeCacheSetParallel[myStruct](structConstructor{}, b) 340 | } 341 | 342 | func BenchmarkBigCacheSetParallelForStruct(b *testing.B) { 343 | BigCacheSetParallel[myStruct](structConstructor{}, b) 344 | } 345 | 346 | func BenchmarkSyncMapSetParallelForBytes(b *testing.B) { 347 | SyncMapSetParallel[[]byte](byteConstructor{}, b) 348 | } 349 | 350 | func BenchmarkOracamanMapSetParallelForBytes(b *testing.B) { 351 | OracamanMapSetParallel[[]byte](byteConstructor{}, b) 352 | } 353 | 354 | func BenchmarkFreeCacheSetParallelForBytes(b *testing.B) { 355 | FreeCacheSetParallel[[]byte](byteConstructor{}, b) 356 | } 357 | 358 | func BenchmarkBigCacheSetParallelForBytes(b *testing.B) { 359 | BigCacheSetParallel[[]byte](byteConstructor{}, b) 360 | } 361 | 362 | func SyncMapGetParallel[T any](cs constructor[T], b *testing.B) { 363 | b.StopTimer() 364 | var m sync.Map 365 | for i := 0; i < maxEntryCount; i++ { 366 | m.Store(key(i), cs.Get(i)) 367 | } 368 | b.StartTimer() 369 | 370 | b.RunParallel(func(pb *testing.PB) { 371 | for pb.Next() { 372 | id := rand.Intn(maxEntryCount) 373 | e, ok := m.Load(key(id)) 374 | if ok { 375 | _ = (T)(e.(T)) 376 | } 377 | } 378 | }) 379 | } 380 | 381 | func OracamanMapGetParallel[T any](cs constructor[T], b *testing.B) { 382 | b.StopTimer() 383 | m := cmap.New[T]() 384 | for i := 0; i < maxEntryCount; i++ { 385 | m.Set(key(i), cs.Get(i)) 386 | } 387 | b.StartTimer() 388 | 389 | b.RunParallel(func(pb *testing.PB) { 390 | for pb.Next() { 391 | id := rand.Intn(maxEntryCount) 392 | e, _ := m.Get(key(id)) 393 | _ = (T)(e) 394 | } 395 | }) 396 | } 397 | 398 | func FreeCacheGetParallel[T any](cs constructor[T], b *testing.B) { 399 | b.StopTimer() 400 | cache := freecache.NewCache(maxEntryCount * maxEntrySize) 401 | for i := 0; i < maxEntryCount; i++ { 402 | data, _ := cs.ToBytes(cs.Get(i)) 403 | cache.Set([]byte(key(i)), data, 0) 404 | } 405 | b.StartTimer() 406 | 407 | b.RunParallel(func(pb *testing.PB) { 408 | for pb.Next() { 409 | id := rand.Intn(maxEntryCount) 410 | data, _ := cache.Get([]byte(key(id))) 411 | v, _ := cs.Parse(data) 412 | _ = (T)(v) 413 | } 414 | }) 415 | } 416 | 417 | func BigCacheGetParallel[T any](cs constructor[T], b *testing.B) { 418 | b.StopTimer() 419 | cache := initBigCache(maxEntryCount) 420 | for i := 0; i < maxEntryCount; i++ { 421 | data, _ := cs.ToBytes(cs.Get(i)) 422 | cache.Set(key(i), data) 423 | } 424 | b.StartTimer() 425 | 426 | b.RunParallel(func(pb *testing.PB) { 427 | for pb.Next() { 428 | id := rand.Intn(maxEntryCount) 429 | data, _ := cache.Get(key(id)) 430 | v, _ := cs.Parse(data) 431 | _ = (T)(v) 432 | } 433 | }) 434 | } 435 | 436 | func BenchmarkSyncMapGetParallelForStruct(b *testing.B) { 437 | SyncMapGetParallel[myStruct](structConstructor{}, b) 438 | } 439 | 440 | func BenchmarkOracamanMapGetParallelForStruct(b *testing.B) { 441 | OracamanMapGetParallel[myStruct](structConstructor{}, b) 442 | } 443 | 444 | func BenchmarkFreeCacheGetParallelForStruct(b *testing.B) { 445 | FreeCacheGetParallel[myStruct](structConstructor{}, b) 446 | } 447 | 448 | func BenchmarkBigCacheGetParallelForStruct(b *testing.B) { 449 | BigCacheGetParallel[myStruct](structConstructor{}, b) 450 | } 451 | 452 | func BenchmarkSyncMapGetParallelForBytes(b *testing.B) { 453 | SyncMapGetParallel[[]byte](byteConstructor{}, b) 454 | } 455 | 456 | func BenchmarkOracamanMapGetParallelForBytes(b *testing.B) { 457 | OracamanMapGetParallel[[]byte](byteConstructor{}, b) 458 | } 459 | 460 | func BenchmarkFreeCacheGetParallelForBytes(b *testing.B) { 461 | FreeCacheGetParallel[[]byte](byteConstructor{}, b) 462 | } 463 | 464 | func BenchmarkBigCacheGetParallelForBytes(b *testing.B) { 465 | BigCacheGetParallel[[]byte](byteConstructor{}, b) 466 | } 467 | 468 | func key(i int) string { 469 | return fmt.Sprintf("key-%010d", i) 470 | } 471 | 472 | func value() []byte { 473 | return make([]byte, 100) 474 | } 475 | 476 | func parallelKey(threadID int, counter int) string { 477 | return fmt.Sprintf("key-%04d-%06d", threadID, counter) 478 | } 479 | 480 | func initBigCache(entriesInWindow int) *bigcache.BigCache { 481 | cache, _ := bigcache.NewBigCache(bigcache.Config{ 482 | Shards: 256, 483 | LifeWindow: 10 * time.Minute, 484 | MaxEntriesInWindow: entriesInWindow, 485 | MaxEntrySize: maxEntrySize, 486 | Verbose: false, 487 | }) 488 | 489 | return cache 490 | } 491 | -------------------------------------------------------------------------------- /caches_gc_overhead_comparison.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "os" 7 | "runtime" 8 | "runtime/debug" 9 | "time" 10 | 11 | "github.com/allegro/bigcache/v3" 12 | "github.com/coocood/freecache" 13 | ) 14 | 15 | var previousPause time.Duration 16 | 17 | func gcPause() time.Duration { 18 | runtime.GC() 19 | var stats debug.GCStats 20 | debug.ReadGCStats(&stats) 21 | pause := stats.PauseTotal - previousPause 22 | previousPause = stats.PauseTotal 23 | return pause 24 | } 25 | 26 | func main() { 27 | 28 | c := "" 29 | entries := 0 30 | repeat := 0 31 | valueSize := 0 32 | flag.StringVar(&c, "cache", "bigcache", "cache to bench.") 33 | flag.IntVar(&entries, "entries", 20000000, "number of entries to test") 34 | flag.IntVar(&repeat, "repeat", 50, "number of repetitions") 35 | flag.IntVar(&valueSize, "value-size", 100, "size of single entry value in bytes") 36 | flag.Parse() 37 | 38 | debug.SetGCPercent(10) 39 | fmt.Println("Cache: ", c) 40 | fmt.Println("Number of entries: ", entries) 41 | fmt.Println("Number of repeats: ", repeat) 42 | fmt.Println("Value size: ", valueSize) 43 | 44 | var benchFunc func(entries, valueSize int) 45 | 46 | switch c { 47 | case "freecache": 48 | benchFunc = freeCache 49 | case "bigcache": 50 | benchFunc = bigCache 51 | case "stdmap": 52 | benchFunc = stdMap 53 | default: 54 | fmt.Printf("unknown cache: %s", c) 55 | os.Exit(1) 56 | } 57 | 58 | benchFunc(entries, valueSize) 59 | fmt.Println("GC pause for startup: ", gcPause()) 60 | for i := 0; i < repeat; i++ { 61 | benchFunc(entries, valueSize) 62 | } 63 | 64 | fmt.Printf("GC pause for %s: %s\n", c, gcPause()) 65 | } 66 | 67 | func stdMap(entries, valueSize int) { 68 | mapCache := make(map[string][]byte) 69 | for i := 0; i < entries; i++ { 70 | key, val := generateKeyValue(i, valueSize) 71 | mapCache[key] = val 72 | } 73 | } 74 | 75 | func freeCache(entries, valueSize int) { 76 | freeCache := freecache.NewCache(entries * 200) //allocate entries * 200 bytes 77 | for i := 0; i < entries; i++ { 78 | key, val := generateKeyValue(i, valueSize) 79 | if err := freeCache.Set([]byte(key), val, 0); err != nil { 80 | fmt.Println("Error in set: ", err.Error()) 81 | } 82 | } 83 | 84 | firstKey, _ := generateKeyValue(1, valueSize) 85 | v, err := freeCache.Get([]byte(firstKey)) 86 | checkFirstElement(valueSize, v, err) 87 | 88 | if freeCache.OverwriteCount() != 0 { 89 | fmt.Println("Overwritten: ", freeCache.OverwriteCount()) 90 | } 91 | } 92 | 93 | func bigCache(entries, valueSize int) { 94 | config := bigcache.Config{ 95 | Shards: 256, 96 | LifeWindow: 100 * time.Minute, 97 | MaxEntriesInWindow: entries, 98 | MaxEntrySize: 200, 99 | Verbose: true, 100 | } 101 | 102 | bigcache, _ := bigcache.NewBigCache(config) 103 | for i := 0; i < entries; i++ { 104 | key, val := generateKeyValue(i, valueSize) 105 | bigcache.Set(key, val) 106 | } 107 | 108 | firstKey, _ := generateKeyValue(1, valueSize) 109 | v, err := bigcache.Get(firstKey) 110 | checkFirstElement(valueSize, v, err) 111 | } 112 | 113 | func checkFirstElement(valueSize int, val []byte, err error) { 114 | _, expectedVal := generateKeyValue(1, valueSize) 115 | if err != nil { 116 | fmt.Println("Error in get: ", err.Error()) 117 | } else if string(val) != string(expectedVal) { 118 | fmt.Println("Wrong first element: ", string(val)) 119 | } 120 | } 121 | 122 | func generateKeyValue(index int, valSize int) (string, []byte) { 123 | key := fmt.Sprintf("key-%010d", index) 124 | fixedNumber := []byte(fmt.Sprintf("%010d", index)) 125 | val := append(make([]byte, valSize-10), fixedNumber...) 126 | 127 | return key, val 128 | } 129 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/allegro/bigcache-bench 2 | 3 | go 1.19 4 | 5 | require ( 6 | github.com/allegro/bigcache/v3 v3.1.0 7 | github.com/coocood/freecache v1.2.4 8 | github.com/orcaman/concurrent-map/v2 v2.0.1 9 | ) 10 | 11 | require github.com/cespare/xxhash/v2 v2.2.0 // indirect 12 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/allegro/bigcache/v3 v3.1.0 h1:H2Vp8VOvxcrB91o86fUSVJFqeuz8kpyyB02eH3bSzwk= 2 | github.com/allegro/bigcache/v3 v3.1.0/go.mod h1:aPyh7jEvrog9zAwx5N7+JUQX5dZTSGpxF1LAR4dr35I= 3 | github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= 4 | github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= 5 | github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= 6 | github.com/coocood/freecache v1.2.4 h1:UdR6Yz/X1HW4fZOuH0Z94KwG851GWOSknua5VUbb/5M= 7 | github.com/coocood/freecache v1.2.4/go.mod h1:RBUWa/Cy+OHdfTGFEhEuE1pMCMX51Ncizj7rthiQ3vk= 8 | github.com/orcaman/concurrent-map/v2 v2.0.1 h1:jOJ5Pg2w1oeB6PeDurIYf6k9PQ+aTITr/6lP/L/zp6c= 9 | github.com/orcaman/concurrent-map/v2 v2.0.1/go.mod h1:9Eq3TG2oBe5FirmYWQfYO5iH1q0Jv47PLaNK++uCdOM= 10 | --------------------------------------------------------------------------------