Skip to content

Instantly share code, notes, and snippets.

@Paxa
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Paxa/9971253 to your computer and use it in GitHub Desktop.
Save Paxa/9971253 to your computer and use it in GitHub Desktop.
Ruby and nodejs http library benchmark
[em-http-request, HTTParty, Net::HTTP, Net::HTTP (persistent), open-uri, RestClient, Typhoeus, Excon, Excon (persistent), Curb, Patron (persistent)]
+------------------------+----------+
| tach | total |
+------------------------+----------+
| Patron (persistent) | 0.223029 |
+------------------------+----------+
| Curb | 0.235344 |
+------------------------+----------+
| Typhoeus | 0.389802 |
+------------------------+----------+
| Net::HTTP (persistent) | 0.459979 |
+------------------------+----------+
| Net::HTTP | 0.485100 |
+------------------------+----------+
| em-http-request | 0.526450 |
+------------------------+----------+
| HTTParty | 0.531966 |
+------------------------+----------+
| RestClient | 0.630613 |
+------------------------+----------+
| open-uri | 0.749793 |
+------------------------+----------+
| Excon (persistent) | 1.319045 |
+------------------------+----------+
| Excon | 1.373122 |
+------------------------+----------+
1000 requests.
Running benchmarks...
==== request ====
1.25 ms/request
0.125sec total time
==== http ====
0.66 ms/request
0.066sec total time
==== curlrequest ====
5.78 ms/request
0.578sec total time
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('\
<!DOCTYPE html>\
<html class=" ">\
<head prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# githubog: http://ogp.me/ns/fb/githubog#">\
<meta charset="utf-8">\
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">\
<title>excon benchmark vs google</title>\
<meta content="authenticity_token" name="csrf-param" />\
<meta content="LuKd/DQ9URBprJnKFz6N2GuQs6a6Knhoqlwksy6AaBA=" name="csrf-token" />\
<meta name="viewport" content="width=960">\
</head>\
<body>test</body>\
</html>\
');
}).listen(51000);
require 'em-http-request'
require 'httparty'
require 'excon'
require 'net/http'
require 'open-uri'
require 'rest_client'
require 'tach'
require 'typhoeus'
require 'curb'
require 'patron'
target_url = "http://127.0.0.1:51000"
target_host = "127.0.0.1"
target_port = "51000"
Tach.meter(1000) do
tach('em-http-request') do
EventMachine.run {
http = EventMachine::HttpRequest.new(target_url).get
http.callback {
http.response
EventMachine.stop
}
}
end
tach('HTTParty') do
HTTParty.get(target_url).body
end
tach('Net::HTTP') do
Net::HTTP.start(target_host, target_port) {|http| http.get('/').body }
end
Net::HTTP.start(target_host, target_port) do |http|
tach('Net::HTTP (persistent)') do
http.get('/').body
end
end
tach('open-uri') do
open(target_url).read
end
tach('RestClient') do
RestClient.get(target_url)
end
#streamly = StreamlyFFI::Connection.new
#tach('StreamlyFFI (persistent)') do
# streamly.get(target_url)
#end
tach('Typhoeus') do
Typhoeus::Request.get(target_url).body
end
tach('Excon') do
Excon.get(target_url).body
end
excon = Excon.new(target_url)
tach('Excon (persistent)') do
excon.request(:method => 'get').body
end
tach("Curb") do
Curl.get(target_url)
end
=begin
tach("Patron") do
patron = Patron::Session.new
patron.base_url = target_url
patron.get("/")
end
=end
patron = Patron::Session.new
patron.base_url = target_url
tach("Patron (persistent)") do
patron.get("/")
end
end
var http = require('http');
var request = require('request');
var curlrequest = require('curlrequest');
var ben = require('ben');
var async = require('async');
var iteractions = 100;
var target_url = "http://127.0.0.1:51000"
var target_host = "127.0.0.1"
var target_port = 51000
var reporter = function (bench_name, callback, ms) {
console.log("==== " + bench_name + " ====");
console.log(ms + ' ms/request');
console.log((iteractions * ms / 1000) + "sec total time\n");
callback();
};
var delay = function (ms, callback) {
setTimeout(callback, ms);
}.bind('', 1000);
console.log("Running benchmarks...\n");
async.series([
function (callback) {
ben.async(iteractions, function(done) {
request(target_url, done);
}, reporter.bind('', 'request', callback));
},
delay,
function (callback) {
ben.async(iteractions, function(done) {
http.get({hostname: target_host, port: target_port, path: '/', agent: false}, function(res) {
res.on('data', function (chunk) { }); // somehow need it
res.on('end', function() {
done();
});
});
}, reporter.bind('', 'http', callback));
},
delay,
/*
// broken on OS X
function (callback) {
ben.async(iteractions, function(done) {
curl(target_url, function() {
console.log('.');
done();
});
}, reporter.bind('node-curl', callback));
},
delay,
*/
function (callback) {
ben.async(iteractions, function(done) {
curlrequest.request({ url: target_url }, done)
}, reporter.bind('', 'curlrequest', callback));
},
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment