-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy path16d-benchmark-vs-ab.go
72 lines (60 loc) · 1.22 KB
/
16d-benchmark-vs-ab.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/guonaihong/gout"
"log"
"os"
"os/exec"
"time"
)
const (
benchCount = 100000
benchConcurrent = 30
)
func server() {
router := gin.New()
router.POST("/", func(c *gin.Context) {
c.String(200, "hello world:gout")
})
router.Run()
}
func runGout() {
fd, err := os.Open("../testdata/voice.pcm")
if err != nil {
fmt.Printf("%s\n", err)
return
}
defer fd.Close()
err = gout.
POST(":8080").
SetBody(fd). //设置请求body内容
Filter(). //打开过滤器
Bench(). //选择bench功能
Concurrent(benchConcurrent). //并发数
Number(benchCount). //压测次数
Do()
if err != nil {
fmt.Printf("%v\n", err)
}
}
// sudo apt install apache2-utils
var abCmd = fmt.Sprintf(`ab -c %d -n %d -p ../testdata/voice.pcm http://127.0.0.1:8080/`, benchConcurrent, benchCount)
func runAb() {
out, err := exec.Command("bash", "-c", abCmd).Output()
if err != nil {
log.Fatal("%s\n", err)
}
fmt.Printf("%s\n", out)
}
func main() {
go server()
time.Sleep(300 * time.Millisecond)
// 设为false,可看ab性能
startGout := true
if startGout {
runGout()
} else {
runAb()
}
}