Skip to content

Commit

Permalink
添加自动测试
Browse files Browse the repository at this point in the history
  • Loading branch information
6boris committed May 12, 2019
1 parent e4220cd commit e15d681
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .travis.yml
@@ -0,0 +1,18 @@
language: go

go:
- 1.11.x

# whitelist
branches:
only:
- master
# - stable

script:
- go get -t -v ./...
#- go vet ./...
- bash ./test.sh

after_success:
- bash <(curl -s https://codecov.io/bash)
101 changes: 101 additions & 0 deletions metrics_test.go
@@ -0,0 +1,101 @@
package gin_metrics

import (
"fmt"
"github.com/gin-gonic/gin"
"math/rand"
"net/http"
"strconv"
"sync"
"testing"
"time"
)

func TestDefault(t *testing.T) {
app := gin.New()
Default(app)

gin.SetMode(gin.DebugMode)

app.GET("rand_str", func(c *gin.Context) {
c.JSON(200, getRandomString(32))
})
app.GET("rand_int", func(c *gin.Context) {
c.JSON(200, rand.Intn(100000000000))
})
app.GET("time_now", func(c *gin.Context) {
c.JSON(200, time.Now())
})
app.GET("rand_sleep", func(c *gin.Context) {
time.Sleep(time.Duration(time.Duration(rand.Intn(1000)) * time.Millisecond))
c.JSON(200, time.Now())
})
for i := 0; i < 100; i++ {
app.GET(fmt.Sprint("rand_route", strconv.Itoa(i)), func(c *gin.Context) {
c.JSON(200, time.Now())
})
}

if err := app.Run("127.0.0.1:9000"); err != nil {
panic(err.Error())
}
}

func BenchmarkDefault(b *testing.B) {
b.N = 10000
var wg sync.WaitGroup
wg.Add(1)

for i := 0; i < 10; i++ {
go func() {
for i := 0; i < b.N; i++ {
Req("http://127.0.0.1:9000/rand_str")
Req("http://127.0.0.1:9000/rand_int")
Req("http://127.0.0.1:9000/time_now")
}
}()
}

go func() {
for i := 0; i < b.N; i++ {
Req("http://127.0.0.1:9000/rand_sleep")
}
}()
go func() {
for i := 0; i < b.N; i++ {
Req("http://127.0.0.1:9000/rand_route" + strconv.Itoa(rand.Intn(100)))
}
}()
time.Sleep(10 * time.Second)
//wg.Wait()

}

//生成随机字符串
func getRandomString(str_len int) string {
str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
bytes := []byte(str)
result := []byte{}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < str_len; i++ {
result = append(result, bytes[r.Intn(str_len)])
}
return string(result)
}

func Req(url string) {
time.Sleep(time.Duration(time.Duration(rand.Intn(100)) * time.Millisecond))

resp, err := http.Get(url)
if err != nil {
panic(err.Error())
}

defer resp.Body.Close()
//body, err := ioutil.ReadAll(resp.Body)
//if err != nil {
// panic(err.Error())
//}
//
//fmt.Println(string(body))
}
14 changes: 14 additions & 0 deletions test.sh
@@ -0,0 +1,14 @@
#!/usr/bin/env bash

set -e
echo "" > coverage.txt

for d in $(go list ./... | grep -v vendor); do
echo $d
go test -coverprofile=profile.out -covermode=atomic $d
if [ -f profile.out ]; then
cat profile.out >> coverage.txt
rm profile.out
fi
done

0 comments on commit e15d681

Please sign in to comment.