Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在未设置解码器时,优化效率 #155

Closed
guonaihong opened this issue Jan 5, 2020 · 0 comments
Closed

在未设置解码器时,优化效率 #155

guonaihong opened this issue Jan 5, 2020 · 0 comments

Comments

@guonaihong
Copy link
Owner

guonaihong commented Jan 5, 2020

测试代码如下

package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/guonaihong/gout"
	"net/http"
	"sync"
	"time"
)

func server() {
	router := gin.New()
	router.POST("/colorjson", func(c *gin.Context) {
		c.String(200, string(make([]byte, 2*1024)))
	})

	router.Run()
}

const nthread = 450
const total = 10000

func useMap() {
	c := &http.Client{
		Transport: &http.Transport{
			MaxIdleConnsPerHost: 500,
		},
	}

	var wg sync.WaitGroup
	wg.Add(nthread)
	now := time.Now()
	defer func() {
		wg.Wait()
		fmt.Printf("total:%v\n", time.Now().Sub(now))
	}()

	done := make(chan struct{})
	go func() {
		for i := 0; i < total; i++ {
			done <- struct{}{}
		}

		close(done)
	}()

	for i := 0; i < nthread; i++ {
		go func() {
			defer wg.Done()

			for range done {
				err := gout.New(c).POST(":8080/colorjson").
					//Debug(true).
					SetJSON(gout.H{"str": "foo",
						"num":   100,
						"bool":  false,
						"null":  nil,
						"array": gout.A{"foo", "bar", "baz"},
						"obj":   gout.H{"a": 1, "b": 2},
					}).
					Do()

				if err != nil {
					fmt.Printf("err = %v\n", err)
				}
			}
		}()
	}
}

func main() {
	go server()

	time.Sleep(time.Millisecond * 200)

	useMap()
}
@guonaihong guonaihong changed the title 不设置解码器,吃掉resp.Body里面的内容 如果不设置解码器,清空resp.Body里面的内容 Jan 5, 2020
@guonaihong guonaihong changed the title 如果不设置解码器,清空resp.Body里面的内容 在未设置编码器时,优化效率 Jan 15, 2020
@guonaihong guonaihong changed the title 在未设置编码器时,优化效率 在未设置解码器时,优化效率 Jan 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant