假设我有一个具有两种路由的api,一种用于保存用户,另一种用于获取用户信息:-
router.go
new Promise
通过点击以下url package main
import (
"github.com/gin-gonic/gin"
"go-training/postAPI/controller"
)
type Route struct {
Name string
Method string
Pattern string
HandlerFunc func(*gin.Context)
}
type Routes []Route
var routes = Routes{
Route{"SaveUser", "POST", "/post", controller.SaveUser},
Route{"GetUser", "GET", "/post/:id", controller.GetUser},
}
func NewRouter() {
router := gin.Default()
v1 := router.Group("/api/v1")
for _, route := range routes {
switch route.Method {
case "GET":
v1.GET(route.Pattern, route.HandlerFunc)
case "POST":
v1.POST(route.Pattern, route.HandlerFunc)
case "PUT":
v1.PUT(route.Pattern, route.HandlerFunc)
case "DELETE":
v1.DELETE(route.Pattern, route.HandlerFunc)
default:
v1.GET(route.Pattern, func(c *gin.Context) {
c.JSON(200, gin.H{
"result": "Specify a valid http method with this route.",
})
})
}
}
router.Run(":8080")
}
方法:-Postman的localhost:8080/api/v1/post
,它将从postman中获取json数据并进入数据库,同时获取用户url POST
方法:-{ {1}}(邮递员)将返回与输入ID匹配的用户。现在,我想对这些api进行负载测试(vegeta),因为我想看看一秒钟内将要处理多少个请求。我读过this link并实现了程序,但我不知道如何分别进行api的负载测试。
谁能告诉我,我将如何对这两个api或任何引用进行负载测试?
已编辑
main.go
localhost:8080/api/v1/post/:id
vegeta.go
GET
这些文件位于相同的文件夹名称package main
func main() {
NewRouter()
GetVegeta()
}
中。我正在使用命令// Panic if there is an error
func check(err error) {
if err != nil {
panic(err)
}
}
func GetVegeta() {
var (
users int
)
// The Go random number generator source is deterministic, so we need to seed
// Configure our command line app
app := cli.NewApp()
app.Name = "Pokemon User Data Generator"
app.Usage = "generate a stream of test data for vegeta. Type 'pokemon help' for details"
// Add -users flag, which defaults to 5
app.Flags = []cli.Flag{
cli.IntFlag{
Name: "users",
},
}
// Our app's main action
app.Action = func(c *cli.Context) error {
// Combine verb and URL to a target for Vegeta
verb := c.Args().Get(0)
url := c.Args().Get(1)
target := fmt.Sprintf("%s %s", verb, url)
fmt.Println(verb)
fmt.Println(url)
fmt.Println(target)
if len(target) > 1 {
for i := 1; i < users; i++ {
fmt.Println(users)
}
} else {
// Return an error if we're missing the required command line arguments
return cli.NewExitError("You must specify the target in format 'VERB url'", 1)
}
return nil
}
app.Run(os.Args)
}
通过运行所有api的运行,当我从邮递员那里碰到任何api时,它将给我Template
和./Template -users=10 GET https://localhost:8080/api/v1/customer | vegeta attack -rate=10 -duration=30s | vegeta report
的错误报告我的api。
感谢您的宝贵时间!
答案 0 :(得分:-1)
Vegeta具有并发性和时间参数,您可以从中创建报告。
答案 1 :(得分:-1)
您可以使用 https://linux.die.net/man/1/siege,它可以让您测试API并生成报告