github.com/lulzWill/go-agent@v2.1.2+incompatible/internal/slow_queries_test.go (about) 1 package internal 2 3 import ( 4 "math/rand" 5 "strconv" 6 "strings" 7 "testing" 8 "time" 9 ) 10 11 func TestEmptySlowQueriesData(t *testing.T) { 12 slows := newSlowQueries(maxHarvestSlowSQLs) 13 js, err := slows.Data("agentRunID", time.Now()) 14 if nil != js || nil != err { 15 t.Error(string(js), err) 16 } 17 } 18 19 func TestSlowQueriesBasic(t *testing.T) { 20 txnSlows := newSlowQueries(maxTxnSlowQueries) 21 txnSlows.observeInstance(slowQueryInstance{ 22 Duration: 2 * time.Second, 23 DatastoreMetric: "Datastore/statement/MySQL/users/INSERT", 24 ParameterizedQuery: "INSERT INTO users (name, age) VALUES ($1, $2)", 25 Host: "db-server-1", 26 PortPathOrID: "3306", 27 DatabaseName: "production", 28 StackTrace: nil, 29 QueryParameters: vetQueryParameters(map[string]interface{}{ 30 strings.Repeat("X", attributeKeyLengthLimit+1): "invalid-key", 31 "invalid-value": struct{}{}, 32 "valid": 123, 33 }), 34 }) 35 harvestSlows := newSlowQueries(maxHarvestSlowSQLs) 36 harvestSlows.Merge(txnSlows, "WebTransaction/Go/hello", "/zip/zap") 37 js, err := harvestSlows.Data("agentRunID", time.Now()) 38 expect := CompactJSONString(`[[ 39 [ 40 "WebTransaction/Go/hello", 41 "/zip/zap", 42 3722056893, 43 "INSERT INTO users (name, age) VALUES ($1, $2)", 44 "Datastore/statement/MySQL/users/INSERT", 45 1, 46 2000, 47 2000, 48 2000, 49 { 50 "host":"db-server-1", 51 "port_path_or_id":"3306", 52 "database_name":"production", 53 "query_parameters":{ 54 "valid":123 55 } 56 } 57 ] 58 ]]`) 59 if nil != err { 60 t.Error(err) 61 } 62 if string(js) != expect { 63 t.Error(string(js), expect) 64 } 65 } 66 67 func TestSlowQueriesAggregation(t *testing.T) { 68 max := 50 69 slows := make([]slowQueryInstance, 3*max) 70 for i := 0; i < max; i++ { 71 num := i + 1 72 str := strconv.Itoa(num) 73 duration := time.Duration(num) * time.Second 74 slow := slowQueryInstance{ 75 DatastoreMetric: "Datastore/" + str, 76 ParameterizedQuery: str, 77 } 78 slow.Duration = duration 79 slow.TxnName = "Txn/0" + str 80 slow.TxnURL = "/0" + str 81 slows[i*3+0] = slow 82 slow.Duration = duration + (100 * time.Second) 83 slow.TxnName = "Txn/1" + str 84 slow.TxnURL = "/1" + str 85 slows[i*3+1] = slow 86 slow.Duration = duration + (200 * time.Second) 87 slow.TxnName = "Txn/2" + str 88 slow.TxnURL = "/2" + str 89 slows[i*3+2] = slow 90 } 91 sq := newSlowQueries(10) 92 seed := int64(99) // arbitrary fixed seed 93 r := rand.New(rand.NewSource(seed)) 94 perm := r.Perm(max * 3) 95 for _, idx := range perm { 96 sq.observeInstance(slows[idx]) 97 } 98 js, err := sq.Data("agentRunID", time.Now()) 99 expect := CompactJSONString(`[[ 100 ["Txn/241","/241",2296612630,"41","Datastore/41",1,241000,241000,241000,{}], 101 ["Txn/242","/242",2279835011,"42","Datastore/42",2,384000,142000,242000,{}], 102 ["Txn/243","/243",2263057392,"43","Datastore/43",2,386000,143000,243000,{}], 103 ["Txn/244","/244",2380500725,"44","Datastore/44",3,432000,44000,244000,{}], 104 ["Txn/247","/247",2330167868,"47","Datastore/47",2,394000,147000,247000,{}], 105 ["Txn/245","/245",2363723106,"45","Datastore/45",2,290000,45000,245000,{}], 106 ["Txn/250","/250",2212577440,"50","Datastore/50",1,250000,250000,250000,{}], 107 ["Txn/246","/246",2346945487,"46","Datastore/46",2,392000,146000,246000,{}], 108 ["Txn/249","/249",2430833582,"49","Datastore/49",3,447000,49000,249000,{}], 109 ["Txn/248","/248",2447611201,"48","Datastore/48",3,444000,48000,248000,{}] 110 ]]`) 111 if nil != err { 112 t.Error(err) 113 } 114 if string(js) != expect { 115 t.Error(string(js), expect) 116 } 117 }