作者 yangfu

ab 测试

appname = ab
httpport = 8080
httpport = 8088
runmode = dev
#没设置的话 this.Ctx.Input.RequestBody 没有值
... ... @@ -10,5 +10,5 @@ redis_add_port = "127.0.0.1:6379"
redis_auth = "123456"
#远程
AHost = "http://127.0.0.1:8081"
BHost = "http://127.0.0.1:8082"
AHost = "http://127.0.0.1:8080"
BHost = "http://mmm-opp-dev.fjmaimaimai.com"
... ...
... ... @@ -15,32 +15,28 @@ var aHost string
var bHost string
const (
QueryResponseError = 1 //请求应答错误
CompareResultSuccess=2 //应答对比成功
CompareResultError=3 //应答对比错误
QueryResponseError = 1 //请求应答错误
CompareResultSuccess = 2 //应答对比成功
CompareResultError = 3 //应答对比错误
)
func init(){
aHost =beego.AppConfig.String("AHost")
bHost =beego.AppConfig.String("BHost")
func init() {
aHost = beego.AppConfig.String("AHost")
bHost = beego.AppConfig.String("BHost")
}
//Compare
func(this *ABController)Compare(){
func (this *ABController) Compare() {
var msg *mybeego.Message
defer func(){
defer func() {
this.Resp(msg)
}()
request :=&protocol.CompareRequest{
Method:this.Ctx.Request.Method,
RequestUri:this.Ctx.Request.RequestURI,
Head:this.Ctx.Request.Header,
Body:this.ByteBody,
IsRetry:false,
request := &protocol.CompareRequest{
Method: this.Ctx.Request.Method,
RequestUri: this.Ctx.Request.RequestURI,
Head: this.Ctx.Request.Header,
Body: this.ByteBody,
IsRetry: true,
}
msg = this.GenMessage(ab.Compare(request))
}
... ...
... ... @@ -7,39 +7,41 @@ import (
/*Compare */
type CompareRequest struct {
Method string
Method string
RequestUri string
Head map[string][]string
Body []byte
IsRetry bool
Head map[string][]string
Body []byte
IsRetry bool
}
type CompareResponse struct {
}
type Request struct {
Host string `json:"-"`
Method string `json:"method"`
RequestUri string `json:"request_uri"`
Head map[string][]string `json:"head"`
Body interface{} `json:"body"`
AResp interface{} `json:"a_resp"`
BResp interface{} `json:"b_resp"`
Host string `json:"-"`
Method string `json:"method"`
RequestUri string `json:"request_uri"`
Head map[string][]string `json:"head"`
Body interface{} `json:"body"`
AResp interface{} `json:"a_resp"`
BResp interface{} `json:"b_resp"`
Result bool `json:"result"`
}
func NewRequest(host,method,requestUri string,head map[string][]string,body []byte)Request{
func NewRequest(host, method, requestUri string, head map[string][]string, body []byte) Request {
var mapBody = make(map[string]interface{})
json.Unmarshal(body,&mapBody)
json.Unmarshal(body, &mapBody)
return Request{
//Host:host,
Method:method,
RequestUri:requestUri,
Head:head,
Body:mapBody,
Method: method,
RequestUri: requestUri,
Head: head,
Body: mapBody,
Result: false,
}
}
func(req Request)GetBody()([]byte){
data,err:= json.Marshal(req.Body)
if err!=nil{
func (req Request) GetBody() []byte {
data, err := json.Marshal(req.Body)
if err != nil {
log.Error(err)
return []byte{}
}
... ...
package ab
import (
"encoding/json"
"fmt"
"github.com/astaxie/beego"
"github.com/astaxie/beego/httplib"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
"gitlab.fjmaimaimai.com/mmm-go/ab/protocol"
"github.com/astaxie/beego"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
"io/ioutil"
"net/http"
"reflect"
"strings"
"encoding/json"
)
var aHost string
var bHost string
const (
QueryResponseError = 1 //请求应答错误
CompareResultSuccess=2 //应答对比成功
CompareResultError=3 //应答对比错误
QueryResponseError = 1 //请求应答错误
CompareResultSuccess = 2 //应答对比成功
CompareResultError = 3 //应答对比错误
)
func init(){
aHost =beego.AppConfig.String("AHost")
bHost =beego.AppConfig.String("BHost")
func init() {
aHost = beego.AppConfig.String("AHost")
bHost = beego.AppConfig.String("BHost")
}
func Compare(request *protocol.CompareRequest)(rsp interface{},err error){
func Compare(request *protocol.CompareRequest) (rsp interface{}, err error) {
var (
status string = protocol.StatusOK
key string
key string
)
method := request.Method
requestURI :=request.RequestUri
head :=request.Head
body :=request.Body
newRequest:= protocol.NewRequest(bHost,method,requestURI,head,body)
aResp,err :=makeHttpRequest(aHost,method,requestURI,head,body)
requestURI := request.RequestUri
head := request.Head
body := request.Body
newRequest := protocol.NewRequest(bHost, method, requestURI, head, body)
aResp, err := makeHttpRequest(aHost, method, requestURI, head, body)
key =requestURI
if err!=nil{
key = requestURI
if err != nil {
log.Error(err)
status =protocol.StatusFial
status = protocol.StatusFial
}
var mapA = make(map[string]interface{})
if err =json.Unmarshal(aResp,&mapA);err!=nil{
if err = json.Unmarshal(aResp, &mapA); err != nil {
return
}
//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)))
Compare :=func(){
defer func(){
if p:=recover();p!=nil{
Compare := func() {
defer func() {
if p := recover(); p != nil {
log.Error(p)
}
}()
//判断已经在错误列表里面就不再判断请求
bResp,err :=makeHttpRequest(bHost,method,requestURI,head,body)
if err!=nil{
bResp, err := makeHttpRequest(bHost, method, requestURI, head, body)
if err != nil {
log.Error(err)
errorHandler(QueryResponseError,&newRequest)
errorHandler(QueryResponseError, &newRequest)
return
}
cResult,err :=compareResponse(aResp,bResp,&newRequest)
if err!=nil{
log.Error(err)
errorHandler(QueryResponseError,&newRequest)
cResult, err := compareResponse(aResp, bResp, &newRequest)
if err != nil {
log.Error(err, "\nA:", string(aResp), "\nB:", string(bResp))
errorHandler(QueryResponseError, &newRequest)
return
}
if !cResult{
errorHandler(CompareResultError,&newRequest)
log.Warn(fmt.Sprintf("" +
"[Compare Error] - ErrorType:%v " +
"Method: %v " +
"\nHead: %v " +
"\nAResponse: %v " +
"\nBResponse: %v",CompareResultError,requestURI,head,string(aResp),string(bResp)))
}else{
if !cResult {
errorHandler(CompareResultError, &newRequest)
log.Warn(fmt.Sprintf(""+
"[Compare Error] - ErrorType:%v "+
"Method: %v "+
"\nHead: %v "+
"\nAResponse: %v "+
"\nBResponse: %v", CompareResultError, requestURI, head, string(aResp), string(bResp)))
} else {
newRequest.Result = true
//记录到记录行里面
errorHandler(CompareResultSuccess,&newRequest)
log.Info(fmt.Sprintf("[Compare Success] Method:%v " +
"\nHead:%v " +
"\nBody:%v",requestURI,head,string(body)))
errorHandler(CompareResultSuccess, &newRequest)
log.Info(fmt.Sprintf("[Compare Success] Method:%v "+
"\nHead:%v "+
"\nBody:%v", requestURI, "", string(body)))
}
}
if status==protocol.StatusOK && (!protocol.Exists(protocol.StatusOK,key) || request.IsRetry){
if !protocol.Exists(protocol.StatusFial,key){
if status == protocol.StatusOK && (!protocol.Exists(protocol.StatusOK, key) || request.IsRetry) {
if !protocol.Exists(protocol.StatusFial, key) {
go Compare() //已经在错误列表
} else {
go Compare()
}
}
rsp =mapA
rsp = mapA
return
}
func compareResponse(aResp,bResp []byte,req *protocol.Request)(result bool,err error){
defer func(){
if p:=recover();p!=nil{
func compareResponse(aResp, bResp []byte, req *protocol.Request) (result bool, err error) {
defer func() {
if p := recover(); p != nil {
log.Error(p)
}
}()
var mapA = make(map[string]interface{})
var mapB = make(map[string]interface{})
if err =json.Unmarshal(aResp,&mapA);err!=nil{
if err = json.Unmarshal(aResp, &mapA); err != nil {
result = false
return
}
if err =json.Unmarshal(bResp,&mapB);err!=nil{
if err = json.Unmarshal(bResp, &mapB); err != nil {
result = false
return
}
req.AResp = mapA
req.BResp = mapB
result = reflect.DeepEqual(mapA,mapB)
if strings.EqualFold(string(aResp), string(bResp)) {
return true, nil
}
result = reflect.DeepEqual(mapA, mapB)
return
}
func errorHandler(errType int,req *protocol.Request){
if errType==QueryResponseError{
protocol.SetRequest(protocol.StatusFial,req.RequestUri,req)
func errorHandler(errType int, req *protocol.Request) {
if errType == QueryResponseError {
protocol.SetRequest(protocol.StatusFial, req.RequestUri, req)
return
}
if errType==CompareResultSuccess{
protocol.SetRequest(protocol.StatusOK,req.RequestUri,req)
protocol.DeleteRequest(protocol.StatusFial,req.RequestUri)
if errType == CompareResultSuccess {
protocol.SetRequest(protocol.StatusOK, req.RequestUri, req)
protocol.DeleteRequest(protocol.StatusFial, req.RequestUri)
return
}
if errType==CompareResultError{
protocol.SetRequest(protocol.StatusFial,req.RequestUri,req)
if errType == CompareResultError {
protocol.SetRequest(protocol.StatusFial, req.RequestUri, req)
return
}
}
func makeHttpRequest(host,method,requestUri string,head map[string][]string,body []byte)(data []byte,err error){
func makeHttpRequest(host, method, requestUri string, head map[string][]string, body []byte) (data []byte, err error) {
var (
resp *http.Response
)
rawUrl := host+requestUri
rawUrl := host + requestUri
req := httplib.Post(rawUrl)
for k,v:=range head{
req.Header(k,strings.Join(v,","))
for k, v := range head {
req.Header(k, strings.Join(v, ","))
}
//req.Header("Content-Type","application/json; charset=utf-8")
req.Body(body)
resp,err =req.Response()
if err!=nil{
resp, err = req.Response()
if err != nil {
return
}
data,err = ioutil.ReadAll(resp.Body)
data, err = ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
return
}
... ...