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

gnet 单核和多核配置的性能问题 #84

Closed
charlesgreat opened this issue May 7, 2020 · 16 comments
Closed

gnet 单核和多核配置的性能问题 #84

charlesgreat opened this issue May 7, 2020 · 16 comments
Assignees
Labels
help wanted Extra attention is needed invalid This doesn't seem right question Further information is requested stale waiting for response waiting for the response from commenter
Milestone

Comments

@charlesgreat
Copy link

多核linux和mac下配置NumEventLoop多了反而没有没有配置1的性能高,然后用pprof查看线程不管配置1还是4,都是只有一个线程(多个协程)。
怎样能让gnet确实启动多个线程呢?

@charlesgreat charlesgreat added help wanted Extra attention is needed question Further information is requested labels May 7, 2020
@panjf2000
Copy link
Owner

你是怎么看线程数量的?具体步骤贴一下?什么输出?

@panjf2000 panjf2000 added the waiting for response waiting for the response from commenter label May 7, 2020
@panjf2000
Copy link
Owner

panjf2000 commented May 8, 2020

我用 GODEBUG=schedtrace=1000 进行调度器追踪之后发现是有启动多个 OS 线程的,我想应该是你的设置问题吧。

SCHED 0ms: gomaxprocs=4 idleprocs=1 threads=5 spinningthreads=1 idlethreads=0 runqueue=0 [1 0 0 0]
# github.com/panjf2000/gnet
SCHED 0ms: gomaxprocs=4 idleprocs=1 threads=4 spinningthreads=1 idlethreads=0 runqueue=0 [1 0 0 0]
# command-line-arguments
SCHED 0ms: gomaxprocs=4 idleprocs=1 threads=4 spinningthreads=1 idlethreads=0 runqueue=0 [1 0 0 0]
# command-line-arguments
SCHED 0ms: gomaxprocs=4 idleprocs=2 threads=3 spinningthreads=1 idlethreads=0 runqueue=0 [0 0 0 0]
SCHED 1002ms: gomaxprocs=4 idleprocs=2 threads=12 spinningthreads=1 idlethreads=5 runqueue=0 [0 0 0 0]
SCHED 0ms: gomaxprocs=4 idleprocs=2 threads=4 spinningthreads=1 idlethreads=0 runqueue=0 [0 0 0 0]
2020/05/08 12:20:14 HTTP server is listening on [::]:8080 (multi-cores: false, loops: 4)
SCHED 2012ms: gomaxprocs=4 idleprocs=4 threads=12 spinningthreads=0 idlethreads=5 runqueue=0 [0 0 0 0]
SCHED 1001ms: gomaxprocs=4 idleprocs=4 threads=11 spinningthreads=0 idlethreads=1 runqueue=0 [0 0 0 0]
SCHED 3014ms: gomaxprocs=4 idleprocs=4 threads=12 spinningthreads=0 idlethreads=5 runqueue=0 [0 0 0 0]
SCHED 2002ms: gomaxprocs=4 idleprocs=4 threads=11 spinningthreads=0 idlethreads=1 runqueue=0 [0 0 0 0]
SCHED 4018ms: gomaxprocs=4 idleprocs=4 threads=12 spinningthreads=0 idlethreads=5 runqueue=0 [0 0 0 0]
SCHED 3002ms: gomaxprocs=4 idleprocs=4 threads=11 spinningthreads=0 idlethreads=1 runqueue=0 [0 0 0 0]
SCHED 5021ms: gomaxprocs=4 idleprocs=4 threads=12 spinningthreads=0 idlethreads=5 runqueue=0 [0 0 0 0]
SCHED 4014ms: gomaxprocs=4 idleprocs=4 threads=11 spinningthreads=0 idlethreads=1 runqueue=0 [0 0 0 0]
SCHED 6027ms: gomaxprocs=4 idleprocs=4 threads=12 spinningthreads=0 idlethreads=5 runqueue=0 [0 0 0 0]
SCHED 5019ms: gomaxprocs=4 idleprocs=4 threads=11 spinningthreads=0 idlethreads=1 runqueue=0 [0 0 0 0]
SCHED 7036ms: gomaxprocs=4 idleprocs=4 threads=12 spinningthreads=0 idlethreads=5 runqueue=0 [0 0 0 0]
SCHED 6023ms: gomaxprocs=4 idleprocs=4 threads=11 spinningthreads=0 idlethreads=1 runqueue=0 [0 0 0 0]
SCHED 8042ms: gomaxprocs=4 idleprocs=4 threads=12 spinningthreads=0 idlethreads=5 runqueue=0 [0 0 0 0]
SCHED 7023ms: gomaxprocs=4 idleprocs=4 threads=11 spinningthreads=0 idlethreads=1 runqueue=0 [0 0 0 0]
SCHED 9045ms: gomaxprocs=4 idleprocs=4 threads=12 spinningthreads=0 idlethreads=5 runqueue=0 [0 0 0 0]
SCHED 8033ms: gomaxprocs=4 idleprocs=4 threads=11 spinningthreads=0 idlethreads=1 runqueue=0 [0 0 0 0]
SCHED 10055ms: gomaxprocs=4 idleprocs=4 threads=12 spinningthreads=0 idlethreads=5 runqueue=0 [0 0 0 0]
SCHED 9038ms: gomaxprocs=4 idleprocs=4 threads=11 spinningthreads=0 idlethreads=1 runqueue=0 [0 0 0 0]
SCHED 11059ms: gomaxprocs=4 idleprocs=4 threads=12 spinningthreads=0 idlethreads=5 runqueue=0 [0 0 0 0]

@panjf2000 panjf2000 added the invalid This doesn't seem right label May 8, 2020
@charlesgreat
Copy link
Author

我是用如下方式测试的, 不知道为啥loops多了之后,TPS反而慢了
ab -c 100 -n 1000000 -k http://127.0.0.1:50007/index.html

(multi-cores: true, loops: 1) 的 TPS大概在200K
(multi-cores: true, loops: 2) 的 TPS大概是175K

@panjf2000
Copy link
Owner

贴一下 server 的代码我看下?

@charlesgreat
Copy link
Author

charlesgreat commented May 10, 2020

package main

import (
	"bytes"
	"flag"
	"fmt"
	"github.com/panjf2000/gnet"
	"log"
	"net/http"
)


type httpServer struct {
	*gnet.EventServer
}

var rtn_close="HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Type: text/html;charset=utf-8\r\nContent-Length:2\r\n\r\nok "
var rtn="HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nContent-Type: text/html;charset=utf-8\r\nContent-Length:2\r\n\r\nok "


func (hs *httpServer) React(frame []byte, c gnet.Conn) (out []byte, action gnet.Action) {
	if c.Context() != nil {
		// bad thing happened
		action = gnet.Close
		return
	}

	if bytes.Contains(frame, []byte("Keep-Alive")) || bytes.Contains(frame, []byte("keep-alive")) {
		out = append(out, rtn...)
	} else {
		out = append(out, rtn_close...)
		action = gnet.Close
	}

	return
}

func (hs *httpServer) OnInitComplete(srv gnet.Server) (action gnet.Action) {
	log.Printf("HTTP server is listening on %s (multi-cores: %t, loops: %d)\n",
		srv.Addr.String(), srv.Multicore, srv.NumEventLoop)
	return
}

func main() {

	var port int
	var NumEventLoop int

	// Example command: go run http.go --port 8080
	flag.IntVar(&port, "port", 50007, "server port")
	flag.IntVar(&NumEventLoop, "NumEventLoop", 4, "num loops")
	flag.Parse()

	http := new(httpServer)

	// Start serving!
	log.Fatal(gnet.Serve(http, fmt.Sprintf("tcp://:%d", port), gnet.WithNumEventLoop(NumEventLoop), gnet.WithMulticore(true)))
}

@panjf2000
Copy link
Owner

抱歉,回复晚了。
首先,NumEventLoopMulticore 不用两个都设置,前者会覆盖后者的。不过单看你这个代码,我看不出什么问题,你有没有试过继续增加 NumEventLoop ? 增加后性能有没有提升?

@charlesgreat

@guonaihong
Copy link

guonaihong commented May 22, 2020

ab 是单核心测试工具,试下wrk呢。我猜和客户端有关系,客户端只能使用一个物理核心的算力,服务端加两个核心,benchmark数据也是上不去的。

@charlesgreat
Copy link
Author

ab 是单核心测试工具,试下wrk呢。我猜和客户端有关系,客户端只能使用一个物理核心的算力,服务端加两个核心,benchmark数据也是上不去的。

上不去也不应该下降吧

@panjf2000
Copy link
Owner

可能是测试结果波动?多测几次也是这样吗?或者试试只设置 gnet.WithMulticore(true) 呢?

@panjf2000 panjf2000 changed the title 多核linux和mac下配置NumEventLoop多了反而没有没有配置1的性能高 gnet 多核配置和性能问题 May 25, 2020
@panjf2000 panjf2000 changed the title gnet 多核配置和性能问题 gnet 单核和多核配置的性能问题 May 25, 2020
@cctse
Copy link

cctse commented Jun 2, 2020

通过下面设置单核真的比默认开启多核性能高
runtime.GOMAXPROCS(1)

@panjf2000

@charlesgreat
Copy link
Author

测试代码业务处理很快,瓶颈在I/0, 启动多核后也无法解决I/0的瓶颈
如果业务代码加点耗时的话,配置多核会比单核快

@ballontt
Copy link

测试代码业务处理很快,瓶颈在I/0, 启动多核后也无法解决I/0的瓶颈
如果业务代码加点耗时的话,配置多核会比单核快

请问这个问题得到了验证吗

@lomoye
Copy link

lomoye commented Jul 15, 2021

我用wrk本地单机测试,同样有这个问题,服务端代码跑的是官网上提供的那个http的例子。
gnet.WithMulticore(multicore)这个multicore设置为false并发可以达到90000左右,设为true只有80000多一点。。
image

开启多核后:
image

@panjf2000 panjf2000 added this to the Long term milestone Jan 3, 2022
@someview
Copy link

someview commented Aug 8, 2023

我们利用gnet实现的ws框架,性能4核的时候是1核的2倍多一点,很好奇这个cpu 亲和性是怎么做的

Copy link

This issue is marked as stale because it has been open for 30 days with no activity.

You should take one of the following actions:

  • Manually close this issue if it is no longer relevant
  • Comment if you have more information to share

This issue will be automatically closed in 7 days if no further activity occurs.

@github-actions github-actions bot added the stale label Sep 26, 2024
Copy link

github-actions bot commented Oct 4, 2024

This issue was closed because it has been inactive for 7 days since being marked as stale.

If you believe this is a false alarm, please leave a comment for it or open a new issue, you can also reopen this issue directly if you have permission.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed invalid This doesn't seem right question Further information is requested stale waiting for response waiting for the response from commenter
Projects
None yet
Development

No branches or pull requests

7 participants