Skip to content

Instantly share code, notes, and snippets.

@skatkov
Last active July 11, 2022 12:43
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 skatkov/eb18ae1f9f75e822812b64a0ae44915d to your computer and use it in GitHub Desktop.
Save skatkov/eb18ae1f9f75e822812b64a0ae44915d to your computer and use it in GitHub Desktop.
Benchmarking content-type and http_headers/content_type gems
require "content-type"
require 'http_headers/content_type'
require 'benchmark'
headers = [
'application/java-archive',
"application/EDI-X12",
'application/EDIFACT',
'application/javascript',
'application/octet-stream',
'application/ogg',
'application/pdf',
'application/xhtml+xml',
'application/x-shockwave-flash',
'application/json',
'application/ld+json',
'application/xml',
'application/zip',
'application/x-www-form-urlencoded',
' application/x-www-form-urlencoded ',
' application/json ',
"application/json; charset=UTF-8",
"application/json; charset=utf-8",
'application/ld+json charset=utf-8',
'application/x-www-form-urlencoded charset=utf-8',
'application/x-www-form-urlencoded charset=UTF-8',
'application/xml charset=UTF-8',
'application/xml charset=utf-8',
]
headers.each do |header|
puts "Header: #{header}"
begin
puts "Content-Type subtype: #{ContentType.parse(header).subtype}"
rescue => e
puts "Content-Type error raised: #{e.message}"
end
puts "HTTPHeaders::ContentType content_type: #{HttpHeaders::ContentType.new(header).content_type}"
end
puts "----"
puts "Now let's benchmark these gems..."
puts "----"
n = 50_000
Benchmark.bm do |benchmark|
benchmark.report("ContentType") do
n.times do
begin
ContentType.parse("application/json").subtype
rescue => e
end
end
end
benchmark.report("HttpHeaders::ContentType") do
n.times do
HttpHeaders::ContentType.new("application/json").content_type
end
end
end
source "https://rubygems.org"
gem "content-type", "0.0.1"
gem "http_headers-content_type"
Header: application/java-archive
Content-Type subtype: java-archive
HTTPHeaders::ContentType content_type: application/java-archive
Header: application/EDI-X12
Content-Type subtype: edi-x12
HTTPHeaders::ContentType content_type: application/EDI-X12
Header: application/EDIFACT
Content-Type subtype: edifact
HTTPHeaders::ContentType content_type: application/EDIFACT
Header: application/javascript
Content-Type subtype: javascript
HTTPHeaders::ContentType content_type: application/javascript
Header: application/octet-stream
Content-Type subtype: octet-stream
HTTPHeaders::ContentType content_type: application/octet-stream
Header: application/ogg
Content-Type subtype: ogg
HTTPHeaders::ContentType content_type: application/ogg
Header: application/pdf
Content-Type subtype: pdf
HTTPHeaders::ContentType content_type: application/pdf
Header: application/xhtml+xml
Content-Type subtype: xhtml+xml
HTTPHeaders::ContentType content_type: application/xhtml+xml
Header: application/x-shockwave-flash
Content-Type subtype: x-shockwave-flash
HTTPHeaders::ContentType content_type: application/x-shockwave-flash
Header: application/json
Content-Type subtype: json
HTTPHeaders::ContentType content_type: application/json
Header: application/ld+json
Content-Type subtype: ld+json
HTTPHeaders::ContentType content_type: application/ld+json
Header: application/xml
Content-Type subtype: xml
HTTPHeaders::ContentType content_type: application/xml
Header: application/zip
Content-Type subtype: zip
HTTPHeaders::ContentType content_type: application/zip
Header: application/x-www-form-urlencoded
Content-Type subtype: x-www-form-urlencoded
HTTPHeaders::ContentType content_type: application/x-www-form-urlencoded
Header: application/x-www-form-urlencoded
Content-Type error raised: Failed to match sequence (type:TYPE '/' subtype:SUBTYPE PARAMETERS{0, }) at line 1 char 1.
HTTPHeaders::ContentType content_type: application/x-www-form-urlencoded
Header: application/json
Content-Type error raised: Failed to match sequence (type:TYPE '/' subtype:SUBTYPE PARAMETERS{0, }) at line 1 char 1.
HTTPHeaders::ContentType content_type: application/json
Header: application/json; charset=UTF-8
Content-Type subtype: json
HTTPHeaders::ContentType content_type: application/json
Header: application/json; charset=utf-8
Content-Type subtype: json
HTTPHeaders::ContentType content_type: application/json
Header: application/ld+json charset=utf-8
Content-Type error raised: Failed to match sequence (type:TYPE '/' subtype:SUBTYPE PARAMETERS{0, }) at line 1 char 20.
HTTPHeaders::ContentType content_type: application/ld+json charset=utf-8
Header: application/x-www-form-urlencoded charset=utf-8
Content-Type error raised: Failed to match sequence (type:TYPE '/' subtype:SUBTYPE PARAMETERS{0, }) at line 1 char 34.
HTTPHeaders::ContentType content_type: application/x-www-form-urlencoded charset=utf-8
Header: application/x-www-form-urlencoded charset=UTF-8
Content-Type error raised: Failed to match sequence (type:TYPE '/' subtype:SUBTYPE PARAMETERS{0, }) at line 1 char 34.
HTTPHeaders::ContentType content_type: application/x-www-form-urlencoded charset=UTF-8
Header: application/xml charset=UTF-8
Content-Type error raised: Failed to match sequence (type:TYPE '/' subtype:SUBTYPE PARAMETERS{0, }) at line 1 char 16.
HTTPHeaders::ContentType content_type: application/xml charset=UTF-8
Header: application/xml charset=utf-8
Content-Type error raised: Failed to match sequence (type:TYPE '/' subtype:SUBTYPE PARAMETERS{0, }) at line 1 char 16.
HTTPHeaders::ContentType content_type: application/xml charset=utf-8
----
Now let's benchmark these gems...
----
user system total real
ContentType 21.963035 0.231030 22.194065 ( 22.385176)
HttpHeaders::ContentType 0.269696 0.002902 0.272598 ( 0.274939)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment