更新时间:2024-03-29 GMT+08:00
分享

签名约定

请求签名

func(map[string]string) interface{}

入参:性能测试服务内置参数以及自定义参数。

出参:interface需实现IResultV1接口,如有必要,请使用性能测试服务提供的IResultV1接口实现

表1 性能测试服务内置参数

名称

含义

补充说明

__name

请求名称

-

__goroutine_id

协程id

-

__executor_index

执行机索引

序号从0开始。

__executor_count

执行机总数

-

函数签名

func(map[string]string) string

入参:自定义参数。

出参:字符串。

检查点签名

func(map[string]string) string

入参:自定义参数

出参:字符串,空字符串表示检查成功,非空字符串表示检查失败信息。

IResultV1接口实现

package main

import (
	"fmt"
	"time"
)

const (
	InnerVarName          = "__name"
	InnerVarGoroutineId   = "__goroutine_id"
	InnerVarExecutorIndex = "__executor_index"
	InnerVarExecutorCount = "__executor_count"
)

type IResultV1 interface {
	GetName() string
	GetUrl() string
	GetMethod() string
	GetRequestHeader() map[string]string
	GetRequestBody() string
	GetSentBytes() int
	GetResponseCode() int
	GetResponseHeader() map[string]string
	GetResponseBody() string
	GetReceivedBytes() int
	GetFailureMessage() string
	IsSuccess() bool
	GetBeginTime() int64
	GetEndTime() int64
	GetSubResults() []interface{}
}

//acquireResult generates root result.
//just call one time on the main func and generate sub result using parent.addSub()
func acquireResult(name string) *Result {
	result := &Result{}
	result.Name = name
	result.RequestHeader = map[string]string{}
	result.ResponseHeader = map[string]string{}
	result.ResponseCode = 200
	result.Success = true
	result.BeginTime = time.Now().UnixMilli()
	result.EndTime = time.Now().UnixMilli()
	return result
}

type Result struct {
	Name           string
	Url            string
	Method         string
	RequestHeader  map[string]string
	RequestBody    string
	SentBytes      int
	ResponseCode   int
	ResponseHeader map[string]string
	ResponseBody   string
	ReceivedBytes  int
	FailureMessage string
	Success        bool
	BeginTime      int64
	EndTime        int64
	SubResults     []interface{}
	SubIndex       int
}

func (r *Result) GetName() string {
	return r.Name
}

func (r *Result) GetUrl() string {
	return r.Url
}

func (r *Result) GetMethod() string {
	return r.Method
}

func (r *Result) GetRequestHeader() map[string]string {
	return r.RequestHeader
}

func (r *Result) GetRequestBody() string {
	return r.RequestBody
}

func (r *Result) GetSentBytes() int {
	return r.SentBytes
}

func (r *Result) GetResponseCode() int {
	return r.ResponseCode
}

func (r *Result) GetResponseHeader() map[string]string {
	return r.ResponseHeader
}

func (r *Result) GetResponseBody() string {
	return r.ResponseBody
}

func (r *Result) GetReceivedBytes() int {
	return r.ReceivedBytes
}

func (r *Result) GetFailureMessage() string {
	return r.FailureMessage
}

func (r *Result) IsSuccess() bool {
	return r.Success
}

func (r *Result) GetBeginTime() int64 {
	return r.BeginTime
}

func (r *Result) GetEndTime() int64 {
	return r.EndTime
}

func (r *Result) GetSubResults() []interface{} {
	return r.SubResults
}

//begin records begin time, do not forget call this function to update
func (r *Result) begin() {
	r.BeginTime = time.Now().UnixMilli()
}

//end records end time, do not forget call this function to update
func (r *Result) end() {
	r.EndTime = time.Now().UnixMilli()
}

//addSub adds sub result to parent, call this function adding sub result always.
//if name is not empty, renaming will be disabled
func (r *Result) addSub(name string) *Result {
	if name == "" {
		name = fmt.Sprintf("%s-%d", r.Name, r.SubIndex)
		r.SubIndex++
	} else {
		name = fmt.Sprintf("%s-%s", r.Name, name)
	}
	sub := acquireResult(name)
	r.SubResults = append(r.SubResults, sub)
	return sub
}
表2 IResultV1接口结构说明表

名称

含义

补充说明

Name

请求名称

根result需使用内置参数__name设置。

Url

请求地址

-

Method

方法

用于HTTP的POST,GET等。

RequestHeader

请求头

用于HTTP。

RequestBody

请求数据

建议不要记录请求较大的数据,例如上传文件,仅记录摘要即可。

SentBytes

发送字节数

-

ResponseCode

响应码

记录响应状态,可以用于HTTP状态码或者自定义状态码,用于报告统计响应状态数量用于分析。

响应码范围:[100,599]

ResponseHeader

响应头

用于HTTP。

ResponseBody

响应数据

建议不要记录响应较大的数据,例如下载文件,仅记录摘要即可。

ReceivedBytes

接收字节数

-

FailureMessage

失败信息

-

Success

是否成功

-

BeginTime

请求开始时间

单位:毫秒

EndTime

请求结束时间

单位:毫秒

SubResults

子请求

当自定义请求需要多个子请求共同完成,需要使用该字段记录各个子请求执行情况。

SubIndex

子请求索引

当使用父Result的addSub方法生成子Result时,如果不为子Result自定义名称,将使用该字段自增产生索引用于生成子Result名称。

分享:

    相关文档

    相关产品