mirror of
https://github.com/zr-hebo/sniffer-agent.git
synced 2025-08-11 23:56:45 +08:00
优化性能
This commit is contained in:
@@ -2,34 +2,76 @@ package model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/pingcap/tidb/util/hack"
|
||||
)
|
||||
|
||||
type QueryPiece interface {
|
||||
String() string
|
||||
String() *string
|
||||
Bytes() []byte
|
||||
GetSQL() string
|
||||
GetSQL() *string
|
||||
NeedSyncSend() bool
|
||||
Recovery()
|
||||
}
|
||||
|
||||
// MysqlQueryPiece 查询信息
|
||||
type MysqlQueryPiece struct {
|
||||
SessionID string `json:"sid"`
|
||||
ClientHost string `json:"-"`
|
||||
ServerIP string `json:"sip"`
|
||||
SessionID *string `json:"sid"`
|
||||
ClientHost *string `json:"-"`
|
||||
SyncSend bool `json:"-"`
|
||||
ServerIP *string `json:"sip"`
|
||||
ServerPort int `json:"sport"`
|
||||
VisitUser *string `json:"user"`
|
||||
VisitDB *string `json:"db"`
|
||||
QuerySQL *string `json:"sql"`
|
||||
BeginTime string `json:"bt"`
|
||||
CostTimeInMS int64 `json:"cms"`
|
||||
CostTimeInMS int64 `json:"cms"`
|
||||
}
|
||||
|
||||
func (qp *MysqlQueryPiece) String() (str string) {
|
||||
content, err := json.Marshal(qp)
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
type PooledMysqlQueryPiece struct {
|
||||
MysqlQueryPiece
|
||||
recoverPool *mysqlQueryPiecePool
|
||||
}
|
||||
|
||||
const (
|
||||
datetimeFormat = "2006-01-02 15:04:05"
|
||||
millSecondUnit = int64(time.Millisecond)
|
||||
)
|
||||
|
||||
var (
|
||||
mqpp = NewMysqlQueryPiecePool()
|
||||
)
|
||||
|
||||
func NewPooledMysqlQueryPiece(
|
||||
sessionID, visitUser, visitDB, clientHost, serverIP *string, serverPort int, stmtBeginTime int64) (
|
||||
mqp *PooledMysqlQueryPiece) {
|
||||
mqp = mqpp.Dequeue()
|
||||
if mqp == nil {
|
||||
mqp = &PooledMysqlQueryPiece{
|
||||
MysqlQueryPiece: MysqlQueryPiece{},
|
||||
}
|
||||
}
|
||||
|
||||
return string(content)
|
||||
nowInMS := time.Now().UnixNano() / millSecondUnit
|
||||
mqp.SessionID = sessionID
|
||||
mqp.ClientHost = clientHost
|
||||
mqp.ServerIP = serverIP
|
||||
mqp.ServerPort = serverPort
|
||||
mqp.VisitUser = visitUser
|
||||
mqp.VisitDB = visitDB
|
||||
mqp.SyncSend = false
|
||||
mqp.BeginTime = time.Unix(stmtBeginTime/1000, 0).Format(datetimeFormat)
|
||||
mqp.CostTimeInMS = nowInMS - stmtBeginTime
|
||||
mqp.recoverPool = mqpp
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (qp *MysqlQueryPiece) String() (*string) {
|
||||
content := qp.Bytes()
|
||||
contentStr := hack.String(content)
|
||||
return &contentStr
|
||||
}
|
||||
|
||||
func (qp *MysqlQueryPiece) Bytes() (bytes []byte) {
|
||||
@@ -41,9 +83,18 @@ func (qp *MysqlQueryPiece) Bytes() (bytes []byte) {
|
||||
return content
|
||||
}
|
||||
|
||||
func (qp *MysqlQueryPiece) GetSQL() (str string) {
|
||||
if qp.QuerySQL != nil {
|
||||
return *qp.QuerySQL
|
||||
}
|
||||
return ""
|
||||
func (qp *MysqlQueryPiece) GetSQL() (str *string) {
|
||||
return qp.QuerySQL
|
||||
}
|
||||
|
||||
func (qp *MysqlQueryPiece) NeedSyncSend() (bool) {
|
||||
return qp.SyncSend
|
||||
}
|
||||
|
||||
func (qp *MysqlQueryPiece) SetNeedSyncSend(syncSend bool) {
|
||||
qp.SyncSend = syncSend
|
||||
}
|
||||
|
||||
func (pmqp *PooledMysqlQueryPiece) Recovery() {
|
||||
pmqp.recoverPool.Enqueue(pmqp)
|
||||
}
|
||||
|
36
model/query_piece_queue.go
Normal file
36
model/query_piece_queue.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
type mysqlQueryPiecePool struct {
|
||||
queue []*PooledMysqlQueryPiece
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
func NewMysqlQueryPiecePool() (mqpp *mysqlQueryPiecePool) {
|
||||
return &mysqlQueryPiecePool{
|
||||
queue: make([]*PooledMysqlQueryPiece, 0, 5000),
|
||||
}
|
||||
}
|
||||
|
||||
func (mqpp *mysqlQueryPiecePool) Enqueue(pmqp *PooledMysqlQueryPiece) {
|
||||
mqpp.lock.Lock()
|
||||
defer mqpp.lock.Unlock()
|
||||
|
||||
mqpp.queue = append(mqpp.queue, pmqp)
|
||||
}
|
||||
|
||||
func (mqpp *mysqlQueryPiecePool) Dequeue() (pmqp *PooledMysqlQueryPiece) {
|
||||
mqpp.lock.Lock()
|
||||
defer mqpp.lock.Unlock()
|
||||
|
||||
if len(mqpp.queue) < 1 {
|
||||
return nil
|
||||
}
|
||||
|
||||
pmqp = mqpp.queue[0]
|
||||
mqpp.queue = mqpp.queue[1:]
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user