From 2bf9bc98a79a9cb5d97916e946fa6df515be5a5c Mon Sep 17 00:00:00 2001 From: John Date: Mon, 26 Aug 2019 23:35:44 +0800 Subject: [PATCH] improve gkvdb, container --- container/garray/garray_z_bench_test.go | 44 ------------ container/glist/glist_z_bench_test.go | 2 +- container/gmap/gmap_z_bench_maps_test.go | 6 +- container/gmap/gmap_z_bench_safe_test.go | 14 ++-- container/gmap/gmap_z_bench_syncmap_test.go | 2 +- container/gmap/gmap_z_bench_unsafe_test.go | 42 ++++++------ container/gring/gring_bench_test.go | 68 +++++++++++++++++++ container/gring/gring_test.go | 46 ------------- container/gset/gset_z_bench_test.go | 12 ++-- .../gtype/{gtype_test.go => z_bench_test.go} | 0 .../{gtype_z_unit_test.go => z_unit_test.go} | 6 ++ .../{gvar_test.go => gvar_z_unit_test.go} | 0 database/gkvdb/gkvdb_unit_test.go | 12 ++++ net/ghttp/ghttp_server_cookie.go | 2 +- net/ghttp/ghttp_server_session.go | 9 ++- 15 files changed, 133 insertions(+), 132 deletions(-) delete mode 100644 container/garray/garray_z_bench_test.go create mode 100644 container/gring/gring_bench_test.go delete mode 100644 container/gring/gring_test.go rename container/gtype/{gtype_test.go => z_bench_test.go} (100%) rename container/gtype/{gtype_z_unit_test.go => z_unit_test.go} (95%) rename container/gvar/{gvar_test.go => gvar_z_unit_test.go} (100%) diff --git a/container/garray/garray_z_bench_test.go b/container/garray/garray_z_bench_test.go deleted file mode 100644 index ab9d676ba..000000000 --- a/container/garray/garray_z_bench_test.go +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved. -// -// This Source Code Form is subject to the terms of the MIT License. -// If a copy of the MIT was not distributed with this file, -// You can obtain one at https://github.com/gogf/gf. - -// go test *.go -bench=".*" -benchmem - -package garray_test - -import ( - "testing" - - "github.com/gogf/gf/container/garray" -) - -var ( - sortedIntArray = garray.NewSortedIntArray() -) - -func BenchmarkSortedIntArray_Add(b *testing.B) { - b.N = 1000 - for i := 0; i < b.N; i++ { - sortedIntArray.Add(i) - } -} - -func BenchmarkSortedIntArray_Search(b *testing.B) { - for i := 0; i < b.N; i++ { - sortedIntArray.Search(i) - } -} - -func BenchmarkSortedIntArray_PopLeft(b *testing.B) { - for i := 0; i < b.N; i++ { - sortedIntArray.PopLeft() - } -} - -func BenchmarkSortedIntArray_PopRight(b *testing.B) { - for i := 0; i < b.N; i++ { - sortedIntArray.PopLeft() - } -} diff --git a/container/glist/glist_z_bench_test.go b/container/glist/glist_z_bench_test.go index 4ddd6bea9..48889e11e 100644 --- a/container/glist/glist_z_bench_test.go +++ b/container/glist/glist_z_bench_test.go @@ -13,7 +13,7 @@ import ( ) var ( - l = New() + l = New(true) ) func Benchmark_PushBack(b *testing.B) { diff --git a/container/gmap/gmap_z_bench_maps_test.go b/container/gmap/gmap_z_bench_maps_test.go index a0d4e9f50..54dc6968c 100644 --- a/container/gmap/gmap_z_bench_maps_test.go +++ b/container/gmap/gmap_z_bench_maps_test.go @@ -15,9 +15,9 @@ import ( "github.com/gogf/gf/util/gutil" ) -var hashMap = gmap.New() -var listMap = gmap.NewListMap() -var treeMap = gmap.NewTreeMap(gutil.ComparatorInt) +var hashMap = gmap.New(true) +var listMap = gmap.NewListMap(true) +var treeMap = gmap.NewTreeMap(gutil.ComparatorInt, true) func Benchmark_HashMap_Set(b *testing.B) { b.RunParallel(func(pb *testing.PB) { diff --git a/container/gmap/gmap_z_bench_safe_test.go b/container/gmap/gmap_z_bench_safe_test.go index 6f1a334ed..2e84d15ca 100644 --- a/container/gmap/gmap_z_bench_safe_test.go +++ b/container/gmap/gmap_z_bench_safe_test.go @@ -15,13 +15,13 @@ import ( "github.com/gogf/gf/container/gmap" ) -var anyAnyMap = gmap.NewAnyAnyMap() -var intIntMap = gmap.NewIntIntMap() -var intAnyMap = gmap.NewIntAnyMap() -var intStrMap = gmap.NewIntStrMap() -var strIntMap = gmap.NewStrIntMap() -var strAnyMap = gmap.NewStrAnyMap() -var strStrMap = gmap.NewStrStrMap() +var anyAnyMap = gmap.NewAnyAnyMap(true) +var intIntMap = gmap.NewIntIntMap(true) +var intAnyMap = gmap.NewIntAnyMap(true) +var intStrMap = gmap.NewIntStrMap(true) +var strIntMap = gmap.NewStrIntMap(true) +var strAnyMap = gmap.NewStrAnyMap(true) +var strStrMap = gmap.NewStrStrMap(true) func Benchmark_IntIntMap_Set(b *testing.B) { b.RunParallel(func(pb *testing.PB) { diff --git a/container/gmap/gmap_z_bench_syncmap_test.go b/container/gmap/gmap_z_bench_syncmap_test.go index def6e7dd5..64c252299 100644 --- a/container/gmap/gmap_z_bench_syncmap_test.go +++ b/container/gmap/gmap_z_bench_syncmap_test.go @@ -15,7 +15,7 @@ import ( "github.com/gogf/gf/container/gmap" ) -var gm = gmap.NewIntIntMap() +var gm = gmap.NewIntIntMap(true) var sm = sync.Map{} func Benchmark_GMapSet(b *testing.B) { diff --git a/container/gmap/gmap_z_bench_unsafe_test.go b/container/gmap/gmap_z_bench_unsafe_test.go index dbdd967f6..c63c88a8b 100644 --- a/container/gmap/gmap_z_bench_unsafe_test.go +++ b/container/gmap/gmap_z_bench_unsafe_test.go @@ -15,55 +15,55 @@ import ( "github.com/gogf/gf/container/gmap" ) -var ififmUnsafe = gmap.New(true) -var iimUnsafe = gmap.NewIntIntMap(true) -var iifmUnsafe = gmap.NewIntAnyMap(true) -var ismUnsafe = gmap.NewIntStrMap(true) -var simUnsafe = gmap.NewStrIntMap(true) -var sifmUnsafe = gmap.NewStrAnyMap(true) -var ssmUnsafe = gmap.NewStrStrMap(true) +var anyAnyMapUnsafe = gmap.New() +var intIntMapUnsafe = gmap.NewIntIntMap() +var intAnyMapUnsafe = gmap.NewIntAnyMap() +var intStrMapUnsafe = gmap.NewIntStrMap() +var strIntMapUnsafe = gmap.NewStrIntMap() +var strAnyMapUnsafe = gmap.NewStrAnyMap() +var strStrMapUnsafe = gmap.NewStrStrMap() // Writing benchmarks. func Benchmark_Unsafe_IntIntMap_Set(b *testing.B) { for i := 0; i < b.N; i++ { - iimUnsafe.Set(i, i) + intIntMapUnsafe.Set(i, i) } } func Benchmark_Unsafe_IntAnyMap_Set(b *testing.B) { for i := 0; i < b.N; i++ { - iifmUnsafe.Set(i, i) + intAnyMapUnsafe.Set(i, i) } } func Benchmark_Unsafe_IntStrMap_Set(b *testing.B) { for i := 0; i < b.N; i++ { - ismUnsafe.Set(i, strconv.Itoa(i)) + intStrMapUnsafe.Set(i, strconv.Itoa(i)) } } func Benchmark_Unsafe_AnyAnyMap_Set(b *testing.B) { for i := 0; i < b.N; i++ { - ififmUnsafe.Set(i, i) + anyAnyMapUnsafe.Set(i, i) } } func Benchmark_Unsafe_StrIntMap_Set(b *testing.B) { for i := 0; i < b.N; i++ { - simUnsafe.Set(strconv.Itoa(i), i) + strIntMapUnsafe.Set(strconv.Itoa(i), i) } } func Benchmark_Unsafe_StrAnyMap_Set(b *testing.B) { for i := 0; i < b.N; i++ { - sifmUnsafe.Set(strconv.Itoa(i), i) + strAnyMapUnsafe.Set(strconv.Itoa(i), i) } } func Benchmark_Unsafe_StrStrMap_Set(b *testing.B) { for i := 0; i < b.N; i++ { - ssmUnsafe.Set(strconv.Itoa(i), strconv.Itoa(i)) + strStrMapUnsafe.Set(strconv.Itoa(i), strconv.Itoa(i)) } } @@ -71,42 +71,42 @@ func Benchmark_Unsafe_StrStrMap_Set(b *testing.B) { func Benchmark_Unsafe_IntIntMap_Get(b *testing.B) { for i := 0; i < b.N; i++ { - iimUnsafe.Get(i) + intIntMapUnsafe.Get(i) } } func Benchmark_Unsafe_IntAnyMap_Get(b *testing.B) { for i := 0; i < b.N; i++ { - iifmUnsafe.Get(i) + intAnyMapUnsafe.Get(i) } } func Benchmark_Unsafe_IntStrMap_Get(b *testing.B) { for i := 0; i < b.N; i++ { - ismUnsafe.Get(i) + intStrMapUnsafe.Get(i) } } func Benchmark_Unsafe_AnyAnyMap_Get(b *testing.B) { for i := 0; i < b.N; i++ { - ififmUnsafe.Get(i) + anyAnyMapUnsafe.Get(i) } } func Benchmark_Unsafe_StrIntMap_Get(b *testing.B) { for i := 0; i < b.N; i++ { - simUnsafe.Get(strconv.Itoa(i)) + strIntMapUnsafe.Get(strconv.Itoa(i)) } } func Benchmark_Unsafe_StrAnyMap_Get(b *testing.B) { for i := 0; i < b.N; i++ { - sifmUnsafe.Get(strconv.Itoa(i)) + strAnyMapUnsafe.Get(strconv.Itoa(i)) } } func Benchmark_Unsafe_StrStrMap_Get(b *testing.B) { for i := 0; i < b.N; i++ { - ssmUnsafe.Get(strconv.Itoa(i)) + strStrMapUnsafe.Get(strconv.Itoa(i)) } } diff --git a/container/gring/gring_bench_test.go b/container/gring/gring_bench_test.go new file mode 100644 index 000000000..c712f4a34 --- /dev/null +++ b/container/gring/gring_bench_test.go @@ -0,0 +1,68 @@ +// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +// go test *.go -bench=".*" + +package gring_test + +import ( + "testing" + + "github.com/gogf/gf/container/gring" +) + +var length = 10000 +var ring = gring.New(length, true) + +func BenchmarkRing_Put(b *testing.B) { + b.RunParallel(func(pb *testing.PB) { + i := 0 + for pb.Next() { + ring.Put(i) + i++ + } + }) +} + +func BenchmarkRing_Next(b *testing.B) { + b.RunParallel(func(pb *testing.PB) { + i := 0 + for pb.Next() { + ring.Next() + i++ + } + }) +} + +func BenchmarkRing_Set(b *testing.B) { + b.RunParallel(func(pb *testing.PB) { + i := 0 + for pb.Next() { + ring.Set(i) + i++ + } + }) +} + +func BenchmarkRing_Len(b *testing.B) { + b.RunParallel(func(pb *testing.PB) { + i := 0 + for pb.Next() { + ring.Len() + i++ + } + }) +} + +func BenchmarkRing_Cap(b *testing.B) { + b.RunParallel(func(pb *testing.PB) { + i := 0 + for pb.Next() { + ring.Cap() + i++ + } + }) +} diff --git a/container/gring/gring_test.go b/container/gring/gring_test.go deleted file mode 100644 index b4da7b478..000000000 --- a/container/gring/gring_test.go +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved. -// -// This Source Code Form is subject to the terms of the MIT License. -// If a copy of the MIT was not distributed with this file, -// You can obtain one at https://github.com/gogf/gf. - -// go test *.go -bench=".*" - -package gring - -import ( - "testing" -) - -var length = 10000 -var r1 = New(length) - -func BenchmarkRing_Put(b *testing.B) { - for i := 0; i < b.N; i++ { - r1.Put(i) - } -} - -func BenchmarkRing_Next(b *testing.B) { - for i := 0; i < b.N; i++ { - r1.Next() - } -} - -func BenchmarkRing_Set(b *testing.B) { - for i := 0; i < b.N; i++ { - r1.Set(i) - } -} - -func BenchmarkRing_Len(b *testing.B) { - for i := 0; i < b.N; i++ { - r1.Len() - } -} - -func BenchmarkRing_Cap(b *testing.B) { - for i := 0; i < b.N; i++ { - r1.Cap() - } -} diff --git a/container/gset/gset_z_bench_test.go b/container/gset/gset_z_bench_test.go index 5d0bbc9ef..6976bcddc 100644 --- a/container/gset/gset_z_bench_test.go +++ b/container/gset/gset_z_bench_test.go @@ -15,12 +15,12 @@ import ( "github.com/gogf/gf/container/gset" ) -var intSet = gset.NewIntSet() -var anySet = gset.NewSet() -var strSet = gset.NewStringSet() -var intSetUnsafe = gset.NewIntSet(true) -var anySetUnsafe = gset.NewSet(true) -var strSetUnsafe = gset.NewStringSet(true) +var intSet = gset.NewIntSet(true) +var anySet = gset.NewSet(true) +var strSet = gset.NewStringSet(true) +var intSetUnsafe = gset.NewIntSet() +var anySetUnsafe = gset.NewSet() +var strSetUnsafe = gset.NewStringSet() func Benchmark_IntSet_Add(b *testing.B) { b.RunParallel(func(pb *testing.PB) { diff --git a/container/gtype/gtype_test.go b/container/gtype/z_bench_test.go similarity index 100% rename from container/gtype/gtype_test.go rename to container/gtype/z_bench_test.go diff --git a/container/gtype/gtype_z_unit_test.go b/container/gtype/z_unit_test.go similarity index 95% rename from container/gtype/gtype_z_unit_test.go rename to container/gtype/z_unit_test.go index dd4ab0bca..bd9675057 100644 --- a/container/gtype/gtype_z_unit_test.go +++ b/container/gtype/z_unit_test.go @@ -1,3 +1,9 @@ +// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package gtype_test import ( diff --git a/container/gvar/gvar_test.go b/container/gvar/gvar_z_unit_test.go similarity index 100% rename from container/gvar/gvar_test.go rename to container/gvar/gvar_z_unit_test.go diff --git a/database/gkvdb/gkvdb_unit_test.go b/database/gkvdb/gkvdb_unit_test.go index 9a0411f74..a96daa1b7 100644 --- a/database/gkvdb/gkvdb_unit_test.go +++ b/database/gkvdb/gkvdb_unit_test.go @@ -11,6 +11,8 @@ import ( "testing" "time" + "github.com/dgraph-io/badger/options" + "github.com/gogf/gf/frame/g" "github.com/gogf/gf/container/garray" @@ -23,6 +25,10 @@ import ( "github.com/gogf/gf/test/gtest" ) +func init() { + +} + func Test_New(t *testing.T) { gtest.Case(t, func() { name := gconv.String(gtime.Nanosecond()) @@ -31,6 +37,8 @@ func Test_New(t *testing.T) { value := []byte("value") db := gkvdb.Instance(name) + // https://github.com/dgraph-io/badger#memory-usage + db.Options().ValueLogLoadingMode = options.FileIO err := db.SetPath(path) gtest.Assert(err, nil) @@ -51,6 +59,8 @@ func Test_Set(t *testing.T) { value := []byte("value") db := gkvdb.Instance(name) + // https://github.com/dgraph-io/badger#memory-usage + db.Options().ValueLogLoadingMode = options.FileIO err := db.SetPath(path) gtest.Assert(err, nil) @@ -68,6 +78,8 @@ func Test_Iterate(t *testing.T) { name := gconv.String(gtime.Nanosecond()) path := "/tmp/gkvdb/" + name db := gkvdb.Instance(name) + // https://github.com/dgraph-io/badger#memory-usage + db.Options().ValueLogLoadingMode = options.FileIO err := db.SetPath(path) gtest.Assert(err, nil) diff --git a/net/ghttp/ghttp_server_cookie.go b/net/ghttp/ghttp_server_cookie.go index d408651f9..4a9aff47c 100644 --- a/net/ghttp/ghttp_server_cookie.go +++ b/net/ghttp/ghttp_server_cookie.go @@ -16,7 +16,7 @@ import ( "github.com/gogf/gf/os/gtime" ) -// COOKIE对象 +// COOKIE对象,非并发安全。 type Cookie struct { data map[string]CookieItem // 数据项 path string // 默认的cookie path diff --git a/net/ghttp/ghttp_server_session.go b/net/ghttp/ghttp_server_session.go index 5963d7355..de9d20187 100644 --- a/net/ghttp/ghttp_server_session.go +++ b/net/ghttp/ghttp_server_session.go @@ -67,7 +67,9 @@ func (s *Session) init() { if data != nil { s.id = id s.data = gmap.NewStrAnyMap(true) - s.Restore(data) + if err := s.Restore(data); err != nil { + panic(err) + } return } } @@ -178,11 +180,14 @@ func (s *Session) UpdateExpire() { // 优先持久化存储 if s.dirty { data, _ := s.Export() - s.server.sessionStorage.Set( + err := s.server.sessionStorage.Set( []byte(s.id), data, time.Duration(s.server.GetSessionMaxAge())*time.Second, ) + if err != nil { + panic(err) + } } // 其次更新内存TTL s.server.sessions.Set(s.id, s.data, s.server.GetSessionMaxAge()*1000)