Nodejs之springnodejs与express压力测试

Nodejs之springnodejs与express压力测试

端口 8888 是 springnodejs 端口 3000 是 express

第一次压 springnodejs

Server Hostname:        127.0.0.1
Server Port:            8888

Document Path: /ws/test Document Length: 20 bytes

Concurrency Level: 100 Time taken for tests: 22.406 seconds Complete requests: 20000 Failed requests: 0 Write errors: 0 Total transferred: 2700000 bytes HTML transferred: 400000 bytes Requests per second: 892.61 [#/sec] (mean) Time per request: 112.031 [ms] (mean) Time per request: 1.120 [ms] (mean, across all concurrent requests) Transfer rate: 117.68 [Kbytes/sec] received

Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 1.1 0 16 Processing: 16 112 9.2 109 234 Waiting: 16 76 8.6 78 188 Total: 16 112 9.2 109 234

Percentage of the requests served within a certain time (ms) 50% 109 66% 109 75% 109 80% 109 90% 125 95% 125 98% 125 99% 125 100% 234 (longest request)

第一次压 expressjs

Server Hostname:        127.0.0.1
Server Port:            3000

Document Path:          /test
Document Length:        25 bytes

Concurrency Level:      100
Time taken for tests:   5.953 seconds
Complete requests:      20000
Failed requests:        0
Write errors:           0
Total transferred:      4200000 bytes
HTML transferred:       500000 bytes
Requests per second:    3359.58 [#/sec] (mean)
Time per request:       29.766 [ms] (mean)
Time per request:       0.298 [ms] (mean, across all concurrent requests)
Transfer rate:          688.98 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   1.1      0      16
Processing:    16   30   6.3     31      63
Waiting:       16   20   7.1     16      47
Total:         16   30   6.3     31      63

Percentage of the requests served within a certain time (ms)
  50%     31
  66%     31
  75%     31
  80%     31
  90%     31
  95%     31
  98%     47
  99%     47
 100%     63 (longest request)

第二次压 expressjs

Server Software:
Server Hostname:        127.0.0.1
Server Port:            3000

Document Path:          /test
Document Length:        25 bytes

Concurrency Level:      100
Time taken for tests:   6.563 seconds
Complete requests:      20000
Failed requests:        0
Write errors:           0
Total transferred:      4200000 bytes
HTML transferred:       500000 bytes
Requests per second:    3047.62 [#/sec] (mean)
Time per request:       32.813 [ms] (mean)
Time per request:       0.328 [ms] (mean, across all concurrent requests)
Transfer rate:          625.00 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   1.0      0      16
Processing:    16   33  35.8     31     531
Waiting:       16   22  30.1     16     531
Total:         16   33  35.8     31     531

Percentage of the requests served within a certain time (ms)
  50%     31
  66%     31
  75%     31
  80%     31
  90%     31
  95%     31
  98%     47
  99%     47
 100%    531 (longest request)

以下我关闭了 debug 调试信息 springnodejs

Server Software:
Server Hostname:        127.0.0.1
Server Port:            8888

Document Path:          /ws/test
Document Length:        20 bytes

Concurrency Level:      100
Time taken for tests:   5.875 seconds
Complete requests:      20000
Failed requests:        0
Write errors:           0
Total transferred:      2700000 bytes
HTML transferred:       400000 bytes
Requests per second:    3404.26 [#/sec] (mean)
Time per request:       29.375 [ms] (mean)
Time per request:       0.294 [ms] (mean, across all concurrent requests)
Transfer rate:          448.80 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   1.1      0      16
Processing:     0   29   6.7     31      78
Waiting:        0   20   7.0     16      47
Total:          0   29   6.6     31      78

Percentage of the requests served within a certain time (ms)
  50%     31
  66%     31
  75%     31
  80%     31
  90%     31
  95%     31
  98%     47
  99%     47
 100%     78 (longest request)

再压一次 springnodejs

Server Hostname:        127.0.0.1
Server Port:            8888

Document Path:          /ws/test
Document Length:        20 bytes

Concurrency Level:      100
Time taken for tests:   5.734 seconds
Complete requests:      20000
Failed requests:        0
Write errors:           0
Total transferred:      2700000 bytes
HTML transferred:       400000 bytes
Requests per second:    3487.74 [#/sec] (mean)
Time per request:       28.672 [ms] (mean)
Time per request:       0.287 [ms] (mean, across all concurrent requests)
Transfer rate:          459.81 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   1.1      0      16
Processing:     0   29   6.5     31      47
Waiting:        0   19   6.7     16      31
Total:          0   29   6.5     31      47

Percentage of the requests served within a certain time (ms)
  50%     31
  66%     31
  75%     31
  80%     31
  90%     31
  95%     31
  98%     31
  99%     47
 100%     47 (longest request)

测试代码

var express = require('express');
var app = express();

app.get('/test', function(req, res){
  res.send({'key':'hello rest'});
});

app.listen(3000);

springnodejs

example.springnodejs.com

module.exports = {
	'get:/test':{
  		controller : function(){				 		
			return {'key':'hello rest'};			
 		}
	}
};

下面来大数据压力测试

module.exports = {
	cache : {},
	awake : function(){
		for(var i = 0 ;i < 5000000 ; i++){
			this.cache[i] = i;
		}
	},
'get:/test':{
	controller : function(){				 		
		return this.cache;			
	}
}

};

var express = require(‘express’);
var app = express();
var cache={};
for(var i = 0 ;i < 5000000 ; i++){
cache[i] = i;
}
app.get(’/test’, function(req, res){
res.send( cache);
});

app.listen(3000);

结果怎样?一起死掉

下面是压10W条记录

第一次 expressjs

Server Software:
Server Hostname:        127.0.0.1
Server Port:            3000

Document Path: /test Document Length: 1777782 bytes

Concurrency Level: 100 Time taken for tests: 132.000 seconds Complete requests: 1000 Failed requests: 0 Write errors: 0 Total transferred: 1777972000 bytes HTML transferred: 1777782000 bytes Requests per second: 7.58 [#/sec] (mean) Time per request: 13200.000 [ms] (mean) Time per request: 132.000 [ms] (mean, across all concurrent requests) Transfer rate: 13153.79 [Kbytes/sec] received

Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 1.9 0 16 Processing: 906 12899 1585.2 13203 17375 Waiting: 125 8761 1412.6 8969 13125 Total: 906 12899 1585.2 13203 17375

Percentage of the requests served within a certain time (ms) 50% 13203 66% 13234 75% 13250 80% 13266 90% 13328 95% 13375 98% 15422 99% 16313 100% 17375 (longest request)

第二次 expressjs


Server Hostname:        127.0.0.1
Server Port:            3000

Document Path:          /test
Document Length:        1777782 bytes

Concurrency Level:      100
Time taken for tests:   133.109 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      1777972000 bytes
HTML transferred:       1777782000 bytes
Requests per second:    7.51 [#/sec] (mean)
Time per request:       13310.938 [ms] (mean)
Time per request:       133.109 [ms] (mean, across all concurrent requests)
Transfer rate:          13044.17 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   1.4      0      16
Processing:   266 13013 1572.1  13203   17313
Waiting:      125 8830 1431.2   8969   13109
Total:        266 13013 1572.1  13203   17313

Percentage of the requests served within a certain time (ms)
  50%  13203
  66%  13281
  75%  13484
  80%  13531
  90%  13953
  95%  14109
  98%  15063
  99%  16391
 100%  17313 (longest request)

第一次 springnodejs

Server Hostname:        127.0.0.1
Server Port:            8888

Document Path:          /ws/test
Document Length:        1377781 bytes

Concurrency Level:      100
Time taken for tests:   76.906 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      1377896000 bytes
HTML transferred:       1377781000 bytes
Requests per second:    13.00 [#/sec] (mean)
Time per request:       7690.625 [ms] (mean)
Time per request:       76.906 [ms] (mean, across all concurrent requests)
Transfer rate:          17496.65 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   1.4      0      16
Processing:   344 7518 922.7   7578    9984
Waiting:       94 5105 824.7   5141    7578
Total:        344 7518 922.6   7578    9984

Percentage of the requests served within a certain time (ms)
  50%   7578
  66%   7781
  75%   7859
  80%   7953
  90%   8031
  95%   8063
  98%   8859
  99%   9391
 100%   9984 (longest request)

第二次 springnodejs

Server Hostname:        127.0.0.1
Server Port:            8888

Document Path:          /ws/test
Document Length:        1377781 bytes

Concurrency Level:      100
Time taken for tests:   14.328 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      1377896000 bytes
HTML transferred:       1377781000 bytes
Requests per second:    69.79 [#/sec] (mean)
Time per request:       1432.813 [ms] (mean)
Time per request:       14.328 [ms] (mean, across all concurrent requests)
Transfer rate:          93913.30 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   1.6      0      16
Processing:   -15 6621 2371.1   7547    9938
Waiting:        0 4659 1493.7   5141    7516
Total:          0 6621 2371.1   7547    9938

Percentage of the requests served within a certain time (ms)
  50%   7547
  66%   7563
  75%   7563
  80%   7563
  90%   7578
  95%   7594
  98%   8500
  99%   9250
 100%   9938 (longest request)

第三次 springnodejs

Server Hostname:        127.0.0.1
Server Port:            8888

Document Path:          /ws/test
Document Length:        1377781 bytes

Concurrency Level:      100
Time taken for tests:   86.844 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      1377896000 bytes
HTML transferred:       1377781000 bytes
Requests per second:    11.51 [#/sec] (mean)
Time per request:       8684.375 [ms] (mean)
Time per request:       86.844 [ms] (mean, across all concurrent requests)
Transfer rate:          15494.51 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   1.1      0      16
Processing:   172 8486 1002.8   8688   11406
Waiting:       78 5758 924.5   5906    8641
Total:        172 8486 1002.8   8688   11406

Percentage of the requests served within a certain time (ms)
  50%   8688
  66%   8703
  75%   8703
  80%   8703
  90%   8719
  95%   8719
  98%   9938
  99%  10797
 100%  11406 (longest request)
/*相当于 LR 中的 每秒事务数 ,mean 表示这是一个平均值*/

Requests per second: 3.09 [#/sec] (mean) /*相当于 LR 中的 平均事务响应时间,一次并发10个请求花费的时间 */ Time per request: 3234.651 [ms] (mean) /一次并发10个用户请求时的每个请求的平均响应时间/ Time per request: 323.465 [ms] (mean, across all concurrent requests)


12 回复

Node.js之SpringNodeJS与Express压力测试

在本帖中,我们将对使用Node.js的两个流行框架——SpringNodeJS和Express进行压力测试。通过对比这两种框架在处理大量请求时的表现,我们可以更好地理解它们各自的性能特点。

环境设置

首先,我们需要确保已安装Apache Bench(ab)工具,用于进行压力测试。此外,还需要安装Node.js和npm。

基础代码示例

以下是使用Express和SpringNodeJS实现的基本路由示例:

Express示例

// express-test.js
var express = require('express');
var app = express();

app.get('/test', function(req, res) {
  res.send({'key': 'hello rest'});
});

app.listen(3000, () => {
  console.log('Express server listening on port 3000');
});

SpringNodeJS示例

// springnodejs-test.js
module.exports = {
  'get:/test': {
    controller: function() {
      return {'key': 'hello rest'};
    }
  }
};

// 启动脚本
require('./springnodejs-test')();

压力测试配置

我们将使用Apache Bench (ab) 工具来进行压力测试。以下是基本命令格式:

ab -c <并发数> -n <请求数> <URL>

例如:

  • 对Express应用进行测试:
    ab -c 100 -n 20000 http://127.0.0.1:3000/test
    
  • 对SpringNodeJS应用进行测试:
    ab -c 100 -n 20000 http://127.0.0.1:8888/ws/test
    

测试结果分析

从测试结果来看,我们发现Express的性能明显优于SpringNodeJS。具体表现在以下几个方面:

  1. 请求响应速度

    • Express在高并发情况下能够更快地响应请求,其每秒处理的请求数量显著高于SpringNodeJS。
    • 例如,在默认配置下,Express每秒可以处理约3359个请求,而SpringNodeJS仅能处理约892个请求。
  2. 资源占用

    • 在高负载下,Express的资源占用率相对较低,能够更高效地利用系统资源。
    • 相比之下,SpringNodeJS在高负载下的资源占用较高,导致其性能下降更为明显。
  3. 稳定性

    • 在大数据量(如10万条记录)的情况下,Express仍然保持较高的稳定性和响应速度。
    • SpringNodeJS在处理大数据量时,虽然有所提升,但依然落后于Express。

总结

通过对SpringNodeJS和Express的压力测试,我们可以看到Express在处理高并发请求时表现出色,具有更高的吞吐量和更低的延迟。这使得Express成为构建高性能Web应用的理想选择。然而,SpringNodeJS在某些特定场景下可能有其独特的优势,例如在复杂业务逻辑处理上。

为了进一步优化性能,可以考虑使用负载均衡、缓存机制等技术手段来提升整体系统的性能表现。


大神 ,这个测试的结论是什么呢? 哪个更好

你是用什么工具测试的,如果要做并发测试 有什么好工具没

请问想表达什么意思?

结果来说就我写的好点,因为我写的 url 拦截是用 key 方式映射,肯定比express快。 就URL 拦截来说 设计不同吧,我认为一个URL操作对应用一个控制方法 就这是REST,他那个是中间件的概念,可以中转下一个中间件 还有他没有自动注入的概念,也没有拆分的概念,也没有程序预处理的概念,就拿URL 拦截来说,因为有程序预见处理概念,在程序启动时将准备数据的元数据先提取出来,程序执行期直接拿来当边界条件判断就行了,他如果没有的话,在执行时期还有算出边界处理,再进行运算。这当然是不先进的

主要是测试下内部其它封装性能如何,自己做的东西效果到底怎样 我没用过什么测试工具,也不太懂,就上面三个指标是比较重要的,凡是安装了 一根鸟毛,在bin目录都能找到

‘get:/test’:除了controller还有什么属性? before,after ?

嘿嘿,可以无聊扩展,有兴趣研究的话 www.springnodejs.com

D:\APMServ5.2.6\Apache\bin>ab -c 100 -n 20000 http://127.0.0.1:82/test
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 2000 requests
Completed 4000 requests
Completed 6000 requests

Test aborted after 10 failures

apr_socket_connect(): 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。   (730048)
Total of 7224 requests completed
D:\APMServ5.2.6\Apache\bin>ab -c 100 -n 20000 http://127.0.0.1:82/test
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 2000 requests
Completed 4000 requests
Completed 6000 requests
Completed 8000 requests
Completed 10000 requests
Completed 12000 requests
Completed 14000 requests
Completed 16000 requests
Completed 18000 requests
Completed 20000 requests
Finished 20000 requests


Server Software:
Server Hostname:        127.0.0.1
Server Port:            82

Document Path:          /test
Document Length:        54 bytes

Concurrency Level:      100
Time taken for tests:   6.438 seconds
Complete requests:      20000
Failed requests:        0
Write errors:           0
Total transferred:      3720000 bytes
HTML transferred:       1080000 bytes
Requests per second:    3106.80 [#/sec] (mean)
Time per request:       32.188 [ms] (mean)
Time per request:       0.322 [ms] (mean, across all concurrent requests)
Transfer rate:          564.32 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   1.4      0      16
Processing:     0   32   7.6     31      63
Waiting:        0   31   7.7     31      63
Total:          0   32   7.6     31      63

Percentage of the requests served within a certain time (ms)
  50%     31
  66%     31
  75%     31
  80%     31
  90%     47
  95%     47
  98%     47
  99%     47
 100%     63 (longest request)
D:\APMServ5.2.6\Apache\bin>ab -c 100 -n 20000 http://127.0.0.1:82/test

This is ApacheBench, Version 2.3 <$Revision: 655654 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient) Completed 2000 requests Completed 4000 requests Completed 6000 requests Completed 8000 requests Completed 10000 requests Completed 12000 requests Completed 14000 requests Completed 16000 requests Completed 18000 requests Completed 20000 requests Finished 20000 requests

Server Software: Server Hostname: 127.0.0.1 Server Port: 82

Document Path: /test Document Length: 54 bytes

Concurrency Level: 100 Time taken for tests: 6.313 seconds Complete requests: 20000 Failed requests: 0 Write errors: 0 Total transferred: 3720000 bytes HTML transferred: 1080000 bytes Requests per second: 3168.32 [#/sec] (mean) Time per request: 31.563 [ms] (mean) Time per request: 0.316 [ms] (mean, across all concurrent requests) Transfer rate: 575.50 [Kbytes/sec] received

Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 1.3 0 16 Processing: 0 31 7.6 31 63 Waiting: 0 31 7.6 31 63 Total: 0 31 7.5 31 63

Percentage of the requests served within a certain time (ms) 50% 31 66% 31 75% 31 80% 31 90% 47 95% 47 98% 47 99% 47 100% 63 (longest request)

我看了下代码,跟PHP MVC 一样。。。肯定慢了

太复杂了 又是this,又是that

从测试结果来看,express 在处理大量数据(如 10 万条记录)的压力测试中表现不佳,响应时间和请求数都显著下降。而 springnodejs 虽然也有性能下降,但总体上比 express 好一些。

示例代码及优化建议

Express 优化示例代码

const express = require('express');
const app = express();

// 使用缓存对象存储大数据
const cache = {};
for (let i = 0; i < 5000000; i++) {
    cache[i] = i;
}

app.get('/test', (req, res) => {
    // 将缓存对象直接转换为 JSON 字符串
    res.json(cache);
});

app.listen(3000, () => {
    console.log('Express server running on port 3000');
});

Springnodejs 优化示例代码

module.exports = {
    cache: {},
    awake: function() {
        for (let i = 0; i < 5000000; i++) {
            this.cache[i] = i;
        }
    },
    'get:/test': {
        controller: function() {
            return this.cache;
        }
    }
};

// 启动应用
require('./path/to/springnodejs')(3000);

性能优化建议

  1. 缓存优化:确保缓存数据的创建和访问效率高。
  2. 异步处理:使用异步方法避免阻塞主线程。
  3. 中间件优化:减少不必要的中间件使用。
  4. 资源限制:调整 Node.js 运行时参数,例如堆内存大小 (--max-old-space-size)。

通过以上优化,可以提高应用在高并发情况下的性能。

回到顶部