averager 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Manifest.txt CHANGED
@@ -10,4 +10,5 @@ script/generate
10
10
  spec/averager_spec.rb
11
11
  spec/spec.opts
12
12
  spec/spec_helper.rb
13
- tasks/rspec.rake
13
+ spec/time_travel.rb
14
+ tasks/rspec.rake
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ require 'fileutils'
5
5
  require './lib/averager'
6
6
 
7
7
  Hoe.plugin :newgem
8
- Hoe.plugin :website
8
+ # Hoe.plugin :website
9
9
  # Hoe.plugin :cucumberfeatures
10
10
 
11
11
  # Generate all the Rake tasks
data/lib/averager.rb CHANGED
@@ -2,37 +2,94 @@ $:.unshift(File.dirname(__FILE__)) unless
2
2
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
3
 
4
4
  class Averager
5
- VERSION = '0.0.2'
5
+ VERSION = '0.0.3'
6
6
 
7
7
  def initialize(options = {})
8
8
  @started = Time.now
9
9
  @every = options[:every] || 1000
10
- if @expected_lines = options[:expected_lines]
11
- @digits = @expected_lines.to_i.to_s.length
10
+ if @expected = options[:expected]
11
+ @digits = @expected.to_i.to_s.length
12
12
  end
13
13
  if options[:digits]
14
14
  @digits = options[:digits]
15
15
  else
16
16
  @digits ||= 7
17
17
  end
18
+ @log_path = options[:log_path]
18
19
  @stream = options[:stream] || STDOUT
20
+ flush_stream_for_progress
21
+ @progress_bar = options[:progress_bar] == true
22
+ @i = 0
23
+ if block_given?
24
+ yield(self)
25
+ self.finish
26
+ end
19
27
  end
20
-
21
- def avg(i, status = nil)
22
- if i > 0 && i % @every == 0
23
- per_second = i / (Time.now - @started)
24
- out = nil
25
- if block_given?
26
- out = yield(:digits => @digits, :i => i, :per_second => per_second, :status => status)
27
- else
28
- out = "%0#{@digits}d: %.1f" % [i, per_second]
29
- out << ": #{status}" if status
28
+
29
+ def flush_stream_for_progress
30
+ if @log_path
31
+ FileUtils.mkdir_p(File.dirname(@log_path))
32
+ @stream = File.open(@log_path, "w")
33
+ else
34
+ @stream.print "\r"
35
+ end
36
+ end
37
+
38
+ def print_current?
39
+ @i % @every == 0
40
+ end
41
+
42
+ def print_current(status = nil)
43
+ per_second = @i / (Time.now - @started)
44
+ out = nil
45
+ if block_given?
46
+ out = yield(:digits => @digits, :iteration => @i, :per_second => per_second, :status => status)
47
+ else
48
+ out = "%#{@digits}d" % @i
49
+ if @expected
50
+ out << "/#{@expected}"
51
+ out << " %3.1f%" % (100 * (@i / @expected.to_f)) if @i <= @expected
30
52
  end
53
+ out << " (%.1f)" % per_second
54
+ out << ": #{status}" if status
55
+ end
56
+ if @progress_bar
57
+ flush_stream_for_progress
58
+ @stream.print out
59
+ else
31
60
  @stream.puts out
32
- @stream.flush
61
+ end
62
+ @stream.flush
63
+ end
64
+
65
+ def avg(*args)
66
+ status = nil
67
+ i_or_status = args.shift
68
+ if args.any?
69
+ status = args.shift
70
+ end
71
+ if i_or_status.is_a?(Numeric)
72
+ @i = i_or_status
73
+ else
74
+ if i_or_status.is_a?(String)
75
+ status = i_or_status
76
+ end
77
+ @i += 1
78
+ end
79
+ if print_current?
80
+ print_current(status)
33
81
  true
34
82
  else
35
83
  false
36
84
  end
37
85
  end
86
+
87
+ def finish
88
+ if !print_current?
89
+ print_current
90
+ end
91
+ @stream.puts "\n" if @progress_bar
92
+ @stream.puts "finished in #{Time.now - @started}"
93
+ @stream.close
94
+ end
38
95
  end
@@ -3,9 +3,80 @@ require File.dirname(__FILE__) + '/spec_helper.rb'
3
3
  # Time to add your specs!
4
4
  # http://rspec.info/
5
5
  describe "Place your specs here" do
6
+ before(:each) do
7
+ @log_path = File.expand_path(File.dirname("#{__FILE__}") + "/../tmp/status.log")
8
+ FileUtils.rm_f(@log_path)
9
+ TimeTravel.freeze_to Time.local(2009, 9, 9, 11, 20, 0)
10
+ end
11
+
12
+ it "should log to a specific logfile" do
13
+ avg = Averager.new(:log_path => @log_path, :every => 1, :digits => 3)
14
+ TimeTravel.jump(1)
15
+ avg.avg(100, "test")
16
+ TimeTravel.jump(1)
17
+ avg.avg(300, "test")
18
+ File.read(@log_path).should == %(100 (100.0): test\n300 (150.0): test\n)
19
+ end
20
+
21
+ it "should only log every x time" do
22
+ avg = Averager.new(:log_path => @log_path, :every => 600)
23
+ TimeTravel.jump(1)
24
+ avg.avg(100, "100")
25
+ TimeTravel.jump(1)
26
+ avg.avg(300, "300")
27
+ TimeTravel.jump(1)
28
+ avg.avg(600, "600")
29
+ avg.avg(601, "601")
30
+ File.read(@log_path).should == %( 600 (200.0): 600\n)
31
+ end
32
+
33
+ it "should include a percentage and estimated end time" do
34
+ avg = Averager.new(:log_path => @log_path, :expected => 1000, :every => 250)
35
+ TimeTravel.jump(1)
36
+ avg.avg(500, "500")
37
+ TimeTravel.jump(1)
38
+ avg.avg(750, "750")
39
+ File.read(@log_path).should == %( 500/1000 50.0% (500.0): 500\n 750/1000 75.0% (375.0): 750\n)
40
+ end
41
+
42
+ it "should print a progress when asked for" do
43
+ avg = Averager.new(:log_path => @log_path, :expected => 1000, :every => 250, :progress_bar => true)
44
+ TimeTravel.jump(1)
45
+ avg.avg(500, "500")
46
+ File.read(@log_path).should == %( 500/1000 50.0% (500.0): 500)
47
+ TimeTravel.jump(1)
48
+ avg.avg(750, "750")
49
+ File.read(@log_path).should == %( 750/1000 75.0% (375.0): 750)
50
+ end
6
51
 
7
- it "find this spec in spec directory" do
8
- # violated "Be sure to write your specs"
52
+ it "should not be mandatory to call with integer" do
53
+ avg = Averager.new(:log_path => @log_path, :expected => 2, :every => 1)
54
+ TimeTravel.jump(1)
55
+ avg.avg
56
+ TimeTravel.jump(1)
57
+ avg.avg
58
+ File.read(@log_path).should == %(1/2 50.0% (1.0)\n2/2 100.0% (1.0)\n)
9
59
  end
10
60
 
61
+ it "should be able to be called with a block" do
62
+ Averager.new(:log_path => @log_path, :expected => 2, :every => 1) do |avg|
63
+ TimeTravel.jump(1)
64
+ avg.avg
65
+ TimeTravel.jump(1)
66
+ avg.avg
67
+ end
68
+ File.read(@log_path).should == %(1/2 50.0% (1.0)\n2/2 100.0% (1.0)\nfinished in 2.0\n)
69
+ end
70
+
71
+ it "should print the last status when finished before" do
72
+ Averager.new(:log_path => @log_path, :expected => 3, :every => 4) do |avg|
73
+ TimeTravel.jump(1)
74
+ avg.avg
75
+ TimeTravel.jump(1)
76
+ avg.avg
77
+ TimeTravel.jump(1)
78
+ avg.avg
79
+ end
80
+ File.read(@log_path).should == %(3/3 100.0% (1.0)\nfinished in 3.0\n)
81
+ end
11
82
  end
data/spec/spec_helper.rb CHANGED
@@ -8,3 +8,4 @@ end
8
8
 
9
9
  $:.unshift(File.dirname(__FILE__) + '/../lib')
10
10
  require 'averager'
11
+ require 'spec/time_travel'
@@ -0,0 +1,29 @@
1
+ module TimeTravel
2
+ @@offset = 0
3
+
4
+ def self.now
5
+ if @@frozen_time
6
+ @@frozen_time + offset
7
+ else
8
+ Time.now_without_time_travel + offset
9
+ end
10
+ end
11
+
12
+ def self.freeze_to(time)
13
+ @@frozen_time = time
14
+ end
15
+
16
+ def self.jump(seconds)
17
+ @@offset = offset + seconds
18
+ end
19
+
20
+ def self.offset
21
+ @@offset
22
+ end
23
+ end
24
+
25
+
26
+ class << Time
27
+ alias_method :now_without_time_travel, :now
28
+ def now; TimeTravel.now; end
29
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: averager
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tobias Schwab
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-02 00:00:00 +02:00
18
+ date: 2010-08-06 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -74,6 +74,7 @@ files:
74
74
  - spec/averager_spec.rb
75
75
  - spec/spec.opts
76
76
  - spec/spec_helper.rb
77
+ - spec/time_travel.rb
77
78
  - tasks/rspec.rake
78
79
  has_rdoc: true
79
80
  homepage: http://github.com/tobstarr/averager