PageRenderTime 66ms CodeModel.GetById 20ms app.highlight 12ms RepoModel.GetById 18ms app.codeStats 1ms

/test/yelp-benchmark.rb

http://github.com/fizx/parsley
Ruby | 53 lines | 46 code | 7 blank | 0 comment | 0 complexity | 72ca30d02e46bfc63393792dfe296f0b MD5 | raw file
 1require "rubygems"
 2require "nokogiri"
 3require "hpricot"
 4require "parsley"
 5require "benchmark"
 6require "pp"
 7
 8YELP_HTML = File.dirname(__FILE__) + "/yelp.html"
 9
10def noko
11  parse Nokogiri.Hpricot(File.open(YELP_HTML))
12end
13
14def hpri
15  parse Hpricot(File.open(YELP_HTML))  
16end
17
18def parse(doc)
19  out = {}
20  out["name"] = (doc / "h1").first.inner_text
21  out["phone"] = (doc / "#bizPhone").first.inner_text
22  out["address"] = (doc / "address").first.inner_text
23  out["reviews"] = (doc / ".nonfavoriteReview").map do |node|
24    review = {}
25    review["date"] = (node / ".ieSucks .smaller").first.inner_text
26    review["user_name"] = (node / ".reviewer_info a").first.inner_text
27    review["comment"] = (node / ".review_comment").first.inner_text
28    review
29  end
30end
31
32def pars
33  parselet = Parsley.new({
34    "name" => "h1",
35    "phone" => "#bizPhone",
36    "address" => "address",
37    "reviews(.nonfavoriteReview)" => [
38      {
39        "date" => ".ieSucks .smaller",
40        "user_name" => ".reviewer_info a",
41        "comment" => ".review_comment"
42      }
43    ]
44  })
45  parselet.parse(:file => YELP_HTML)
46end
47
48Benchmark.bm do |x|
49  x.report("nokogiri: ")  { 3.times { noko } }
50  x.report("hpricot: ")   { 3.times { hpri } }
51  x.report("parsley: ") { 3.times { pars } }
52end
53