我们的服务器无法处理超过20个请求/秒 [英] Our Server can't handle more than 20 requests/second

查看:124
本文介绍了我们的服务器无法处理超过20个请求/秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,经过3个月的努力发展和发展,将公司API从PHP切换到Go我发现我们的Go服务器无法处理超过20个请求/秒。

所以基本上我们的API如何工作:


  • 接受请求

  • 验证请求

  • 使用MYSQL从数据库获取数据

  • put's Map

  • 中的数据以JSON格式将其发送回客户端



所以在写了大约30个API之后,我决定采用它来看看它在负载测试下的性能如何。



测试1: ab -n 1 -c 1 http:// localhost:8000 / sales / report ,结果为每次请求的时间:72.623 [ms](mean)。

测试2: ab -n 100 -c 100 http:// localhost:8000 / sales / report 结果是每次请求的时间:4548.155 [ms](mean)(没有MYSQL错误)。

在第二次测试中,数字是如何突然从72.623增加到4548 ms?我们预计每天有数千个请求,所以我们需要在我们最终发布之前解决这个问题。当我看到数字时,我很惊讶;我简直不敢相信。我知道GO可以做得更好。



有关服务器和设置的基本信息:




  • 使用GO 1.5

  • 16GB RAM

  • GOMAXPROCS正在使用所有8个内核

  • db.SetMaxIdleConns(1000)

  • db.SetMaxOpenConns(1000)(也确保我们使用的是
    连接池)
  • 通过unix套接字连接到MYSQL
  • 系统在Ubuntu下运行


我们正在使用的外部库:


  • github.com/go-sql-driver/mysql


  • github.com/gorilla/mux


  • github.com/elgs/gosqljson




有什么想法可能导致这种情况? 。我看了一下这个帖子,但没有工作,因为我上面提到我从来没有得到任何MYSQL错误。您可以提前感谢您提供的任何帮助。

解决方案

您的帖子没有足够的信息来解释为什么您的程序是但是我认为这个问题本身就值得回答:


这个数字是如何突然从72.623在第二次测试中为4548毫秒?


在第一次测试中,您做了一个单一的请求( -n 1 )。在第二次测试中,您同时执行了100个请求( -c 100 -n 100 )。



您提到您的程序与外部数据库进行通信,您的程序必须等待该资源作出响应。您是否了解数据库在同时发送1,000个请求时的性能?你没有提到这一点。 Go可以毫不费力地处理数百个并发请求,但这取决于你在做什么以及如何做。如果你的程序不能完成请求,它们将会堆积起来,导致高延迟。



你告诉我们的那些测试对于理解你的服务器在正常情况下的表现如何,你会说每天有成千上万的请求(这不是很具体,但我会认为是几秒钟)。那么看看 -c 4 -n 1000 或者一些在更长时间内运行服务器的事情会更加有趣,其中有许多并发请求更像是你期望得到的。


So after 3 months of hard work in developing & switching the company API from PHP to Go I found out that our Go server can't handle more than 20 req/second.

So basically how our API works:

  • takes in a request
  • validates the request
  • fetches the data from the DB using MYSQL
  • put's the data in a Map
  • send's it back to the Client in a JSON format

So after writing about 30 APIs I decided to take it for a spin and see how it performance under load test.

Test 1: ab -n 1 -c 1 http://localhost:8000/sales/report the results are "Time per request: 72.623 [ms] (mean)" .

Test 2: ab -n 100 -c 100 http://localhost:8000/sales/report the results are "Time per request: 4548.155 [ms] (mean)" (No MYSQL errors).

How did the number suddenly spike from 72.623 to 4548 ms in the second test?. We expect thousands of requests per day so I need to solve this issue before we finally release it . I was surprised when I saw the numbers ; I couldn't believe it. I know GO can do much better.

So basic info about the server and settings:

  • Using GO 1.5
  • 16GB RAM
  • GOMAXPROCS is using all 8 Cores
  • db.SetMaxIdleConns(1000)
  • db.SetMaxOpenConns(1000) (also made sure we are using pool of connections)
  • Connecting to MYSQL through unix socket
  • System is running under Ubuntu

External libraries that we are using:

  • github.com/go-sql-driver/mysql

  • github.com/gorilla/mux

  • github.com/elgs/gosqljson

Any ideas what might be causing this? . I took a look at this post but didn't work as I mentioned above I never got any MYSQL error. Thank you in advance for any help you can provide.

解决方案

Your post doesn't have enough information to address why your program is not performing how you expect, but I think this question alone is worth an answer:

How did the number suddenly spike from 72.623 to 4548 ms in the second test?

In your first test, you did one single request (-n 1). In your second test, you did 100 requests in flight simultaneously (-c 100 -n 100).

You mention that your program communicates to an external database, your program has to wait for that resource to respond. Do you understand how your database performs when you send it 1,000 requests simultaneously? You made no mention of this. Go can certainly handle many hundreds of concurrent requests a second without breaking a sweat, but it depends what you're doing and how you're doing it. If your program can't complete requests as fast as they are coming in, they will pile up, leading to a high latency.

Neither of those tests you told us about are useful to understand how your server performs under "normal" circumstances - which you said would be "thousands of requests per day" (which isn't very specific, but I'll take to mean, "a few a second"). Then it would be much more interesting to look at -c 4 -n 1000, or something that exercises the server over a longer period of time, with a number of concurrent requests which is more like what you expect to get.

这篇关于我们的服务器无法处理超过20个请求/秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆