Skip to content

Instantly share code, notes, and snippets.

@linxGnu
Created November 28, 2018 10:35
Show Gist options
  • Save linxGnu/0511f18c768cbe8f3dc0061cd6a0b5fb to your computer and use it in GitHub Desktop.
Save linxGnu/0511f18c768cbe8f3dc0061cd6a0b5fb to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"sync"
"time"
"github.com/gin-gonic/gin"
)
const (
numRoutine = 100
numRequest = 100
)
func startServer() {
r := gin.New()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
"ab": "cd",
})
})
go r.Run() // listen and serve on 0.0.0.0:8080
}
func doGetWithDrainup() {
start := time.Now()
// multiple call
var wg sync.WaitGroup
for i := 0; i < numRoutine; i++ {
wg.Add(1)
go func() {
defer wg.Done()
client := http.DefaultClient
for j := 0; j < numRequest; j++ {
resp, err := client.Get("http://localhost:8080/ping")
if err != nil {
return
}
io.CopyN(ioutil.Discard, resp.Body, 512)
resp.Body.Close()
}
}()
}
wg.Wait()
fmt.Printf("With drainup: %.3f\n", time.Since(start).Seconds())
}
func doGetWithoutDrainup() {
start := time.Now()
// multiple call
var wg sync.WaitGroup
for i := 0; i < numRoutine; i++ {
wg.Add(1)
go func() {
defer wg.Done()
client := http.DefaultClient
for j := 0; j < numRequest; j++ {
resp, err := client.Get("http://localhost:8080/ping")
if err != nil {
return
}
resp.Body.Close()
}
}()
}
wg.Wait()
fmt.Printf("Without drainup: %.3f\n", time.Since(start).Seconds())
}
func main() {
startServer()
// wait server start
time.Sleep(time.Second)
doGetWithDrainup()
time.Sleep(time.Second)
doGetWithoutDrainup()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment