From e6df1c7a5810e2c5642a388c9906e10e29d71aa3 Mon Sep 17 00:00:00 2001 From: John Date: Sat, 28 Apr 2018 11:47:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0gdb=E5=92=8Cgredis=E5=8C=85?= =?UTF-8?q?=E7=9A=84benchmark=E6=B5=8B=E8=AF=95=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/database/gdb/gdb.go | 39 +++++++------------------------- g/database/gdb/gdb_base.go | 6 ++--- g/database/gredis/gredis_test.go | 31 +++++++++++++++++++++++++ geg/frame/config.yml | 8 +++---- geg/other/test.go | 3 --- 5 files changed, 45 insertions(+), 42 deletions(-) create mode 100644 g/database/gredis/gredis_test.go diff --git a/g/database/gdb/gdb.go b/g/database/gdb/gdb.go index 017990a78..9a9c61771 100644 --- a/g/database/gdb/gdb.go +++ b/g/database/gdb/gdb.go @@ -15,8 +15,6 @@ import ( "gitee.com/johng/gf/g/util/grand" _ "github.com/lib/pq" _ "github.com/go-sql-driver/mysql" - "sync" - "gitee.com/johng/gf/g/container/gmap" ) const ( @@ -103,9 +101,6 @@ var linkMysql = &dbmysql{} // PostgreSQL接口对象 var linkPgsql = &dbpgsql{} -// 二级数据库连接池,这是一个临时的对象池,键名为对应的单节点数据库配置,键值为*sync.Pool -var dbPools = gmap.NewStringInterfaceMap() - // 使用默认/指定分组配置进行连接,数据库集群配置项:default func New(groupName...string) (*Db, error) { name := config.d @@ -186,10 +181,16 @@ func newDb (masterNode *ConfigNode, slaveNode *ConfigNode) (*Db, error) { default: return nil, errors.New(fmt.Sprintf("unsupported db type '%s'", masterNode.Type)) } - master := openDb(link, masterNode) + master, err := link.Open(masterNode) + if err != nil { + return nil, err + } slave := master if slaveNode != nil { - slave = openDb(link, slaveNode) + slave, err = link.Open(slaveNode) + if err != nil { + return nil, err + } } return &Db { link : link, @@ -200,30 +201,6 @@ func newDb (masterNode *ConfigNode, slaveNode *ConfigNode) (*Db, error) { }, nil } -func openDb (link Link, node *ConfigNode) *sql.DB { - var pool *sync.Pool - poolKey := fmt.Sprintf("%v", *node) - if v := dbPools.Get(poolKey); v == nil { - pool := sync.Pool{ - New : func() interface{} { - if db, err := link.Open(node); err != nil { - //glog.Error(err) - return nil - } else { - return db - } - }, - } - dbPools.Set(poolKey, pool) - } else { - pool = v.(*sync.Pool) - } - if v := pool.Get(); v != nil { - return v.(*sql.DB) - } - return nil -} - // 将结果列表按照指定的字段值做map[string]Map func (list List) ToStringMap(key string) map[string]Map { m := make(map[string]Map) diff --git a/g/database/gdb/gdb_base.go b/g/database/gdb/gdb_base.go index e9c50cf33..f92e648e8 100644 --- a/g/database/gdb/gdb_base.go +++ b/g/database/gdb/gdb_base.go @@ -20,16 +20,14 @@ import ( // 关闭链接 func (db *Db) Close() error { if db.master != nil { - err := db.master.Close() - if err == nil { + if err := db.master.Close(); err == nil { db.master = nil } else { return err } } if db.slave != nil { - err := db.slave.Close() - if err == nil { + if err := db.slave.Close(); err == nil { db.slave = nil } else { return err diff --git a/g/database/gredis/gredis_test.go b/g/database/gredis/gredis_test.go new file mode 100644 index 000000000..2a8a6f082 --- /dev/null +++ b/g/database/gredis/gredis_test.go @@ -0,0 +1,31 @@ +// Copyright 2018 gf Author(https://gitee.com/johng/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://gitee.com/johng/gf. + +// go test *.go -bench=".*" + +package gredis_test + +import ( + "testing" + "gitee.com/johng/gf/g" +) + +func init() { + // 这里需要修改为本地配置文件的目录地址 + g.Config().SetPath("/home/john/Workspace/Go/GOPATH/src/gitee.com/johng/gf/geg/frame") +} + +func Benchmark_New(b *testing.B) { + for i := 0; i < b.N; i++ { + g.Redis() + } +} + +func Benchmark_NewAndClose(b *testing.B) { + for i := 0; i < b.N; i++ { + g.Redis().Close() + } +} diff --git a/geg/frame/config.yml b/geg/frame/config.yml index 84a2fa21c..550d322b1 100644 --- a/geg/frame/config.yml +++ b/geg/frame/config.yml @@ -6,7 +6,7 @@ database: - host: 127.0.0.1 port: 3306 user: root - pass: "8692651" + pass: "123456" name: test type: mysql role: master @@ -15,7 +15,7 @@ database: - host: 127.0.0.1 port: 3306 user: root - pass: "8692651" + pass: "123456" name: test type: mysql role: master @@ -23,5 +23,5 @@ database: priority: 1 # Redis数据库配置 redis: - disk: 127.0.0.1:6379,0 - cache: 127.0.0.1:6379,2 + default: 127.0.0.1:6379,0 + cache : 127.0.0.1:6379,2 diff --git a/geg/other/test.go b/geg/other/test.go index aeb1e5efd..5aa12c1dc 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -3,12 +3,9 @@ package main import ( "fmt" "math/rand" - "sync" ) func main() { - p := &sync.Pool{} - p.Put() for i := 0; i < 100; i++ { fmt.Println(rand.Intn(200)) }