代码之家  ›  专栏  ›  技术社区  ›  Daniel Viglione

异步路由的处理时间是同步路由的两倍

  •  0
  • Daniel Viglione  · 技术社区  · 6 年前

    我有一个很小的NodeJS应用程序,我正在用apachebenchmark(ab)进行测试。有两条路线。一种是异步(非阻塞),另一种是同步的(阻塞)。

    因为同步路由会阻止任何其他工作,从而阻止对服务器的任何其他请求,直到服务器完成读取大文件为止

    由于异步路由不会阻塞,而是将工作添加到节点内部事件队列中进行处理,并同时处理其他请求,

    然而,ab对阻断路径显示了以下结果:

    $ ab -c 10 -t 5 "http://0.0.0.0:8080/sync"
    
    Document Path:          /sync
    Document Length:        62 bytes
    
    Concurrency Level:      10
    Time taken for tests:   5.096 seconds
    Complete requests:      221
    Failed requests:        0
    Total transferred:      61870 bytes
    HTML transferred:       14260 bytes
    Requests per second:    43.37 [#/sec] (mean)
    Time per request:       230.571 [ms] (mean)
    Time per request:       23.057 [ms] (mean, across all concurrent requests)
    Transfer rate:          11.86 [Kbytes/sec] received
    
    Connection Times (ms)
                  min  mean[+/-sd] median   max
    Connect:        0    0   0.0      0       0
    Processing:   220  221   1.1    221     226
    Waiting:       23  122  63.6    123     225
    Total:        220  222   1.1    221     226
    

    ab显示了以下非阻塞路径的结果:

    $ ab -c 10 -t 5 "http://0.0.0.0:8080/async"
    
    Document Path:          /async
    Document Length:        62 bytes
    
    Concurrency Level:      10
    Time taken for tests:   5.040 seconds
    Complete requests:      134
    Failed requests:        0
    Total transferred:      36046 bytes
    HTML transferred:       8308 bytes
    Requests per second:    26.59 [#/sec] (mean)
    Time per request:       376.129 [ms] (mean)
    Time per request:       37.613 [ms] (mean, across all concurrent requests)
    Transfer rate:          6.98 [Kbytes/sec] received
    
    Connection Times (ms)
                  min  mean[+/-sd] median   max
    Connect:        0    0   0.0      0       0
    Processing:   252  362  27.3    366     475
    Waiting:      198  342  34.6    347     475
    Total:        253  362  27.3    366     475
    

    !

    import express from 'express';
    const app = express();
    import fs from 'fs';
    const fsp = fs.promises;
    const HOST = '0.0.0.0';
    const PORT = 8080;
    
    app.get('/async', async (req, res) => {
        const data = await fsp.readFile('data/file.csv');
    
        res.send({
            statusCode: 200,
            body: { 
                resp: data.toString('utf8').slice(0,10),
                success: true
            }
        })
    });
    
    app.get('/sync', (req, res) => {
        const data = fs.readFileSync('data/file.csv');
    
        res.send({
            statusCode: 200,
            body: { 
                resp: data.toString('utf8').slice(0,10),
                success: true
            }
        })
    });
    
    app.listen(PORT, HOST, () => {
        console.log(
          `Server ready at http://${HOST}:${PORT}`);
    });
    

    项目可在此处进行基准测试:

    https://github.com/lovefamilychildrenhappiness/blockingvsnonblocking

    0 回复  |  直到 6 年前