javascript-如何获取由node-http-proxy完成的每个代理请求的响应时间?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-如何获取由node-http-proxy完成的每个代理请求的响应时间?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2931字,纯文字阅读大概需要5分钟。
内容图文
我想计算由node-http-proxy完成的每个代理请求的响应时间,如下所示:
var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer();
require('http').createServer(function(req, res) {
// start-time for one request
proxy.web(req, res, { target: 'localhost:80' });
}).listen(3000);
proxy.on('proxyRes', function (res) {
// end-time for one request to calculate the time
console.log('RAW Response from the target', JSON.stringify(res.headers, true, 2));
});
所以我想获得开始时间和进入时间之间的时差.我能够捕获proxyRes事件,但是如何使代理响应与正确的客户端请求匹配?
更新:我尝试了以下操作:
var httpProxy = require('http-proxy');
var http = require('http');
var proxy = new httpProxy.createProxyServer({
target: {
host: 'localhost',
port: 80
}
});
var start_time = 0;
var proxyServer = http.createServer(function (req, res) {
start_time = new Date().getTime();
proxy.web(req, res);
res.on('finish', function() {
console.log("The request was proxied in " + (new Date().getTime() - start_time) + "ms");
});
});
proxyServer.listen(3000);
// Proxy-Target server
var n = 0; sleep = 0;
http.createServer(function (req, res) {
n++;
var start_time = new Date().getTime();
res.writeHead(200, {'Content-Type': 'text/plain'});
if (n % 2 == 0){
sleep = 2000;
} else {
sleep = 5000;
}
setTimeout(function(){
res.end("...");
}, sleep);
res.on('finish', function(d) {
var diff_time = new Date().getTime() - start_time;
console.log("The request was processed in " + (new Date().getTime() - start_time) + "ms");
});
}).listen(80);
因此,正确测量了目标服务器的时间,导致一个请求可能持续2到5秒,但是代理有时测量的响应时间仅为几毫秒.
我通过使用apache-bench来尝试它,例如:ab -n 10 -c 3 http:// localhost:3000 /和控制台的结果:
The request was processed in 2007ms
The request was proxied in 2017ms
The request was processed in 2003ms
The request was proxied in 2006ms
The request was processed in 5002ms
The request was processed in 5001ms
The request was proxied in 980ms
The request was proxied in 981ms
The request was processed in 2002ms
The request was proxied in 2006ms
The request was processed in 2002ms
The request was proxied in 2005ms
The request was processed in 5000ms
The request was proxied in 8ms
The request was processed in 5000ms
The request was proxied in 982ms
The request was processed in 2001ms
The request was proxied in 2005ms
The request was processed in 5002ms
The request was proxied in 4998ms
解决方法:
现在它可以工作了,在nodejs google-gloup上得到了关键提示,start_time的对应项未在本地设置,因此这是工作代码:
var httpProxy = require('http-proxy');
var http = require('http');
var proxy = new httpProxy.createProxyServer({});
// before: var start_time = 0;
var proxyServer = http.createServer(function (req, res) {
// now
var start_time = new Date().getTime();
proxy.web(req, res, { target: 'http://localhost:80' });
res.on('finish', function() {
console.log("The request was proxied in " + (new Date().getTime() - start_time) + "ms");
});
});
proxyServer.listen(3000);
内容总结
以上是互联网集市为您收集整理的javascript-如何获取由node-http-proxy完成的每个代理请求的响应时间?全部内容,希望文章能够帮你解决javascript-如何获取由node-http-proxy完成的每个代理请求的响应时间?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。