作者 yangfu

ab 测试

1 appname = ab 1 appname = ab
2 -httpport = 8080 2 +httpport = 8088
3 runmode = dev 3 runmode = dev
4 4
5 #没设置的话 this.Ctx.Input.RequestBody 没有值 5 #没设置的话 this.Ctx.Input.RequestBody 没有值
@@ -10,5 +10,5 @@ redis_add_port = "127.0.0.1:6379" @@ -10,5 +10,5 @@ redis_add_port = "127.0.0.1:6379"
10 redis_auth = "123456" 10 redis_auth = "123456"
11 11
12 #远程 12 #远程
13 -AHost = "http://127.0.0.1:8081"  
14 -BHost = "http://127.0.0.1:8082" 13 +AHost = "http://127.0.0.1:8080"
  14 +BHost = "http://mmm-opp-dev.fjmaimaimai.com"
@@ -16,31 +16,27 @@ var bHost string @@ -16,31 +16,27 @@ var bHost string
16 16
17 const ( 17 const (
18 QueryResponseError = 1 //请求应答错误 18 QueryResponseError = 1 //请求应答错误
19 - CompareResultSuccess=2 //应答对比成功  
20 - CompareResultError=3 //应答对比错误 19 + CompareResultSuccess = 2 //应答对比成功
  20 + CompareResultError = 3 //应答对比错误
21 ) 21 )
22 22
23 -func init(){  
24 - aHost =beego.AppConfig.String("AHost")  
25 - bHost =beego.AppConfig.String("BHost") 23 +func init() {
  24 + aHost = beego.AppConfig.String("AHost")
  25 + bHost = beego.AppConfig.String("BHost")
26 } 26 }
27 27
28 //Compare 28 //Compare
29 -func(this *ABController)Compare(){ 29 +func (this *ABController) Compare() {
30 var msg *mybeego.Message 30 var msg *mybeego.Message
31 - defer func(){ 31 + defer func() {
32 this.Resp(msg) 32 this.Resp(msg)
33 }() 33 }()
34 - request :=&protocol.CompareRequest{  
35 - Method:this.Ctx.Request.Method,  
36 - RequestUri:this.Ctx.Request.RequestURI,  
37 - Head:this.Ctx.Request.Header,  
38 - Body:this.ByteBody,  
39 - IsRetry:false, 34 + request := &protocol.CompareRequest{
  35 + Method: this.Ctx.Request.Method,
  36 + RequestUri: this.Ctx.Request.RequestURI,
  37 + Head: this.Ctx.Request.Header,
  38 + Body: this.ByteBody,
  39 + IsRetry: true,
40 } 40 }
41 msg = this.GenMessage(ab.Compare(request)) 41 msg = this.GenMessage(ab.Compare(request))
42 } 42 }
43 -  
44 -  
45 -  
46 -  
@@ -24,22 +24,24 @@ type Request struct { @@ -24,22 +24,24 @@ type Request struct {
24 Body interface{} `json:"body"` 24 Body interface{} `json:"body"`
25 AResp interface{} `json:"a_resp"` 25 AResp interface{} `json:"a_resp"`
26 BResp interface{} `json:"b_resp"` 26 BResp interface{} `json:"b_resp"`
  27 + Result bool `json:"result"`
27 } 28 }
28 29
29 -func NewRequest(host,method,requestUri string,head map[string][]string,body []byte)Request{ 30 +func NewRequest(host, method, requestUri string, head map[string][]string, body []byte) Request {
30 var mapBody = make(map[string]interface{}) 31 var mapBody = make(map[string]interface{})
31 - json.Unmarshal(body,&mapBody) 32 + json.Unmarshal(body, &mapBody)
32 return Request{ 33 return Request{
33 //Host:host, 34 //Host:host,
34 - Method:method,  
35 - RequestUri:requestUri,  
36 - Head:head,  
37 - Body:mapBody, 35 + Method: method,
  36 + RequestUri: requestUri,
  37 + Head: head,
  38 + Body: mapBody,
  39 + Result: false,
38 } 40 }
39 } 41 }
40 -func(req Request)GetBody()([]byte){  
41 - data,err:= json.Marshal(req.Body)  
42 - if err!=nil{ 42 +func (req Request) GetBody() []byte {
  43 + data, err := json.Marshal(req.Body)
  44 + if err != nil {
43 log.Error(err) 45 log.Error(err)
44 return []byte{} 46 return []byte{}
45 } 47 }
1 package ab 1 package ab
2 2
3 import ( 3 import (
  4 + "encoding/json"
4 "fmt" 5 "fmt"
  6 + "github.com/astaxie/beego"
5 "github.com/astaxie/beego/httplib" 7 "github.com/astaxie/beego/httplib"
6 - "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"  
7 "gitlab.fjmaimaimai.com/mmm-go/ab/protocol" 8 "gitlab.fjmaimaimai.com/mmm-go/ab/protocol"
8 - "github.com/astaxie/beego" 9 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
9 "io/ioutil" 10 "io/ioutil"
10 "net/http" 11 "net/http"
11 "reflect" 12 "reflect"
12 "strings" 13 "strings"
13 - "encoding/json"  
14 ) 14 )
15 15
16 var aHost string 16 var aHost string
@@ -18,133 +18,142 @@ var bHost string @@ -18,133 +18,142 @@ var bHost string
18 18
19 const ( 19 const (
20 QueryResponseError = 1 //请求应答错误 20 QueryResponseError = 1 //请求应答错误
21 - CompareResultSuccess=2 //应答对比成功  
22 - CompareResultError=3 //应答对比错误 21 + CompareResultSuccess = 2 //应答对比成功
  22 + CompareResultError = 3 //应答对比错误
23 ) 23 )
24 24
25 -func init(){  
26 - aHost =beego.AppConfig.String("AHost")  
27 - bHost =beego.AppConfig.String("BHost") 25 +func init() {
  26 + aHost = beego.AppConfig.String("AHost")
  27 + bHost = beego.AppConfig.String("BHost")
28 } 28 }
29 29
30 -func Compare(request *protocol.CompareRequest)(rsp interface{},err error){ 30 +func Compare(request *protocol.CompareRequest) (rsp interface{}, err error) {
31 var ( 31 var (
32 status string = protocol.StatusOK 32 status string = protocol.StatusOK
33 key string 33 key string
34 ) 34 )
35 35
36 method := request.Method 36 method := request.Method
37 - requestURI :=request.RequestUri  
38 - head :=request.Head  
39 - body :=request.Body  
40 - newRequest:= protocol.NewRequest(bHost,method,requestURI,head,body)  
41 - aResp,err :=makeHttpRequest(aHost,method,requestURI,head,body) 37 + requestURI := request.RequestUri
  38 + head := request.Head
  39 + body := request.Body
  40 + newRequest := protocol.NewRequest(bHost, method, requestURI, head, body)
  41 + aResp, err := makeHttpRequest(aHost, method, requestURI, head, body)
42 42
43 - key =requestURI  
44 - if err!=nil{ 43 + key = requestURI
  44 + if err != nil {
45 log.Error(err) 45 log.Error(err)
46 - status =protocol.StatusFial 46 + status = protocol.StatusFial
47 } 47 }
48 var mapA = make(map[string]interface{}) 48 var mapA = make(map[string]interface{})
49 - if err =json.Unmarshal(aResp,&mapA);err!=nil{ 49 + if err = json.Unmarshal(aResp, &mapA); err != nil {
50 return 50 return
51 } 51 }
52 //log.Debug(fmt.Sprintf("[%v]- URI:%v \n Head:\n%v \nBody:\n%v \n Response:\n%v",status,requestURI,head,string(body),string(aResp))) 52 //log.Debug(fmt.Sprintf("[%v]- URI:%v \n Head:\n%v \nBody:\n%v \n Response:\n%v",status,requestURI,head,string(body),string(aResp)))
53 - Compare :=func(){  
54 - defer func(){  
55 - if p:=recover();p!=nil{ 53 + Compare := func() {
  54 + defer func() {
  55 + if p := recover(); p != nil {
56 log.Error(p) 56 log.Error(p)
57 } 57 }
58 }() 58 }()
59 //判断已经在错误列表里面就不再判断请求 59 //判断已经在错误列表里面就不再判断请求
60 - bResp,err :=makeHttpRequest(bHost,method,requestURI,head,body)  
61 - if err!=nil{ 60 + bResp, err := makeHttpRequest(bHost, method, requestURI, head, body)
  61 + if err != nil {
62 log.Error(err) 62 log.Error(err)
63 - errorHandler(QueryResponseError,&newRequest) 63 + errorHandler(QueryResponseError, &newRequest)
64 return 64 return
65 } 65 }
66 - cResult,err :=compareResponse(aResp,bResp,&newRequest)  
67 - if err!=nil{  
68 - log.Error(err)  
69 - errorHandler(QueryResponseError,&newRequest) 66 + cResult, err := compareResponse(aResp, bResp, &newRequest)
  67 + if err != nil {
  68 + log.Error(err, "\nA:", string(aResp), "\nB:", string(bResp))
  69 + errorHandler(QueryResponseError, &newRequest)
70 return 70 return
71 } 71 }
72 - if !cResult{  
73 - errorHandler(CompareResultError,&newRequest)  
74 - log.Warn(fmt.Sprintf("" +  
75 - "[Compare Error] - ErrorType:%v " +  
76 - "Method: %v " +  
77 - "\nHead: %v " +  
78 - "\nAResponse: %v " +  
79 - "\nBResponse: %v",CompareResultError,requestURI,head,string(aResp),string(bResp)))  
80 - }else{ 72 + if !cResult {
  73 + errorHandler(CompareResultError, &newRequest)
  74 + log.Warn(fmt.Sprintf(""+
  75 + "[Compare Error] - ErrorType:%v "+
  76 + "Method: %v "+
  77 + "\nHead: %v "+
  78 + "\nAResponse: %v "+
  79 + "\nBResponse: %v", CompareResultError, requestURI, head, string(aResp), string(bResp)))
  80 + } else {
  81 + newRequest.Result = true
81 //记录到记录行里面 82 //记录到记录行里面
82 - errorHandler(CompareResultSuccess,&newRequest)  
83 - log.Info(fmt.Sprintf("[Compare Success] Method:%v " +  
84 - "\nHead:%v " +  
85 - "\nBody:%v",requestURI,head,string(body))) 83 + errorHandler(CompareResultSuccess, &newRequest)
  84 + log.Info(fmt.Sprintf("[Compare Success] Method:%v "+
  85 +
  86 + "\nHead:%v "+
  87 + "\nBody:%v", requestURI, "", string(body)))
86 } 88 }
87 } 89 }
88 - if status==protocol.StatusOK && (!protocol.Exists(protocol.StatusOK,key) || request.IsRetry){  
89 - if !protocol.Exists(protocol.StatusFial,key){ 90 + if status == protocol.StatusOK && (!protocol.Exists(protocol.StatusOK, key) || request.IsRetry) {
  91 + if !protocol.Exists(protocol.StatusFial, key) {
90 go Compare() //已经在错误列表 92 go Compare() //已经在错误列表
  93 + } else {
  94 + go Compare()
91 } 95 }
92 } 96 }
93 - rsp =mapA 97 + rsp = mapA
94 return 98 return
95 } 99 }
96 -func compareResponse(aResp,bResp []byte,req *protocol.Request)(result bool,err error){  
97 - defer func(){  
98 - if p:=recover();p!=nil{ 100 +func compareResponse(aResp, bResp []byte, req *protocol.Request) (result bool, err error) {
  101 + defer func() {
  102 + if p := recover(); p != nil {
99 log.Error(p) 103 log.Error(p)
100 } 104 }
101 }() 105 }()
102 var mapA = make(map[string]interface{}) 106 var mapA = make(map[string]interface{})
103 var mapB = make(map[string]interface{}) 107 var mapB = make(map[string]interface{})
104 108
105 - if err =json.Unmarshal(aResp,&mapA);err!=nil{ 109 + if err = json.Unmarshal(aResp, &mapA); err != nil {
106 result = false 110 result = false
107 return 111 return
108 } 112 }
109 - if err =json.Unmarshal(bResp,&mapB);err!=nil{ 113 + if err = json.Unmarshal(bResp, &mapB); err != nil {
110 result = false 114 result = false
111 return 115 return
112 } 116 }
113 req.AResp = mapA 117 req.AResp = mapA
114 req.BResp = mapB 118 req.BResp = mapB
115 - result = reflect.DeepEqual(mapA,mapB) 119 + if strings.EqualFold(string(aResp), string(bResp)) {
  120 + return true, nil
  121 + }
  122 + result = reflect.DeepEqual(mapA, mapB)
  123 +
116 return 124 return
117 } 125 }
118 -func errorHandler(errType int,req *protocol.Request){  
119 - if errType==QueryResponseError{  
120 - protocol.SetRequest(protocol.StatusFial,req.RequestUri,req) 126 +func errorHandler(errType int, req *protocol.Request) {
  127 + if errType == QueryResponseError {
  128 + protocol.SetRequest(protocol.StatusFial, req.RequestUri, req)
121 return 129 return
122 } 130 }
123 - if errType==CompareResultSuccess{  
124 - protocol.SetRequest(protocol.StatusOK,req.RequestUri,req)  
125 - protocol.DeleteRequest(protocol.StatusFial,req.RequestUri) 131 + if errType == CompareResultSuccess {
  132 + protocol.SetRequest(protocol.StatusOK, req.RequestUri, req)
  133 + protocol.DeleteRequest(protocol.StatusFial, req.RequestUri)
126 return 134 return
127 } 135 }
128 - if errType==CompareResultError{  
129 - protocol.SetRequest(protocol.StatusFial,req.RequestUri,req) 136 + if errType == CompareResultError {
  137 + protocol.SetRequest(protocol.StatusFial, req.RequestUri, req)
130 return 138 return
131 } 139 }
132 } 140 }
133 -func makeHttpRequest(host,method,requestUri string,head map[string][]string,body []byte)(data []byte,err error){ 141 +func makeHttpRequest(host, method, requestUri string, head map[string][]string, body []byte) (data []byte, err error) {
134 var ( 142 var (
135 resp *http.Response 143 resp *http.Response
136 ) 144 )
137 - rawUrl := host+requestUri 145 + rawUrl := host + requestUri
138 req := httplib.Post(rawUrl) 146 req := httplib.Post(rawUrl)
139 - for k,v:=range head{  
140 - req.Header(k,strings.Join(v,",")) 147 + for k, v := range head {
  148 + req.Header(k, strings.Join(v, ","))
141 } 149 }
  150 + //req.Header("Content-Type","application/json; charset=utf-8")
142 req.Body(body) 151 req.Body(body)
143 - resp,err =req.Response()  
144 - if err!=nil{ 152 + resp, err = req.Response()
  153 + if err != nil {
145 return 154 return
146 } 155 }
147 - data,err = ioutil.ReadAll(resp.Body) 156 + data, err = ioutil.ReadAll(resp.Body)
148 defer resp.Body.Close() 157 defer resp.Body.Close()
149 return 158 return
150 } 159 }