Suppose I have an api having two routes one is for saving the user and another one is for getting the user given below:-
router.go
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")
}
By hitting these url localhost:8080/api/v1/post
method:- POST
by Postman it will take the json data from postman and enters into the database and while getting the user url localhost:8080/api/v1/post/:id
method:- GET
by postman then it will return the user matching with the id entered. Now, I want to take load test(vegeta) these api's because I want to see that how many request it would be take in one second. I read this link and implement the program but I don't know how will I take load test of my api's separately.
Can anyone tell me that how will I take the load test of these two api's or any refrence?
Edited
main.go
package main
func main() {
NewRouter()
GetVegeta()
}
vegeta.go
// 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)
}
These files are in same folder name Template
. I'm running this whole folder with the command ./Template -users=10 GET https://localhost:8080/api/v1/customer | vegeta attack -rate=10 -duration=30s | vegeta report
by running this all api's run and when I will hit any api from the postman then it will give me the error of bad method: [GIN]
and encode: can't detect encoding of "stdin"
How will I solve this to make a report of my api's.
Thanks for your precious time!
Vegeta takes concurrency and time parameters and you can create report out of it.
you could use https://linux.die.net/man/1/siege and it allow you to test APIs and having report