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, Tryout

Constant Summary collapse

VERSION =
"0.8.1"
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.



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

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



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

def command
  @command
end

#dtypeObject

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



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

def dtype
  @dtype
end

#errorsObject (readonly)

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



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

def errors
  @errors
end

#group(name = nil) ⇒ Object

The name of this group of Tryout objects



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

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'
    


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

def library
  @library
end

#pathsObject

An Array of file paths which populated this instance of Tryouts



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

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.



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

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.



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

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

.debug?Boolean

Returns:

  • (Boolean)


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

def self.debug?; @@debug; end

.disable_debugObject



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

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

.enable_debugObject



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

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

.failed=(v) ⇒ Object



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

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

.failed?Boolean

Returns:

  • (Boolean)


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

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.



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

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.



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

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

.instancesObject

Returns @@instances



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

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.



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

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



273
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
# File 'lib/tryouts.rb', line 273

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



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

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



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

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.



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

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

.tryouts(*args, &block) ⇒ Object

An alias for Tryouts.tryout.



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

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

.verboseObject



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

def self.verbose; @@verbose; end

.verbose=(v) ⇒ Object



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

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.



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

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.



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

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.



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

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



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

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

#reportObject

Execute Tryout#report for each Tryout in @tryouts



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

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



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

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:



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

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.



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

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.



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

def xtryouts(*args, &block); end