QPS/RT/并发的关系

By | 29 1 月, 2025

取自:https://wx.zsxq.com/group/15552551584552/topic/814811112852482

一个请求如果需要消耗 10ms,但是其中 9 毫秒是 CPU 调用查 DB,实际这个请求需要消耗本机 CPU 1ms,也就是单核 QPS = 1000ms/1ms=1000,8 核机器理论支撑最大 QPS 为 8000。

如果用单线程去压这个服务,RT(Reaction Time)是 10ms(1ms+9ms DB),那么 QPS 是 100,但是其中 CPU 有 90% 的时间在等待 DB 返回,相当于 90% 的时间在休息,那么需要用 10 个并发一起来压,刚好填满休息的 9ms,这样让 CPU 跑到 100%,RT 还是 10ms,但是 QPS 会提升到 1000。

如果 10 个并发,QPS 提升到 1000 后,继续加并发到 20,因为 CPU 负载已经 100% 了,你可以理解这个时候前 10 个并发跑着的时候,后面 10 个并发都要等 10ms,然后再轮到自己,所以 RT=10ms(等待CPU调度时间)+1ms(CPU 实际处理时间)+9ms(DB 处理时间)=20ms,QPS 仍然是 1000。

几个扩展场景:

场景:

还是上面的例子,如果 9ms 访问 DB 改成需要访问两次 DB,分别为 4ms、5ms,那么我们可以将代码重构,两次 DB 访问不用串行改成并行(业务允许这么做),那么总 RT=1ms(本机 CPU 耗时)+max(4,5)ms = 6ms。RT 从 10ms 优化到了 6ms,减少了 40%,是个很了不起的改进,如果还是用 10 并发来压那么总 QPS 变了吗?

没有。CPU 依然只能够执行 1000 个请求每秒,因为每个请求需要 1ms 的 CPU 时间,这是硬瓶颈。并且如果未来进一步提高并发,QPS 不会变化,RT 会继续增大。

最简单计算 CPU time 和 CPU wait time 的方法

一个并发跑的时候,如果 RT=1000ms,一次业务请求调用了 MySQL 10 次,每次 10ms 总共 100ms;

调用了服务 B 2 次分别为 100/400ms,2 次总共 500ms,那么对于这个业务 CPU time=1000(总RT)-100(MySQL)-500(服务B)=400ms,wait time=1000-400=600ms

88 thoughts on “QPS/RT/并发的关系

  1. Pingback: generique kamagra en ligne afin

  2. Pingback: cheapest buy enclomiphene generic how effective

  3. Pingback: discount androxal generic canada

  4. Pingback: discount dutasteride buy generic

  5. Pingback: flexeril cyclobenzaprine cheap fed ex delivery

  6. Pingback: how to order gabapentin usa pharmacy

  7. Pingback: how to order fildena pill

  8. Pingback: order itraconazole cheap uk buy purchase

  9. Pingback: ordering staxyn cheap online canada

  10. Pingback: cheapest buy avodart uk over the counter

  11. Pingback: No perscription required rifaximin

  12. Pingback: discount xifaxan generic when available

  13. Pingback: kamagra levná cena

  14. Lucille

    slots best uk, casino offers no deposit usa and canadian poker, or
    casino pay by mobile usa

    Feel free to visit my website :: russian roulette computer game (Lucille)

    Reply
  15. Vivien

    european online casinos real money usa players free spins, $5 minimum deposit
    casino new zealand 2021 and 21 dukes best casino in natchez
    ms, Vivien, review, or 2021 usa
    online casino no deposit bonus codes

    Reply
  16. Rosalinda

    paypal gambling sites canada, harrah’s casino smoky mountains (Rosalinda) online uk paypal and whats the largest casino in australia,
    or united statesn casinos in california

    Reply
  17. Cheryl

    free casino cash no deposit usa, united statesn poker and age of gods slots uk, or the blackjacks homepage australia

    My web page … are there any casinos in cyprus – Cheryl

    Reply
  18. Emilio

    online gambling ads usa, spin usa casino and how to hack canadian pokies,
    or roulette online casino australia

    Look into my web page … best craps pc game (Emilio)

    Reply
  19. Kathrin

    united statesn roulette builders, best new zealandn online
    real money pokies and royal panda casino canada, or best online casino sites for real money canada

    my web-site – add free blackjack (Kathrin)

    Reply

回复 sportwetten geld zurück erfahrungen 取消回复

您的邮箱地址不会被公开。 必填项已用 * 标注