-
-
Save thiensubs/b04fd643237cf5a432baf50c90175cc0 to your computer and use it in GitHub Desktop.
Memory profiling of http.rb and other popular Ruby HTTP client libraries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2.6.6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Run with `rackup app.ru` | |
require "roda" | |
Roda.route do |r| | |
r.post "upload" do | |
"Success" | |
end | |
r.get "download" do | |
"a" * 10*1024*1024 # 10 MB | |
end | |
end | |
run Roda.app |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "http" | |
require "net/http" | |
require "rest-client" | |
require "httparty" | |
require 'patron' | |
require 'typhoeus' | |
def profile(name) | |
memory_before = `ps -p #{Process::pid} -o rss`.split("\n")[1].chomp.to_i | |
result = yield | |
memory_after = `ps -p #{Process::pid} -o rss`.split("\n")[1].chomp.to_i | |
total_memory = memory_after - memory_before | |
puts "#{name}: #{"%.2f" % (total_memory.to_f / 1024)} MB" | |
result | |
end | |
GC.disable |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative "common" | |
url = "http://localhost:9292/download" | |
profile("http.rb") do | |
response = HTTP.post(url) | |
response.body.each { |chunk| chunk.clear } | |
end | |
profile("Net::HTTP") do | |
uri = URI.parse(url) | |
Net::HTTP.start(uri.host, uri.port) do |http| | |
http.request_get(uri.request_uri) do |response| | |
response.read_body { |chunk| chunk.clear } | |
end | |
end | |
end | |
profile("RestClient") do | |
RestClient::Request.execute( | |
method: :get, | |
url: url, | |
block_response: -> (response) { response.read_body { |chunk| chunk.clear } }) | |
end | |
profile("HTTParty") do | |
HTTParty.get(url, stream_body: true) { |chunk| chunk.clear } | |
end | |
profile("Patron") do | |
sess = Patron::Session.new | |
sess.timeout = 10 | |
sess.base_url = "http://localhost:9292" | |
sess.headers['User-Agent'] = 'myapp/1.0' | |
resp = sess.get("/download") | |
if resp.status < 400 | |
resp.body | |
end | |
end | |
profile("Typhoeus") do | |
# request = Typhoeus::Request.new(url) | |
Typhoeus.get(url) | |
# request.run | |
end | |
# Results (download) | |
# ================== | |
# | |
# http.rb: 0.17 MB | |
# Net::HTTP: 0.04 MB | |
# RestClient: 0.01 MB | |
# HTTParty: 10.47 MB | |
# Patron: 24.09 MB | |
# Typhoeus: 20.46 MB |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source "https://rubygems.org" | |
gem "roda" | |
gem "http", "~> 3.3" | |
gem "rest-client" | |
gem "httparty" | |
gem 'patron' | |
gem "typhoeus" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative "common" | |
require "tempfile" | |
tempfile = Tempfile.new | |
tempfile.write "a" * 10*1024*1024 # 10 MB | |
tempfile.rewind | |
path = tempfile.path | |
url = "http://localhost:9292/upload" | |
profile("http.rb") do | |
HTTP.post(url, form: { file: HTTP::FormData::File.new(path) }) | |
end | |
profile("Net::HTTP") do | |
uri = URI.parse(url) | |
Net::HTTP.start(uri.host, uri.port) do |http| | |
post = Net::HTTP::Post.new(uri.request_uri) | |
post.set_form({ "file" => File.open(path) }, "multipart/form-data") | |
http.request(post) | |
end | |
end | |
profile("RestClient") do | |
RestClient.post(url, file: File.open(path)) | |
end | |
profile("HTTParty") do | |
HTTParty.post(url, body: { file: tempfile }) | |
end | |
profile("Patron") do | |
sess = Patron::Session.new | |
sess.timeout = 10 | |
sess.post(url, { file: tempfile }) | |
end | |
profile("Typhoeus") do | |
Typhoeus.post( | |
url, | |
body: { | |
file: tempfile | |
} | |
) | |
end | |
# Results (upload) | |
# ================ | |
# | |
# http.rb: 0.36 MB | |
# Net::HTTP: 0.05 MB | |
# RestClient: 9.02 MB | |
# HTTParty: 40.04 MB | |
# Patron: 0.05 MB | |
# Typhoeus: 0.11 MB |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment