注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

snoopyxdy的博客

https://github.com/DoubleSpout

 
 
 

日志

 
 

小测thrift和http作为内部通信的性能  

2015-04-24 18:31:05|  分类: node |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

1、http代码,server端:

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser({limit: '1mb'}));
var count = 1;
app.post('/rec/data', function(req, res){
  var bufLen = (req.body.content || '').length
  res.send({
 len:bufLen,
 count:count++
  })
});
app.listen(4001);

2、http代码,client端

var request = require('request');
var assert = require('assert');
var async = require('async')
var host = 'http://127.0.0.1:4001'
var buf = new Buffer(1024)
var content = buf.fill('a').toString()
console.time('http1000');
var count = 0
var needCount  = 0
var reqList = []
for(var i=1;i<=1000;i++){
needCount += i
reqList.push(function(callback){
request.post({url:host+'/rec/data', 
form: {content:content}}, 
function(err,httpResponse,body){
if(err){
callback(err)
return console.log(err)
}
//console.log(body)
var resJson = JSON.parse(body)
assert(resJson.len === content.length)
count += resJson.count
callback()
})
})
}
async.series(reqList, function(err){
if(err){
console.log(err)
}
assert(count === needCount)
console.timeEnd('http1000');
})


3、thrift接口文件:

struct SendMsg {
  1: string content,
}

struct ResMsg {
  1: i32 len = 1,
  2: i32 count,
}

service Message{
   ResMsg send(1:SendMsg data),
}

4、thrif端的server代码:

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser());
var count = 1;
app.post('/rec/data', function(req, res){
  
  var bufLen = (req.body.content || '').length
  res.send({
 len:bufLen,
 count:count++
  })
});
app.listen(4001);


5、thrift端的client代码:

var thrift = require('thrift');
var assert = require('assert');
var async = require('async');
var transport = thrift.TBufferedTransport();
var protocol = thrift.TBinaryProtocol();
var Message = require('./gen-nodejs/Message.js');
var ttypes = require('./gen-nodejs/nodejs_msg_types.js');

var connection = thrift.createConnection("127.0.0.1", 9090, {
  transport : transport,
  protocol : protocol
});
connection.on('error', function(err) {
  console.log(err)
});

var client = thrift.createClient(Message, connection);
var buf = new Buffer(1024)
var content = buf.fill('a').toString()

console.time('thrift1000');
var count = 0
var needCount  = 0
var reqList = []
for(var i=1;i<=1000;i++){
needCount += i
reqList.push(function(callback){
var sendData = new ttypes.SendMsg()
sendData.content = content
client.send(sendData, function(err, response) {
if(err){
return callback(err)
}
//console.log(response)
assert(response.len == content.length)
count += response.count
callback()
});
})
}

async.series(reqList, function(err){
if(err){
console.log(err)
}
assert(count === needCount)
console.timeEnd('thrift1000');
})


6、测试结果对比
网络环境,内网千兆,依次循环1000次,发送数据包为1k,

http:
http1000: 4879ms
thrift:
thrift1000: 1254ms

网络环境,内网千兆,依次循环1000次,发送数据包为100k,
http:
http1000: 16924ms
thrift:
thrift1000: 2105ms
网络环境,内网千兆,并行执行1000次,发送数据包为1k,就是将async改为parallel
http:
http1000: 4265ms
thrift:
thrift1000: 437ms

  评论这张
 
阅读(2785)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016