added ls-tree to gitr
[rubygit.git] / benchmark.rb
blob022e4eeee9ef26881fbb6bcdf04a0ff6eea4b2a7
1 require 'fileutils'
2 require 'benchmark'
3 require 'rubygems'
4 require 'ruby-prof'
5 #require_gem 'git', '1.0.3'
6 require 'lib/git'
8 def main
9   @wbare = File.expand_path(File.join('tests', 'files', 'working.git'))
10   
11   in_temp_dir do
12     g = Git.clone(@wbare, 'test')
13     g.chdir do
14       
15       n = 40
16       result = RubyProf.profile do
17       puts "<pre>"
18       
19       Benchmark.bm(8) do |x|
20         run_code(x, 'objects') do
21           @commit = g.gcommit('1cc8667014381')
22           @tree = g.gtree('1cc8667014381^{tree}')
23           @blob = g.gblob('v2.5:example.txt')
24           @obj = g.object('v2.5:example.txt')
25         end
26         
27                 
28         x.report('config  ') do
29           n.times do
30             c = g.config
31             c = g.config('user.email')
32             c = g.config('user.email', 'schacon@gmail.com')
33           end
34         end
35         
36         x.report('diff    ') do
37           n.times do
38             g.diff('gitsearch1', 'v2.5').lines
39             g.diff('gitsearch1', 'v2.5').stats
40             g.diff('gitsearch1', 'v2.5').patch
41           end
42         end
43         
44         x.report('path    ') do
45           n.times do
46             g.dir.readable?
47             g.index.readable?
48             g.repo.readable?
49           end
50         end
51         
52         #------------------
53         x.report('status  ') do
54           n.times do
55             g.status['example.txt'].mode_index
56             s = g.status
57             s.added
58             s.added
59           end
60         end
62         #------------------
63         x.report('log     ') do
64           n.times do
65             log = g.log.between('v2.5').object('example.txt')
66             log.size
67             log.size
68             log.first
69             g.log.between('v2.5').object('example.txt').map { |c| c.message }
70             g.log.since("2 years ago").map { |c| c.message }
71           end
72         end
74         #------------------
75         x.report('branch  ') do
76           for i in 1..10 do
77             g.checkout('master')
78             g.branch('new_branch' + i.to_s).in_branch('test') do
79               g.current_branch
80               new_file('new_file_' + i.to_s, 'hello')
81               g.add
82               true
83             end
84             g.branch('new_branch').merge('new_branch' + i.to_s)
85             g.checkout('new_branch')
86           end
87         end
88         
89         #------------------
90         x.report('tree    ') do
91           for i in 1..10 do
92             tr = g.with_temp_index do
93                g.read_tree('new_branch' + i.to_s)
94                index = g.ls_files
95                g.write_tree
96              end
97           end
98         end rescue nil
100         x.report('archive ') do
101           n.times do
102             f = g.gcommit('v2.6').archive # returns path to temp file
103           end
104         end rescue nil
105    
106              
107       end
108     
109       end
111       # Print a graph profile to text
112       puts "</pre>"
113       printer = RubyProf::GraphHtmlPrinter.new(result)
114       printer.print(STDOUT, 1)
115       printer = RubyProf::FlatPrinter.new(result)
116       puts "<pre>"
117       printer.print(STDOUT, 1)
118       puts "</pre>"
119     end
120   end
124 def run_code(x, name, times = 30)
125   #result = RubyProf.profile do
127     x.report(name) do
128       for i in 1..times do
129         yield i
130       end
131     end
132   
133   #end
134   
135   # Print a graph profile to text
136   #printer = RubyProf::FlatPrinter.new(result)
137   #printer.print(STDOUT, 0)
140 def new_file(name, contents)
141   File.open(name, 'w') do |f|
142     f.puts contents
143   end
147 def in_temp_dir(remove_after = true)
148   filename = 'git_test' + Time.now.to_i.to_s + rand(300).to_s.rjust(3, '0')
149   tmp_path = File.join("/tmp/", filename)
150   FileUtils.mkdir(tmp_path)
151   Dir.chdir tmp_path do
152     yield tmp_path
153   end
154   FileUtils.rm_r(tmp_path) if remove_after
157 main()