Class: Kronk::Player
- Inherits:
-
QueueRunner
- Object
- QueueRunner
- Kronk::Player
- Defined in:
- lib/kronk/player.rb
Overview
The Player class is used for running multiple requests and comparisons and providing useful output through inherited Player classes. Kronk includes a Suite (test-like), a Stream (chunked), and a Benchmark output.
Defined Under Namespace
Classes: Benchmark, Download, InputReader, RequestParser, Stream, Suite, TSV
Instance Attribute Summary collapse
-
#concurrency ⇒ Object
Returns the value of attribute concurrency.
-
#input ⇒ Object
Returns the value of attribute input.
-
#mutex ⇒ Object
Returns the value of attribute mutex.
-
#qps ⇒ Object
Returns the value of attribute qps.
Attributes inherited from QueueRunner
#count, #number, #queue, #reader_thread, #threads
Class Method Summary collapse
-
.new_type(type, opts = {}) ⇒ Object
Instantiate a new type of player, typically :suite, :stream, or :benchmark.
Instance Method Summary collapse
-
#compare(uri1, uri2, opts = {}, &block) ⇒ Object
Process the queue to compare two uris.
-
#from_io(io, parser = nil) ⇒ Object
Populate the queue by reading from the given IO instance and parsing it into kronk options.
-
#initialize(opts = {}) ⇒ Player
constructor
Create a new Player for batch diff or response validation.
-
#request(uri, opts = {}, &block) ⇒ Object
Process the queue to request uris.
-
#run(opts = {}) ⇒ Object
Similar to QueueRunner#run but yields a Kronk instance.
-
#trigger_result(kronk, err, &block) ⇒ Object
Trigger a single kronk result callback.
Methods inherited from QueueRunner
#concurrently, #finish, #finished?, #kill, #on, #periodically, #start_input!, #stop_input!, #trigger, #until_finished, #yield_queue_item
Constructor Details
#initialize(opts = {}) ⇒ Player
Create a new Player for batch diff or response validation. Supported options are:
- :io
-
IO - The IO instance to read from
- :parser
-
Class - The IO parser to use.
- :concurrency
-
Fixnum - Maximum number of concurrent items to process
- :qps
-
Fixnum - Number of queries to process per second
42 43 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 |
# File 'lib/kronk/player.rb', line 42 def initialize opts={} super @concurrency = opts[:concurrency] @concurrency = 1 if !@concurrency || @concurrency <= 0 @qps = opts[:qps] @input = InputReader.new opts[:io], opts[:parser] @last_req = nil @mutex = Mutex.new @on_input = Proc.new do stop_input! if !@number && @input.eof? && !@input.io.nil? @last_req = @input.get_next || @queue.last || @last_req || {} end on(:input, &@on_input) on(:interrupt){ interrupt and return if respond_to?(:interrupt) complete if respond_to?(:complete) exit 2 } on(:start){ start if respond_to?(:start) } on(:complete){ complete if respond_to?(:complete) } end |
Instance Attribute Details
#concurrency ⇒ Object
Returns the value of attribute concurrency.
32 33 34 |
# File 'lib/kronk/player.rb', line 32 def concurrency @concurrency end |
#input ⇒ Object
Returns the value of attribute input.
32 33 34 |
# File 'lib/kronk/player.rb', line 32 def input @input end |
#mutex ⇒ Object
Returns the value of attribute mutex.
32 33 34 |
# File 'lib/kronk/player.rb', line 32 def mutex @mutex end |
#qps ⇒ Object
Returns the value of attribute qps.
32 33 34 |
# File 'lib/kronk/player.rb', line 32 def qps @qps end |
Class Method Details
.new_type(type, opts = {}) ⇒ Object
Instantiate a new type of player, typically :suite, :stream, or :benchmark.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/kronk/player.rb', line 15 def self.new_type type, opts={} klass = case type.to_s when /^(Player::)?benchmark$/i then Benchmark when /^(Player::)?download$/i then Download when /^(Player::)?stream$/i then Stream when /^(Player::)?suite$/i then Suite when /^(Player::)?tsv$/i then TSV else Kronk.find_const type.to_s end klass.new opts end |
Instance Method Details
#compare(uri1, uri2, opts = {}, &block) ⇒ Object
Process the queue to compare two uris. If options are given, they are merged into every request.
90 91 92 93 94 95 96 |
# File 'lib/kronk/player.rb', line 90 def compare uri1, uri2, opts={}, &block on(:result){|(kronk, err)| trigger_result(kronk, err, &block) } run opts do |kronk| kronk.compare uri1, uri2 end end |
#from_io(io, parser = nil) ⇒ Object
Populate the queue by reading from the given IO instance and parsing it into kronk options.
Default parser is RequestParser. See InputReader for parser requirements.
79 80 81 82 83 |
# File 'lib/kronk/player.rb', line 79 def from_io io, parser=nil @input.io = io @input.parser = parser if parser @input end |
#request(uri, opts = {}, &block) ⇒ Object
Process the queue to request uris. If options are given, they are merged into every request.
103 104 105 106 107 108 109 |
# File 'lib/kronk/player.rb', line 103 def request uri, opts={}, &block on(:result){|(kronk, err)| trigger_result(kronk, err, &block) } run opts do |kronk| kronk.request uri end end |
#run(opts = {}) ⇒ Object
Similar to QueueRunner#run but yields a Kronk instance.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/kronk/player.rb', line 115 def run opts={} if @qps method = :periodically args = [(1.0 / @qps.to_f), @concurrency] else method = :concurrently args = [@concurrency] end send(method, *args) do |kronk_opts| err = nil kronk = Kronk.new kronk_opts.merge(opts) begin yield kronk rescue *RESCUABLE => e err = e end [kronk, err] end end |
#trigger_result(kronk, err, &block) ⇒ Object
Trigger a single kronk result callback.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/kronk/player.rb', line 142 def trigger_result kronk, err, &block if block_given? if block.arity > 2 || block.arity < 0 block.call kronk, err, @mutex else @mutex.synchronize do block.call kronk, err end end elsif err && respond_to?(:error) error err, kronk elsif respond_to?(:result) result kronk end end |