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