Skip to content

Instantly share code, notes, and snippets.

@ujifgc
Last active June 10, 2017 11:52
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 ujifgc/479e0261bda871923f82efa4715342ab to your computer and use it in GitHub Desktop.
Save ujifgc/479e0261bda871923f82efa4715342ab to your computer and use it in GitHub Desktop.
require "benchmark"
module HTTP
def self.default_status_message_for(status_code : Int) : String
case status_code
when 100 then "Continue"
when 101 then "Switching Protocols"
when 102 then "Processing"
when 200 then "OK"
when 201 then "Created"
when 202 then "Accepted"
when 203 then "Non-Authoritative Information"
when 204 then "No Content"
when 205 then "Reset Content"
when 206 then "Partial Content"
when 207 then "Multi-Status"
when 208 then "Already Reported"
when 226 then "IM Used"
when 300 then "Multiple Choices"
when 301 then "Moved Permanently"
when 302 then "Found"
when 303 then "See Other"
when 304 then "Not Modified"
when 305 then "Use Proxy"
when 307 then "Temporary Redirect"
when 308 then "Permanent Redirect"
when 400 then "Bad Request"
when 401 then "Unauthorized"
when 402 then "Payment Required"
when 403 then "Forbidden"
when 404 then "Not Found"
when 405 then "Method Not Allowed"
when 406 then "Not Acceptable"
when 407 then "Proxy Authentication Required"
when 408 then "Request Timeout"
when 409 then "Conflict"
when 410 then "Gone"
when 411 then "Length Required"
when 412 then "Precondition Failed"
when 413 then "Payload Too Large"
when 414 then "URI Too Long"
when 415 then "Unsupported Media Type"
when 416 then "Range Not Satisfiable"
when 417 then "Expectation Failed"
when 421 then "Misdirected Request"
when 422 then "Unprocessable Entity"
when 423 then "Locked"
when 424 then "Failed Dependency"
when 426 then "Upgrade Required"
when 428 then "Precondition Required"
when 429 then "Too Many Requests"
when 431 then "Request Header Fields Too Large"
when 451 then "Unavailable For Legal Reasons"
when 500 then "Internal Server Error"
when 501 then "Not Implemented"
when 502 then "Bad Gateway"
when 503 then "Service Unavailable"
when 504 then "Gateway Timeout"
when 505 then "HTTP Version Not Supported"
when 506 then "Variant Also Negotiates"
when 507 then "Insufficient Storage"
when 508 then "Loop Detected"
when 510 then "Not Extended"
when 511 then "Network Authentication Required"
else ""
end
end
STATUS_CODES = {
100 => "Continue",
101 => "Switching Protocols",
102 => "Processing",
200 => "OK",
201 => "Created",
202 => "Accepted",
203 => "Non-Authoritative Information",
204 => "No Content",
205 => "Reset Content",
206 => "Partial Content",
207 => "Multi-Status",
208 => "Already Reported",
226 => "IM Used",
300 => "Multiple Choices",
301 => "Moved Permanently",
302 => "Found",
303 => "See Other",
304 => "Not Modified",
305 => "Use Proxy",
307 => "Temporary Redirect",
308 => "Permanent Redirect",
400 => "Bad Request",
401 => "Unauthorized",
402 => "Payment Required",
403 => "Forbidden",
404 => "Not Found",
405 => "Method Not Allowed",
406 => "Not Acceptable",
407 => "Proxy Authentication Required",
408 => "Request Timeout",
409 => "Conflict",
410 => "Gone",
411 => "Length Required",
412 => "Precondition Failed",
413 => "Payload Too Large",
414 => "URI Too Long",
415 => "Unsupported Media Type",
416 => "Range Not Satisfiable",
417 => "Expectation Failed",
421 => "Misdirected Request",
422 => "Unprocessable Entity",
423 => "Locked",
424 => "Failed Dependency",
426 => "Upgrade Required",
428 => "Precondition Required",
429 => "Too Many Requests",
431 => "Request Header Fields Too Large",
451 => "Unavailable For Legal Reasons",
500 => "Internal Server Error",
501 => "Not Implemented",
502 => "Bad Gateway",
503 => "Service Unavailable",
504 => "Gateway Timeout",
505 => "HTTP Version Not Supported",
506 => "Variant Also Negotiates",
507 => "Insufficient Storage",
508 => "Loop Detected",
510 => "Not Extended",
511 => "Network Authentication Required",
}
def self.default_status_message_for1(status_code : Int) : String
STATUS_CODES.fetch(status_code, "")
end
def self.default_status_message_for2(status_code : Int) : String
{% for code, message in STATUS_CODES %}
return {{message}} if status_code == {{code}}
{% end %}
""
end
end
Benchmark.ips do |x|
keys = HTTP::STATUS_CODES.keys
x.report("case sample") { HTTP.default_status_message_for(keys.sample) }
x.report("hash sample") { HTTP.default_status_message_for1(keys.sample) }
x.report("macro sample") { HTTP.default_status_message_for2(keys.sample) }
end
[100, 200, 404, 500].each do |status|
Benchmark.ips do |x|
x.report("case #{status}") { HTTP.default_status_message_for(status) }
x.report("hash #{status}") { HTTP.default_status_message_for1(status) }
x.report("macro #{status}") { HTTP.default_status_message_for2(status) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment