简体   繁体   English

每秒增加数量响应

[英]increase number response per second

I have an android game that has 40,000 users online. 我有一个Android游戏,有40,000用户在线。 And each user send request to server every 5 second. 每个用户每5秒向服务器发送一次请求。

I write this code for test request: 我为测试请求编写了这段代码:

const express = require('express')  
const app = express()
const pg = require('pg')  
const conString = 'postgres://postgres:123456@localhost/dbtest'      

 app.get('/', function (req, res, next) { 

  pg.connect(conString, function (err, client, done) {
    if (err) {

      return next(err)
    }
    client.query('SELECT name, age FROM users limit 1;', [], function (err, result) {
      done()

      if (err) {

        return next(err)
      }

      res.json(result.rows)
    })
  })
})
app.listen(3000)

Demo 演示

And for test this code with 40,000 requests I write this ajax code: 为了测试这个代码有40,000个请求我写这个ajax代码:

for (var i = 0; i < 40000; i++) {
    var j = 1;
    $.ajax({
        url: "http://85.185.161.139:3001/",

        success: function(reponse) {
            var d = new Date();
            console.log(j++, d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds());
        }
    });
}

SERVER detail(I know this is poor) 服务器细节(我知道这很差) 在此输入图像描述

Questions: 问题:

  1. this code (node js)only response 200 requests per second! 这段代码(节点js)每秒仅响应200个请求!

  2. how can improve my code for increase number response per second? 如何提高我的代码每秒增加数量响应?

  3. this way(ajax) for simulate 40,000 online users is correct or not? 这种方式(ajax)模拟40,000个在线用户是否正确?

  4. if i use socket is better or not? 如果我使用套接字更好或不?

You should use Divide&Conquer algorithm for solving such problems. 您应该使用Divide&Conquer算法来解决此类问题。 Find the most resource inefficient operation and try to replace or reduce an amount of calls to it. 找到资源效率最低的操作,并尝试替换或减少对它的调用量。

The main problem that I see here is that server open new connection to database on each request which possibly takes most of the time and resources. 我在这里看到的主要问题是服务器在每个请求上打开与数据库的新连接,这可能占用大部分时间和资源。

I suggest to open connection when the server boots up and reuse it in requests. 我建议在服务器启动时打开连接并在请求中重用它。

const express = require('express')  
const app = express()
const pg = require('pg')  
const conString = 'postgres://postgres:123456@localhost/dbtest'      
const pgClient

pg.connect(conString, function (err, client, done) {
    if (err) {
        throw err
    }
    pgClient = client
})

app.get('/', function (req, res, next) { 
        pgClient.query('SELECT name, age FROM users limit 1;', [], function (err, result) {
            if (err) {
                return next(err)
            }

            res.json(result.rows)
        })
})
app.listen(3000)

For proper stress load testing better use specialized utilities such as ab from Apache. 对于正确的压力负载测试更好地使用专门的工具,如AB Apache的。 Finally, sockets are better for rapid, small data transfer but remember it has problems with scaling and in most cases became very inefficient at 10K+ simultaneous connections. 最后,套接字更适合快速,小型数据传输,但请记住它存在扩展问题,并且在大多数情况下,在10K +同时连接时变得非常低效。

EDIT: As @robertklep pointed out, better use client pooling in this case, and retrieve client from pool. 编辑:正如@robertklep指出的那样,在这种情况下更好地使用客户端池 ,并从池中检索客户端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM