mirror of
https://github.com/zr-hebo/sniffer-agent.git
synced 2025-08-12 02:23:09 +08:00
use cache reduce malloc
This commit is contained in:
@@ -1,14 +1,16 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
// "encoding/json"
|
||||
"github.com/json-iterator/go"
|
||||
"bytes"
|
||||
|
||||
"encoding/json"
|
||||
// "github.com/json-iterator/go"
|
||||
"time"
|
||||
|
||||
"github.com/pingcap/tidb/util/hack"
|
||||
)
|
||||
|
||||
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
// var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
|
||||
type QueryPiece interface {
|
||||
String() *string
|
||||
@@ -32,20 +34,23 @@ type MysqlQueryPiece struct {
|
||||
ThrowPacketRate float64 `json:"tpr"`
|
||||
BeginTime int64 `json:"bt"`
|
||||
CostTimeInMS int64 `json:"cms"`
|
||||
|
||||
jsonContent []byte `json:"-"`
|
||||
}
|
||||
|
||||
type PooledMysqlQueryPiece struct {
|
||||
MysqlQueryPiece
|
||||
recoverPool *mysqlQueryPiecePool
|
||||
sliceBufferPool *sliceBufferPool
|
||||
}
|
||||
|
||||
const (
|
||||
datetimeFormat = "2006-01-02 15:04:05"
|
||||
millSecondUnit = int64(time.Millisecond)
|
||||
)
|
||||
|
||||
var (
|
||||
mqpp = NewMysqlQueryPiecePool()
|
||||
localSliceBufferPool = NewSliceBufferPool("json cache", 2*1024*1024)
|
||||
)
|
||||
|
||||
func NewPooledMysqlQueryPiece(
|
||||
@@ -53,11 +58,6 @@ func NewPooledMysqlQueryPiece(
|
||||
clientPort, serverPort int, throwPacketRate float64, stmtBeginTime int64) (
|
||||
mqp *PooledMysqlQueryPiece) {
|
||||
mqp = mqpp.Dequeue()
|
||||
if mqp == nil {
|
||||
mqp = &PooledMysqlQueryPiece{
|
||||
MysqlQueryPiece: MysqlQueryPiece{},
|
||||
}
|
||||
}
|
||||
|
||||
nowInMS := time.Now().UnixNano() / millSecondUnit
|
||||
mqp.SessionID = sessionID
|
||||
@@ -73,6 +73,7 @@ func NewPooledMysqlQueryPiece(
|
||||
mqp.BeginTime = stmtBeginTime
|
||||
mqp.CostTimeInMS = nowInMS - stmtBeginTime
|
||||
mqp.recoverPool = mqpp
|
||||
mqp.sliceBufferPool = localSliceBufferPool
|
||||
|
||||
return
|
||||
}
|
||||
@@ -83,13 +84,23 @@ func (mqp *MysqlQueryPiece) String() (*string) {
|
||||
return &contentStr
|
||||
}
|
||||
|
||||
func (mqp *MysqlQueryPiece) Bytes() (bytes []byte) {
|
||||
content, err := json.Marshal(mqp)
|
||||
if err != nil {
|
||||
return []byte(err.Error())
|
||||
func (mqp *MysqlQueryPiece) Bytes() (content []byte) {
|
||||
// content, err := json.Marshal(mqp)
|
||||
if len(mqp.jsonContent) > 0 {
|
||||
return mqp.jsonContent
|
||||
}
|
||||
|
||||
return content
|
||||
var cacheBuffer = localSliceBufferPool.Dequeue()
|
||||
buffer := bytes.NewBuffer(cacheBuffer)
|
||||
err := json.NewEncoder(buffer).Encode(mqp)
|
||||
if err != nil {
|
||||
mqp.jsonContent = []byte(err.Error())
|
||||
|
||||
} else {
|
||||
mqp.jsonContent = buffer.Bytes()
|
||||
}
|
||||
|
||||
return mqp.jsonContent
|
||||
}
|
||||
|
||||
func (mqp *MysqlQueryPiece) GetSQL() (str *string) {
|
||||
@@ -106,4 +117,6 @@ func (mqp *MysqlQueryPiece) SetNeedSyncSend(syncSend bool) {
|
||||
|
||||
func (pmqp *PooledMysqlQueryPiece) Recovery() {
|
||||
pmqp.recoverPool.Enqueue(pmqp)
|
||||
pmqp.sliceBufferPool.Enqueue(pmqp.jsonContent[:0])
|
||||
pmqp.jsonContent = nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user