From 13b7e704aaa3eff97216c25c5a9a546b5ab176f7 Mon Sep 17 00:00:00 2001 From: parMaster Date: Thu, 13 Jul 2023 18:08:37 +0300 Subject: [PATCH] benchmarks --- README.md | 16 ++++++++++++++++ bench_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 bench_test.go diff --git a/README.md b/README.md index 85f47f7..9460f5b 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,22 @@ go func() { See the [examples](https://github.com/parMaster/mcache/tree/main/examples) directory for more examples. +## Benchmarks +```shell +Running tool: /usr/local/go/bin/go test -benchmem -run=^$ -coverprofile=/tmp/vscode-gopk5hiw/go-code-cover -bench . github.com/parMaster/mcache + +goos: linux +goarch: amd64 +pkg: github.com/parMaster/mcache +cpu: Intel(R) Core(TM) i5-4308U CPU @ 2.80GHz +BenchmarkWrite-4 1797061 815.2 ns/op 247 B/op 3 allocs/op +BenchmarkRead-4 4516329 260.7 ns/op 48 B/op 3 allocs/op +BenchmarkRWD-4 1761019 686.6 ns/op 56 B/op 6 allocs/op +PASS +github.com/parMaster/mcache coverage: 83.0% of statements +ok github.com/parMaster/mcache 7.190s +``` + ## Contributing Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request. diff --git a/bench_test.go b/bench_test.go new file mode 100644 index 0000000..56a7afb --- /dev/null +++ b/bench_test.go @@ -0,0 +1,38 @@ +package mcache + +import ( + "fmt" + "testing" +) + +var mcache *Cache + +// BenchmarkWrite +func BenchmarkWrite(b *testing.B) { + mcache = NewCache() + + b.ResetTimer() + for i := 0; i < b.N; i++ { + mcache.Set(fmt.Sprintf("%d", i), i, int64(i)) + } + b.StopTimer() + mcache.Cleanup() +} + +// BenchmarkRead +func BenchmarkRead(b *testing.B) { + for i := 0; i < b.N; i++ { + mcache.Get(fmt.Sprintf("%d", i)) + } + mcache.Clear() +} + +// BenchmarkRW +func BenchmarkRWD(b *testing.B) { + for i := 0; i < b.N; i++ { + mcache.Set(fmt.Sprintf("%d", i), i, int64(i)) + mcache.Get(fmt.Sprintf("%d", i)) + mcache.Del(fmt.Sprintf("%d", i)) + } + mcache.Clear() +}