mirror of
https://gitee.com/johng/gf
synced 2026-07-06 21:45:34 +08:00
improve gkvdb, container
This commit is contained in:
@ -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()
|
||||
}
|
||||
}
|
||||
@ -13,7 +13,7 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
l = New()
|
||||
l = New(true)
|
||||
)
|
||||
|
||||
func Benchmark_PushBack(b *testing.B) {
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
68
container/gring/gring_bench_test.go
Normal file
68
container/gring/gring_bench_test.go
Normal file
@ -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++
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -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()
|
||||
}
|
||||
}
|
||||
@ -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) {
|
||||
|
||||
@ -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 (
|
||||
@ -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)
|
||||
|
||||
|
||||
@ -16,7 +16,7 @@ import (
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
)
|
||||
|
||||
// COOKIE对象
|
||||
// COOKIE对象,非并发安全。
|
||||
type Cookie struct {
|
||||
data map[string]CookieItem // 数据项
|
||||
path string // 默认的cookie path
|
||||
|
||||
@ -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)
|
||||
|
||||
Reference in New Issue
Block a user