具有2个CPU的Go服务器

huangapple go评论92阅读模式
英文:

Go server with 2 cpus

问题

我正在构建一个使用GO语言编写的GRPC服务器,并且想要提高其性能。我已经增加了机器的配置,并且安装了2个CPU以提升性能,但是我注意到我的服务器并没有使用所有的CPU核心,而且我找不到解决方法(我已经运行了一些测试来让服务器更加努力工作)。看起来我的服务器只使用了一个CPU,而另一个CPU没有使用。我的负载平均值保持在1以上,这意味着我的服务器有很多请求,但是当我在netdata中查看性能时,只有一个CPU在工作。

这是我的GRPC代码:

package main

import (
	"fmt"
	"runtime"
	"log"
	"net"
	"google.golang.org/grpc"
	"golang.org/x/net/context"
	"google.golang.org/grpc/reflection"
	pb "teste/prototeste"
)

func (s *server) Test(ctx context.Context, n *pb.TestRequest) (*pb.TestReply, error) {
	return &pb.TestReply{Message: n.Name}, nil
}

type server struct{}

const (
	port = ":50051"
)

func main() {
	numOfCores := runtime.NumCPU()
	fmt.Println(numOfCores)
	runtime.GOMAXPROCS(numOfCores)
	lis, err := net.Listen("tcp", port)
	if err != nil {
		log.Fatalf("failed to listen: %v", err)
	}
	s := grpc.NewServer()
	pb.RegisterGreeterServer(s, &server{})
	// Register reflection service on gRPC server.
	reflection.Register(s)

	s.Serve(lis)
}

请注意,在Serve函数中有一个goroutine,允许服务器并行接受多个请求。

GO中的GRPC API参考

这是我的CPU使用情况

有什么提示可以解决这个问题,让我的服务器使用两个CPU?

英文:

I'm building a GRPC server in GO and I want increase its performance. I've increased my machine and put 2CPUS in order to make it better but I noticed that my server doesn't use all cpu cores and I couldn't find a way to fix it (I've run some tests to make the server worker harder). It seems like my server uses 1 cpu a lot and the other not. My load average stay above 1 what means that my server is full of requests but when I see the performance in netdata there's only one CPU working.

This is my grpc code:

package main
import (
    "fmt"
    "runtime"
    "log"
    "net"
    "google.golang.org/grpc"
    "golang.org/x/net/context"
    "google.golang.org/grpc/reflection"
    pb "teste/prototeste"

)
func (s *server) Test(ctx context.Context, n *pb.TestRequest)      (*pb.TestReply, error){
    return &pb.TestReply{Message: n.Name}, nil
}

type server struct{}

const (
    port = ":50051"
)

func main(){

	numOfCores := runtime.NumCPU()
	fmt.Println(numOfCores)
	runtime.GOMAXPROCS(numOfCores)
	lis, err := net.Listen("tcp", port)
	if err != nil {
		log.Fatalf("failed to listen: %v", err)
	}
	s := grpc.NewServer()
	pb.RegisterGreeterServer(s, &server{})
	// Register reflection service on gRPC server.
	reflection.Register(s)

	s.Serve(lis)

}

Notice that in Serve function there's a goroutine that allow the server accept multiple requests in parallel.

API reference grpc in go

This is my cpu usage

Any tips to how to solve this and make my server use 2 cpus?

答案1

得分: 1

在gRPC中,每个服务器处理程序在自己的goroutine中运行。然后由Go调度器决定将它们安排在哪个CPU上。根据服务器处理程序的操作、系统负载等情况,它们有可能都被安排在同一个CPU上。

英文:

In gRPC, each server handler runs in its own goroutine. From there it is up to the Go scheduler to determine what CPUs to schedule them on. Depending on what your server handlers are doing, system load, etc, it's possible they could all end up scheduled on the same CPU.

huangapple
  • 本文由 发表于 2017年3月8日 04:20:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/42657556.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定