mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2026-01-01 19:16:35 +08:00
增加对数据库操作的统计命令:edge-node dbstat/减少几个不必要的查询操作
This commit is contained in:
69
internal/utils/dbs/db.go
Normal file
69
internal/utils/dbs/db.go
Normal file
@@ -0,0 +1,69 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package dbs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type DB struct {
|
||||
rawDB *sql.DB
|
||||
|
||||
enableStat bool
|
||||
}
|
||||
|
||||
func NewDB(rawDB *sql.DB) *DB {
|
||||
return &DB{
|
||||
rawDB: rawDB,
|
||||
}
|
||||
}
|
||||
|
||||
func (this *DB) EnableStat(b bool) {
|
||||
this.enableStat = b
|
||||
}
|
||||
|
||||
func (this *DB) Prepare(query string) (*Stmt, error) {
|
||||
stmt, err := this.rawDB.Prepare(query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var s = NewStmt(stmt, query)
|
||||
if this.enableStat {
|
||||
s.EnableStat()
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (this *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
|
||||
if this.enableStat {
|
||||
defer SharedQueryStatManager.AddQuery(query).End()
|
||||
}
|
||||
return this.rawDB.ExecContext(ctx, query, args...)
|
||||
}
|
||||
|
||||
func (this *DB) Exec(query string, args ...interface{}) (sql.Result, error) {
|
||||
if this.enableStat {
|
||||
defer SharedQueryStatManager.AddQuery(query).End()
|
||||
}
|
||||
return this.rawDB.Exec(query, args...)
|
||||
}
|
||||
|
||||
func (this *DB) Query(query string, args ...interface{}) (*sql.Rows, error) {
|
||||
if this.enableStat {
|
||||
defer SharedQueryStatManager.AddQuery(query).End()
|
||||
}
|
||||
return this.rawDB.Query(query, args...)
|
||||
}
|
||||
|
||||
func (this *DB) QueryRow(query string, args ...interface{}) *sql.Row {
|
||||
if this.enableStat {
|
||||
defer SharedQueryStatManager.AddQuery(query).End()
|
||||
}
|
||||
return this.rawDB.QueryRow(query, args...)
|
||||
}
|
||||
|
||||
func (this *DB) Close() error {
|
||||
return this.rawDB.Close()
|
||||
}
|
||||
24
internal/utils/dbs/query_label.go
Normal file
24
internal/utils/dbs/query_label.go
Normal file
@@ -0,0 +1,24 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package dbs
|
||||
|
||||
import "time"
|
||||
|
||||
type QueryLabel struct {
|
||||
manager *QueryStatManager
|
||||
query string
|
||||
before time.Time
|
||||
}
|
||||
|
||||
func NewQueryLabel(manager *QueryStatManager, query string) *QueryLabel {
|
||||
return &QueryLabel{
|
||||
manager: manager,
|
||||
query: query,
|
||||
before: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
func (this *QueryLabel) End() {
|
||||
var cost = time.Since(this.before).Seconds()
|
||||
this.manager.AddCost(this.query, cost)
|
||||
}
|
||||
30
internal/utils/dbs/query_stat.go
Normal file
30
internal/utils/dbs/query_stat.go
Normal file
@@ -0,0 +1,30 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package dbs
|
||||
|
||||
type QueryStat struct {
|
||||
Query string
|
||||
CostMin float64
|
||||
CostMax float64
|
||||
|
||||
CostTotal float64
|
||||
Calls int64
|
||||
}
|
||||
|
||||
func NewQueryStat(query string) *QueryStat {
|
||||
return &QueryStat{
|
||||
Query: query,
|
||||
}
|
||||
}
|
||||
|
||||
func (this *QueryStat) AddCost(cost float64) {
|
||||
if this.CostMin == 0 || this.CostMin > cost {
|
||||
this.CostMin = cost
|
||||
}
|
||||
if this.CostMax == 0 || this.CostMax < cost {
|
||||
this.CostMax = cost
|
||||
}
|
||||
|
||||
this.CostTotal += cost
|
||||
this.Calls++
|
||||
}
|
||||
81
internal/utils/dbs/query_stat_manager.go
Normal file
81
internal/utils/dbs/query_stat_manager.go
Normal file
@@ -0,0 +1,81 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package dbs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/events"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/goman"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
var ticker = time.NewTicker(5 * time.Second)
|
||||
|
||||
events.On(events.EventLoaded, func() {
|
||||
if teaconst.EnableDBStat {
|
||||
goman.New(func() {
|
||||
for range ticker.C {
|
||||
var stats = []string{}
|
||||
for _, stat := range SharedQueryStatManager.TopN(10) {
|
||||
var avg = stat.CostTotal / float64(stat.Calls)
|
||||
stats = append(stats, fmt.Sprintf("%.2fms/%.2fms/%.2fms - %d - %s", stat.CostMin*1000, stat.CostMax*1000, avg*1000, stat.Calls, stat.Query))
|
||||
}
|
||||
logs.Println("====DB STATS====\n" + strings.Join(stats, "\n"))
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
var SharedQueryStatManager = NewQueryStatManager()
|
||||
|
||||
type QueryStatManager struct {
|
||||
statsMap map[string]*QueryStat // query => *QueryStat
|
||||
locker sync.Mutex
|
||||
}
|
||||
|
||||
func NewQueryStatManager() *QueryStatManager {
|
||||
return &QueryStatManager{
|
||||
statsMap: map[string]*QueryStat{},
|
||||
}
|
||||
}
|
||||
|
||||
func (this *QueryStatManager) AddQuery(query string) *QueryLabel {
|
||||
return NewQueryLabel(this, query)
|
||||
}
|
||||
|
||||
func (this *QueryStatManager) AddCost(query string, cost float64) {
|
||||
this.locker.Lock()
|
||||
defer this.locker.Unlock()
|
||||
|
||||
stat, ok := this.statsMap[query]
|
||||
if !ok {
|
||||
stat = NewQueryStat(query)
|
||||
this.statsMap[query] = stat
|
||||
}
|
||||
stat.AddCost(cost)
|
||||
}
|
||||
|
||||
func (this *QueryStatManager) TopN(n int) []*QueryStat {
|
||||
this.locker.Lock()
|
||||
defer this.locker.Unlock()
|
||||
|
||||
var stats = []*QueryStat{}
|
||||
for _, stat := range this.statsMap {
|
||||
stats = append(stats, stat)
|
||||
}
|
||||
sort.Slice(stats, func(i, j int) bool {
|
||||
return stats[i].CostMax > stats[j].CostMax
|
||||
})
|
||||
|
||||
if len(stats) > n {
|
||||
return stats[:n]
|
||||
}
|
||||
return stats
|
||||
}
|
||||
24
internal/utils/dbs/query_stat_manager_test.go
Normal file
24
internal/utils/dbs/query_stat_manager_test.go
Normal file
@@ -0,0 +1,24 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package dbs_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/dbs"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestQueryStatManager(t *testing.T) {
|
||||
var manager = dbs.NewQueryStatManager()
|
||||
{
|
||||
var label = manager.AddQuery("sql 1")
|
||||
time.Sleep(1 * time.Second)
|
||||
label.End()
|
||||
}
|
||||
manager.AddQuery("sql 1").End()
|
||||
manager.AddQuery("sql 2").End()
|
||||
for _, stat := range manager.TopN(10) {
|
||||
logs.PrintAsJSON(stat, t)
|
||||
}
|
||||
}
|
||||
72
internal/utils/dbs/stmt.go
Normal file
72
internal/utils/dbs/stmt.go
Normal file
@@ -0,0 +1,72 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package dbs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type Stmt struct {
|
||||
rawStmt *sql.Stmt
|
||||
query string
|
||||
|
||||
enableStat bool
|
||||
}
|
||||
|
||||
func NewStmt(rawStmt *sql.Stmt, query string) *Stmt {
|
||||
return &Stmt{
|
||||
rawStmt: rawStmt,
|
||||
query: query,
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Stmt) EnableStat() {
|
||||
this.enableStat = true
|
||||
}
|
||||
|
||||
func (this *Stmt) ExecContext(ctx context.Context, args ...interface{}) (sql.Result, error) {
|
||||
if this.enableStat {
|
||||
defer SharedQueryStatManager.AddQuery(this.query).End()
|
||||
}
|
||||
return this.rawStmt.ExecContext(ctx, args...)
|
||||
}
|
||||
|
||||
func (this *Stmt) Exec(args ...interface{}) (sql.Result, error) {
|
||||
if this.enableStat {
|
||||
defer SharedQueryStatManager.AddQuery(this.query).End()
|
||||
}
|
||||
return this.rawStmt.Exec(args...)
|
||||
}
|
||||
|
||||
func (this *Stmt) QueryContext(ctx context.Context, args ...interface{}) (*sql.Rows, error) {
|
||||
if this.enableStat {
|
||||
defer SharedQueryStatManager.AddQuery(this.query).End()
|
||||
}
|
||||
return this.rawStmt.QueryContext(ctx, args...)
|
||||
}
|
||||
|
||||
func (this *Stmt) Query(args ...interface{}) (*sql.Rows, error) {
|
||||
if this.enableStat {
|
||||
defer SharedQueryStatManager.AddQuery(this.query).End()
|
||||
}
|
||||
return this.rawStmt.Query(args...)
|
||||
}
|
||||
|
||||
func (this *Stmt) QueryRowContext(ctx context.Context, args ...interface{}) *sql.Row {
|
||||
if this.enableStat {
|
||||
defer SharedQueryStatManager.AddQuery(this.query).End()
|
||||
}
|
||||
return this.rawStmt.QueryRowContext(ctx, args...)
|
||||
}
|
||||
|
||||
func (this *Stmt) QueryRow(args ...interface{}) *sql.Row {
|
||||
if this.enableStat {
|
||||
defer SharedQueryStatManager.AddQuery(this.query).End()
|
||||
}
|
||||
return this.rawStmt.QueryRow(args...)
|
||||
}
|
||||
|
||||
func (this *Stmt) Close() error {
|
||||
return this.rawStmt.Close()
|
||||
}
|
||||
Reference in New Issue
Block a user