File: thin.rb

package info (click to toggle)
ruby-http-parser.rb 0.6.0-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 376 kB
  • sloc: java: 431; ansic: 412; ruby: 355; makefile: 20
file content (58 lines) | stat: -rw-r--r-- 1,845 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
$:.unshift File.dirname(__FILE__) + "/../lib"
require "rubygems"
require "thin_parser"
require "http_parser"
require "benchmark"
require "stringio"

data = "POST /postit HTTP/1.1\r\n" +
       "Host: localhost:3000\r\n" +
       "User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9\r\n" +
       "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n" +
       "Accept-Language: en-us,en;q=0.5\r\n" +
       "Accept-Encoding: gzip,deflate\r\n" +
       "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" +
       "Keep-Alive: 300\r\n" +
       "Connection: keep-alive\r\n" +
       "Content-Type: text/html\r\n" +
       "Content-Length: 37\r\n" +
       "\r\n" +
       "name=marc&email=macournoyer@gmail.com"

def thin(data)
  env = {"rack.input" => StringIO.new}
  Thin::HttpParser.new.execute(env, data, 0)
  env
end

def http_parser(data)
  body = StringIO.new
  env = nil

  parser = HTTP::RequestParser.new
  parser.on_headers_complete = proc { |e| env = e }
  parser.on_body = proc { |c| body << c }
  parser << data

  env["rack-input"] = body
  env
end

# p thin(data)
# p http_parser(data)

TESTS = 30_000
Benchmark.bmbm do |results|
  results.report("thin:") { TESTS.times { thin data } }
  results.report("http-parser:") { TESTS.times { http_parser data } }
end

# On my MBP core duo 2.2Ghz
# Rehearsal ------------------------------------------------
# thin:          1.470000   0.000000   1.470000 (  1.474737)
# http-parser:   1.270000   0.020000   1.290000 (  1.292758)
# --------------------------------------- total: 2.760000sec
#
#                    user     system      total        real
# thin:          1.150000   0.030000   1.180000 (  1.173767)
# http-parser:   1.250000   0.010000   1.260000 (  1.263796)