Gin、Go HTTP库、ReverseProxy、Nginx代理性能压测

Gin、Go HTTP库、ReverseProxy、Nginx代理性能压测,第1张

笔记本(英特尔 i7 8700HQ),分配内存4G, 4核CPU虚拟机进行试验

1、Gin
func main() {
	router := gin.New()
	router.GET("/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{"message": "pong"})
	})
	server := &http.Server{
		Addr:         addr,
		WriteTimeout: time.Second * 3,
		Handler:      router,
	}
	log.Fatal(server.ListenAndServe())
}

c10k+100w

2、Go HTTP库
var addr = "127.0.0.1:2003"
func main() {
	log.Println("Starting httpserver at " + addr)
	mux := http.NewServeMux()
	mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
		upath := fmt.Sprintf("http://%s%s\n", r.Addr, req.URL.Path)
		realIP := fmt.Sprintf("RemoteAddr=%s,X-Forwarded-For=%v,X-Real-Ip=%v\n", req.RemoteAddr, req.Header.Get("X-Forwarded-For"), req.Header.Get("X-Real-Ip"))
		header := fmt.Sprintf("headers =%v\n", req.Header)
		io.WriteString(w, upath)
		io.WriteString(w, realIP)
		io.WriteString(w, header)
	})
	server := &http.Server{
		Addr:         addr,
		WriteTimeout: time.Second * 3,
		Handler:      mux,
	}
	log.Fatal(server.ListenAndServe())
}

3、ReverseProxy
var realAddr, proxyAddr = "http://127.0.0.1:2003", "127.0.0.1:8080"
func main() {
	backend, _ := url.Parse(realAddr)
	server := http.Server{
		Addr:         proxyAddr,
		WriteTimeout: time.Second * 3,
		Handler:      httputil.NewSingleHostReverseProxy(backend),
	}
	log.Fatal(server.ListenAndServe())
}

4、Nginx代理
user  root;
worker_processes  16;
events {
    worker_connections 65535 ;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       20030;
        server_name  localhost;

        location / {
            proxy_pass http://127.0.0.1:2003/;
        }
    }
}

5、汇总表格
方式类别QPStotaltime
http服务c10k+100wgin10444100w95.741s
http服务线程30、600个连接持续30秒gin60684183w30.10s
http服务线程30、600个连接持续30秒go http44583134w30.10s
http代理线程30、600个连接持续30秒ReverseProxy2731382.2w30.10s
http代理线程30、600个连接持续30秒Nginx1516245.6w30.10s

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/996005.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存