Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 3rd, 2012  |  syntax: None  |  size: 1.68 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. require 'nokogiri'
  2. require 'benchmark'
  3. require 'thread'
  4. require 'open-uri'
  5.  
  6. # for JRUBY: export JRUBY_OPTS="--1.9 -J-Xmx2048m"
  7. # JRuby 1.6.7 doesn't support File.write yet in 1.9 mode. Just doing some cache here - not important
  8. File.open('test.html', 'w'){|f| f.write(open('http://www.sec.gov/Archives/edgar/data/1129623/000095012310064472/d73737ddefm14a.htm').read) } unless File.exists?('test.html')
  9. content = File.read('test.html')
  10.  
  11. class Executor
  12.   def initialize(pool_size)
  13.     @available_threads = Queue.new
  14.     pool_size.times { @available_threads << nil }
  15.     @threads = []
  16.   end
  17.  
  18.   def execute
  19.     @available_threads.pop
  20.     @threads << Thread.start {
  21.       yield
  22.       @available_threads << nil
  23.     }
  24.   end
  25.  
  26.   def shutdown
  27.     @threads.each &:join
  28.   end
  29. end
  30.  
  31. Benchmark.bmbm do |x|
  32.   x.report('Parse HTML') do
  33.     executor = Executor.new(10)
  34.     50.times { executor.execute { Nokogiri.HTML(content) } }
  35.     executor.shutdown
  36.   end  
  37. end
  38.  
  39. # Results for JRuby 1.6.7:
  40. # Rehearsal ----------------------------------------------
  41. # Parse HTML   4.501000   0.000000   4.501000 (  4.501000)
  42. # ------------------------------------- total: 4.501000sec
  43. #
  44. #                  user     system      total        real
  45. # Parse HTML   0.927000   0.000000   0.927000 (  0.928000)
  46.  
  47. # Results for MRI 1.9.3-p
  48. # Rehearsal ----------------------------------------------
  49. # Parse HTML   3.800000   0.050000   3.850000 (  3.853381)
  50. # ------------------------------------- total: 3.850000sec
  51. #
  52. #                  user     system      total        real
  53. # Parse HTML   3.780000   0.010000   3.790000 (  3.788918)
  54.  
  55. # They perform about the same if I change to Executor.new(1), ie, using a single thread
  56.  
  57. # Am I doing any mistake in this code?