Class: Tryouts

Inherits:
Object
  • Object
show all
Defined in:
lib/tryouts.rb,
lib/tryouts/cli.rb,
lib/tryouts/drill.rb,
lib/tryouts/stats.rb,
lib/tryouts/tryout.rb,
lib/tryouts/cli/run.rb,
lib/tryouts/drill/context.rb,
lib/tryouts/drill/sergeant/api.rb,
lib/tryouts/drill/sergeant/cli.rb,
lib/tryouts/drill/sergeant/benchmark.rb,
lib/tryouts/drill/sergeant/rbenchmark.rb

Overview

Stolen from: mongrel.rubyforge.org/browser/trunk/lib/mongrel/stats.rb

A very simple little class for doing some basic fast statistics sampling. You feed it either samples of numeric data you want measured or you call Stats.tick to get it to add a time delta between the last time you called it. When you’re done either call sum, sumsq, n, min, max, mean or sd to get the information. The other option is to just call dump and see everything.

It does all of this very fast and doesn’t take up any memory since the samples are not stored but instead all the values are calculated on the fly.

Defined Under Namespace

Modules: CLI Classes: BadDream, Drill, Exception, NoDrillType, OrderedHash, Stats, TooManyArgs, Tryout

Constant Summary collapse

VERSION =
"0.8.4"
HASH_TYPE =
(RUBY_VERSION =~ /1.9/) ? ::Hash : Tryouts::OrderedHash
@@loaded_files =

An Array of _tryouts.rb file paths that have been loaded.

[]
@@instances =

An Hash of Tryouts instances stored under the name of the Tryouts subclass.

HASH_TYPE.new
@@sysinfo =

An instance of SysInfo

SysInfo.new
@@debug =
false
@@verbose =
0
@@failed =

This will be true if any error occurred during any of the drills or parsing.

false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group = nil) ⇒ Tryouts

Returns a new instance of Tryouts.



101
102
103
104
105
106
# File 'lib/tryouts.rb', line 101

def initialize(group=nil)
  @group = group || "Default Group"
  @tryouts = HASH_TYPE.new
  @paths, @errors = [], []
  @command = nil
end

Instance Attribute Details

#command(name = nil, path = nil) ⇒ Object

Add a shell command to Rye::Cmd and save the command name in @@commands so it can be used as the default for drills



95
96
97
# File 'lib/tryouts.rb', line 95

def command
  @command
end

#dtypeObject

A Symbol representing the default drill type. One of: :cli, :api



89
90
91
# File 'lib/tryouts.rb', line 89

def dtype
  @dtype
end

#errorsObject (readonly)

An Array of exceptions that were raised during the tryouts that were not captured by a drill.



99
100
101
# File 'lib/tryouts.rb', line 99

def errors
  @errors
end

#group(name = nil) ⇒ Object

The name of this group of Tryout objects



87
88
89
# File 'lib/tryouts.rb', line 87

def group
  @group
end

#library(name = nil, *path) ⇒ Object

Require name. If path is supplied, it will “require path”.

  • name The name of the library in question (required). Stored as a Symbol to @library.

  • path Add a path to the front of $LOAD_PATH (optional). Use this if you want to load a specific copy of the library. Otherwise, it loads from the system path. If the path in specified in multiple arguments they are joined and expanded.

    library '/an/absolute/path'
    library __FILE__, '..', 'lib'
    


97
98
99
# File 'lib/tryouts.rb', line 97

def library
  @library
end

#pathsObject

An Array of file paths which populated this instance of Tryouts



91
92
93
# File 'lib/tryouts.rb', line 91

def paths
  @paths
end

#tryouts(*args, &block) ⇒ Object

Returns @tryouts.

Also acts as a stub for Tryouts#tryout in case someone specifies “tryouts ‘name’ do …” in the DSL.



93
94
95
# File 'lib/tryouts.rb', line 93

def tryouts
  @tryouts
end

Class Method Details

.command(*args) ⇒ Object

Calls Tryouts#command on the current instance of Tryouts

NOTE: this is a standalone DSL-syntax method.



141
142
143
# File 'lib/tryouts.rb', line 141

def self.command(*args)
  @@instances.last.command(*args)
end

.debug?Boolean

Returns:

  • (Boolean)


71
# File 'lib/tryouts.rb', line 71

def self.debug?; @@debug; end

.disable_debugObject



73
# File 'lib/tryouts.rb', line 73

def self.disable_debug; @@debug = false; end

.enable_debugObject



72
# File 'lib/tryouts.rb', line 72

def self.enable_debug; @@debug = true; end

.failed=(v) ⇒ Object



79
# File 'lib/tryouts.rb', line 79

def self.failed=(v); @@failed = v; end

.failed?Boolean

Returns:

  • (Boolean)


78
# File 'lib/tryouts.rb', line 78

def self.failed?; @@failed; end

.group(*args) ⇒ Object

Raises a Tryouts::Exception. group is not support in the standalone syntax because the group name is taken from the name of the class. See inherited.

NOTE: this is a standalone DSL-syntax method.



190
191
192
# File 'lib/tryouts.rb', line 190

def self.group(*args)
  raise "Group is already set: #{@@instances.last.group}"
end

.inherited(klass) ⇒ Object

Called when a new class inherits from Tryouts. This creates a new instance of Tryouts, sets group to the name of the new class, and adds the instance to @@instances.

NOTE: this is a standalone DSL-syntax method.



320
321
322
323
324
325
326
# File 'lib/tryouts.rb', line 320

def self.inherited(klass)
  to = @@instances[ klass ]
  to ||= Tryouts.new
  to.paths << __FILE__
  to.group = klass
  @@instances[to.group] = to
end

.instancesObject

Returns @@instances



82
# File 'lib/tryouts.rb', line 82

def self.instances; @@instances; end

.library(*args) ⇒ Object

Calls Tryouts#library on the current instance of Tryouts

NOTE: this is a standalone DSL-syntax method.



177
178
179
# File 'lib/tryouts.rb', line 177

def self.library(*args)
  @@instances.last.library(*args)
end

.parse_file(fpath) ⇒ Object

Parse a _tryouts.rb file. See Tryouts::CLI::Run for an example.

NOTE: this is an OO syntax method



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/tryouts.rb', line 274

def self.parse_file(fpath)
  raise "No such file: #{fpath}" unless File.exists?(fpath)
  file_content = File.read(fpath)
  to = Tryouts.new
  begin
    to.paths << fpath
    to.instance_eval file_content, fpath
    # After parsing the DSL, we'll know the group name.
    # If a Tryouts object already exists for that group
    # we'll use that instead and re-parse the DSL. 
    if @@instances.has_key? to.group
      to = @@instances[to.group]
      to.instance_eval file_content, fpath
    end
  rescue SyntaxError, LoadError, Exception, TypeError,
         RuntimeError, NoMethodError, NameError => ex
    to.errors << ex
    Tryouts.failed = true
    # It's helpful to display the group name
    file_content.match(/^group (.+?)$/) do |x,t|
      # We use eval as a quick cheat so we don't have
      # to parse all the various kinds of quotes.
      to.group = eval x.captures.first
    end
  end
  @@instances[to.group] = to
  to
end

.runObject

Run all Tryout objects in @tryouts

NOTE: this is an OO syntax method



306
307
308
309
310
311
312
313
# File 'lib/tryouts.rb', line 306

def self.run
  @@instances.each_pair do |group, inst|
    inst.tryouts.each_pair do |name,to|
      to.run
      to.report
    end
  end
end

.sysinfoObject

Returns @@sysinfo



84
# File 'lib/tryouts.rb', line 84

def self.sysinfo;   @@sysinfo;   end

.tryout(*args, &block) ⇒ Object

Calls Tryouts#tryout on the current instance of Tryouts

NOTE: this is a standalone DSL-syntax method.



227
228
229
# File 'lib/tryouts.rb', line 227

def self.tryout(*args, &block)
  @@instances.last.tryout(*args, &block)
end

.tryouts(*args, &block) ⇒ Object

An alias for Tryouts.tryout.



257
258
259
# File 'lib/tryouts.rb', line 257

def self.tryouts(*args, &block)
  tryout(args, &block)
end

.verboseObject



75
# File 'lib/tryouts.rb', line 75

def self.verbose; @@verbose; end

.verbose=(v) ⇒ Object



76
# File 'lib/tryouts.rb', line 76

def self.verbose=(v); @@verbose = (v == true) ? 1 : v; end

.xtryout(*args, &block) ⇒ Object

This method does nothing. It provides a quick way to disable a tryout.

NOTE: this is a standalone DSL-syntax method.



246
# File 'lib/tryouts.rb', line 246

def self.xtryout(*args, &block); end

.xtryouts(*args, &block) ⇒ Object

This method does nothing. It provides a quick way to disable a tryout.

NOTE: this is a standalone DSL-syntax method.



268
# File 'lib/tryouts.rb', line 268

def self.xtryouts(*args, &block); end

Instance Method Details

#find_tryout(name, dtype = nil) ⇒ Object

Find matching Tryout objects by name and filter by dtype if specified. Returns a Tryout object or nil.



233
234
235
236
237
# File 'lib/tryouts.rb', line 233

def find_tryout(name, dtype=nil)
  by_name = @tryouts.values.select { |t| t.name == name }
  by_name = by_name.select { |t| t.dtype == dtype } if dtype
  by_name.first  # by_name is an Array. We just want the Object. 
end

#from_block(b, &inline) ⇒ Object

Populate this Tryouts from a block. The block should contain calls to the external DSL methods: tryout, command, library, group



110
111
112
# File 'lib/tryouts.rb', line 110

def from_block(b, &inline)
  instance_eval &b
end

#reportObject

Execute Tryout#report for each Tryout in @tryouts



115
116
117
118
119
# File 'lib/tryouts.rb', line 115

def report
  successes = []
  @tryouts.each_pair { |n,to| successes << to.report }
  puts $/, "All your dreams came true" unless successes.member?(false)
end

#runObject

Execute Tryout#run for each Tryout in @tryouts



122
# File 'lib/tryouts.rb', line 122

def run; @tryouts.each_pair { |n,to| to.run }; end

#tryout(name, dtype = nil, command = nil, &block) ⇒ Object

Create a new Tryout object and add it to the list for this Tryouts class.

  • name is the name of the Tryout

  • dtype is the default drill type for the Tryout.

  • command when type is :cli, this is the name of the Rye::Box method that we’re testing. Otherwise ignored.

  • b is a block definition for the Tryout. See Tryout#from_block

NOTE: This is a DSL-only method and is not intended for OO use.

Raises:



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/tryouts.rb', line 201

def tryout(name, dtype=nil, command=nil, &block)
  return if name.nil?
  dtype ||= @dtype
  command ||= @command if dtype == :cli
  
  raise NoDrillType, name if dtype.nil?
  
  to = find_tryout(name, dtype)
  if to.nil?
    to = Tryouts::Tryout.new(name, dtype, command)
    @tryouts[name] = to
  end
  
  # Process the rest of the DSL
  begin
    to.from_block block if block
  rescue SyntaxError, LoadError, Exception, TypeError,
         RuntimeError, NoMethodError, NameError => ex
    @errors << ex
    Tryouts.failed = true
  end
  to
end

#xtryout(*args, &block) ⇒ Object

This method does nothing. It provides a quick way to disable a tryout.

NOTE: This is a DSL-only method and is not intended for OO use.



242
# File 'lib/tryouts.rb', line 242

def xtryout(*args, &block); end

#xtryouts(*args, &block) ⇒ Object

This method does nothing. It provides a quick way to disable a tryout.

NOTE: This is a DSL-only method and is not intended for OO use.



264
# File 'lib/tryouts.rb', line 264

def xtryouts(*args, &block); end