Class: TourBus

Inherits:
Monitor
  • Object
show all
Defined in:
lib/tour_bus.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = "localhost", concurrency = 1, number = 1, tourists = [], test_list = nil) ⇒ TourBus

Returns a new instance of TourBus.



6
7
8
9
10
11
# File 'lib/tour_bus.rb', line 6

def initialize(host="localhost", concurrency=1, number=1, tourists=[], test_list=nil)
  @host, @concurrency, @number, @tourists, @test_list = host, concurrency, number, tourists, test_list
  @runner_id = 0
  @runs, @tests, @passes, @fails, @errors = 0,0,0,0,0
  super()
end

Instance Attribute Details

#benchmarksObject (readonly)

Returns the value of attribute benchmarks.



4
5
6
# File 'lib/tour_bus.rb', line 4

def benchmarks
  @benchmarks
end

#concurrencyObject (readonly)

Returns the value of attribute concurrency.



4
5
6
# File 'lib/tour_bus.rb', line 4

def concurrency
  @concurrency
end

#errorsObject (readonly)

Returns the value of attribute errors.



4
5
6
# File 'lib/tour_bus.rb', line 4

def errors
  @errors
end

#failsObject (readonly)

Returns the value of attribute fails.



4
5
6
# File 'lib/tour_bus.rb', line 4

def fails
  @fails
end

#hostObject (readonly)

Returns the value of attribute host.



4
5
6
# File 'lib/tour_bus.rb', line 4

def host
  @host
end

#numberObject (readonly)

Returns the value of attribute number.



4
5
6
# File 'lib/tour_bus.rb', line 4

def number
  @number
end

#passesObject (readonly)

Returns the value of attribute passes.



4
5
6
# File 'lib/tour_bus.rb', line 4

def passes
  @passes
end

#runsObject (readonly)

Returns the value of attribute runs.



4
5
6
# File 'lib/tour_bus.rb', line 4

def runs
  @runs
end

#testsObject (readonly)

Returns the value of attribute tests.



4
5
6
# File 'lib/tour_bus.rb', line 4

def tests
  @tests
end

#touristsObject (readonly)

Returns the value of attribute tourists.



4
5
6
# File 'lib/tour_bus.rb', line 4

def tourists
  @tourists
end

Instance Method Details

#log(message) ⇒ Object



98
99
100
# File 'lib/tour_bus.rb', line 98

def log(message)
  puts "#{Time.now.strftime('%F %H:%M:%S')} TourBus: #{message}"
end

#next_runner_idObject



13
14
15
16
17
# File 'lib/tour_bus.rb', line 13

def next_runner_id
  synchronize do
    @runner_id += 1
  end 
end

#runObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/tour_bus.rb', line 44

def run
  threads = []
  threads_ready = 0
  start_running = false
  mutex = Mutex.new
  tourist_name = "#{total_runs} runs: #{concurrency}x#{number} of #{tourists * ','}"
  started = Time.now.to_f
  concurrency.times do |conc|
    log "Starting #{tourist_name}"
    threads << Thread.new do
      runner_id = next_runner_id
      mutex.lock
      threads_ready += 1
      if threads_ready == concurrency
        log "All Runners are ready -- STARTING!"
        start_running = true
      end
      mutex.unlock
      sleep 0.05 until start_running
      runs,tests,passes,fails,errors,start = 0,0,0,0,0,Time.now.to_f
      bm = Benchmark.measure do
        runner = Runner.new(@host, @tourists, @number, runner_id, @test_list)
        runs,tests,passes,fails,errors = runner.run_tourists
        update_stats runs, tests, passes, fails, errors
      end
      log "Runner Finished!"
      log "Runner finished in %0.3f seconds" % (Time.now.to_f - start)
      log "Runner Finished! runs,passes,fails,errors: #{runs},#{passes},#{fails},#{errors}"
      log "Benchmark for runner #{runner_id}: #{bm}"
    end
  end
  log "Initializing #{concurrency} Runners..."
  threads.each {|t| t.join }
  finished = Time.now.to_f
  log '-' * 80
  log tourist_name
  log "All Runners finished."
  log "Total Tourists: #{@runs}"
  log "Total Tests: #{@tests}"
  log "Total Passes: #{@passes}"
  log "Total Fails: #{@fails}"
  log "Total Errors: #{@errors}"
  log "Elapsed Time: #{finished - started}"
  log "Speed: %5.3f tours/sec" % (@runs / (finished-started))
  log '-' * 80
  if @fails > 0 || @errors > 0
    log '********************************************************************************'
    log '********************************************************************************'
    log '                            !! THERE WERE FAILURES !!'
    log '********************************************************************************'
    log '********************************************************************************'
  end
end

#runners(filter = []) ⇒ Object



35
36
37
38
# File 'lib/tour_bus.rb', line 35

def runners(filter=[])
  # All files in tours folder, stripped to basename, that match any item in filter
  Dir[File.join('.', 'tours', '**', '*.rb')].map {|fn| File.basename(fn, ".rb")}.select {|fn| filter.size.zero? || filter.any?{|f| fn =~ /#{f}/}}
end

#total_runsObject



40
41
42
# File 'lib/tour_bus.rb', line 40

def total_runs
  tourists.size * concurrency * number    
end

#update_benchmarks(bm) ⇒ Object



29
30
31
32
33
# File 'lib/tour_bus.rb', line 29

def update_benchmarks(bm)
  synchronize do
    @benchmarks = @benchmarks.zip(bm).map { |a,b| a+b}
  end 
end

#update_stats(runs, tests, passes, fails, errors) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/tour_bus.rb', line 19

def update_stats(runs,tests,passes,fails,errors)
  synchronize do
    @runs += runs
    @tests += tests
    @passes += passes
    @fails += fails
    @errors += errors
  end
end