Class: GitBranchBenchmark
- Inherits:
-
Object
- Object
- GitBranchBenchmark
- Defined in:
- lib/git_branch_benchmark.rb
Constant Summary collapse
- VERSION =
0.1
Instance Attribute Summary collapse
-
#branches ⇒ Object
Returns the value of attribute branches.
-
#urls ⇒ Object
Returns the value of attribute urls.
-
#verbosity ⇒ Object
Returns the value of attribute verbosity.
Instance Method Summary collapse
- #avg(ary) ⇒ Object
-
#branch_realtime_measure ⇒ Object
When called with a block, runs the block code in each branch given.
-
#initialize ⇒ GitBranchBenchmark
constructor
A new instance of GitBranchBenchmark.
-
#print_stats(times) ⇒ Object
Prints stats from results in input hash.
- #run! ⇒ Object
Constructor Details
#initialize ⇒ GitBranchBenchmark
Returns a new instance of GitBranchBenchmark.
11 12 13 14 15 16 17 18 |
# File 'lib/git_branch_benchmark.rb', line 11 def initialize @branches = [] @urls = [] @verbosity = 0 @git = Git.open('.') @original_branch = @git.branch.name end |
Instance Attribute Details
#branches ⇒ Object
Returns the value of attribute branches.
9 10 11 |
# File 'lib/git_branch_benchmark.rb', line 9 def branches @branches end |
#urls ⇒ Object
Returns the value of attribute urls.
9 10 11 |
# File 'lib/git_branch_benchmark.rb', line 9 def urls @urls end |
#verbosity ⇒ Object
Returns the value of attribute verbosity.
9 10 11 |
# File 'lib/git_branch_benchmark.rb', line 9 def verbosity @verbosity end |
Instance Method Details
#avg(ary) ⇒ Object
53 54 55 56 |
# File 'lib/git_branch_benchmark.rb', line 53 def avg(ary) sum = eval ary.join('+') sum.to_f / ary.size end |
#branch_realtime_measure ⇒ Object
When called with a block, runs the block code in each branch given. If we were only initialized with one branch, adds the current branch to the list of branches to test against. Restores state by checking out original branch after execution, and returns hash of time results on each branch.
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/git_branch_benchmark.rb', line 24 def branch_realtime_measure times = {} @branches.each do |branch| @git.checkout(branch) times[branch] = Benchmark.realtime { yield } puts "Operation on #{branch} took #{times[branch]} seconds." if @verbosity >= 1 end @git.checkout(@original_branch) times end |
#print_stats(times) ⇒ Object
Prints stats from results in input hash
59 60 61 62 63 64 65 |
# File 'lib/git_branch_benchmark.rb', line 59 def print_stats(times) puts "Results are for test with #{@urls.size} urls.\n\n" @branches.each do |b| puts "Avg execution time for branch #{b}: #{avg(times[b])}" end end |
#run! ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/git_branch_benchmark.rb', line 38 def run! @branches << @original_branch unless @branches.size >= 2 results = @branches.inject({}) {|result, element| result.merge({ element => [] })} @urls.each do |url| branch_realtime_measure { Curl::Easy.perform(url) }.each_pair do |k, v| results[k] << v end end print_stats(results) end |