@@ -3,6 +3,7 @@ package packfile
3
3
import (
4
4
"strconv"
5
5
"strings"
6
+ "testing"
6
7
7
8
"gopkg.in/src-d/go-git.v4/plumbing"
8
9
@@ -26,12 +27,12 @@ func (s *IndexSuite) TestLookupOffset(c *C) {
26
27
e , ok := idx .LookupOffset (uint64 (o2 ))
27
28
c .Assert (ok , Equals , true )
28
29
c .Assert (e , NotNil )
29
- c .Assert (e .Hash , Equals , s . toHash (o2 ))
30
+ c .Assert (e .Hash , Equals , toHash (o2 ))
30
31
c .Assert (e .Offset , Equals , uint64 (o2 ))
31
32
}
32
33
}
33
34
34
- h1 := s . toHash (o1 )
35
+ h1 := toHash (o1 )
35
36
idx .Add (h1 , uint64 (o1 ), 0 )
36
37
37
38
for o2 := 0 ; o2 < 10000 ; o2 += 100 {
@@ -43,7 +44,7 @@ func (s *IndexSuite) TestLookupOffset(c *C) {
43
44
e , ok := idx .LookupOffset (uint64 (o2 ))
44
45
c .Assert (ok , Equals , true )
45
46
c .Assert (e , NotNil )
46
- c .Assert (e .Hash , Equals , s . toHash (o2 ))
47
+ c .Assert (e .Hash , Equals , toHash (o2 ))
47
48
c .Assert (e .Offset , Equals , uint64 (o2 ))
48
49
}
49
50
}
@@ -56,31 +57,31 @@ func (s *IndexSuite) TestLookupHash(c *C) {
56
57
for o1 := 0 ; o1 < 10000 ; o1 += 100 {
57
58
for o2 := 0 ; o2 < 10000 ; o2 += 100 {
58
59
if o2 >= o1 {
59
- e , ok := idx .LookupHash (s . toHash (o2 ))
60
+ e , ok := idx .LookupHash (toHash (o2 ))
60
61
c .Assert (ok , Equals , false )
61
62
c .Assert (e , IsNil )
62
63
} else {
63
- e , ok := idx .LookupHash (s . toHash (o2 ))
64
+ e , ok := idx .LookupHash (toHash (o2 ))
64
65
c .Assert (ok , Equals , true )
65
66
c .Assert (e , NotNil )
66
- c .Assert (e .Hash , Equals , s . toHash (o2 ))
67
+ c .Assert (e .Hash , Equals , toHash (o2 ))
67
68
c .Assert (e .Offset , Equals , uint64 (o2 ))
68
69
}
69
70
}
70
71
71
- h1 := s . toHash (o1 )
72
+ h1 := toHash (o1 )
72
73
idx .Add (h1 , uint64 (o1 ), 0 )
73
74
74
75
for o2 := 0 ; o2 < 10000 ; o2 += 100 {
75
76
if o2 > o1 {
76
- e , ok := idx .LookupHash (s . toHash (o2 ))
77
+ e , ok := idx .LookupHash (toHash (o2 ))
77
78
c .Assert (ok , Equals , false )
78
79
c .Assert (e , IsNil )
79
80
} else {
80
- e , ok := idx .LookupHash (s . toHash (o2 ))
81
+ e , ok := idx .LookupHash (toHash (o2 ))
81
82
c .Assert (ok , Equals , true )
82
83
c .Assert (e , NotNil )
83
- c .Assert (e .Hash , Equals , s . toHash (o2 ))
84
+ c .Assert (e .Hash , Equals , toHash (o2 ))
84
85
c .Assert (e .Offset , Equals , uint64 (o2 ))
85
86
}
86
87
}
@@ -92,7 +93,7 @@ func (s *IndexSuite) TestSize(c *C) {
92
93
93
94
for o1 := 0 ; o1 < 1000 ; o1 ++ {
94
95
c .Assert (idx .Size (), Equals , o1 )
95
- h1 := s . toHash (o1 )
96
+ h1 := toHash (o1 )
96
97
idx .Add (h1 , uint64 (o1 ), 0 )
97
98
}
98
99
}
@@ -107,16 +108,26 @@ func (s *IndexSuite) TestIdxFileEmpty(c *C) {
107
108
func (s * IndexSuite ) TestIdxFile (c * C ) {
108
109
idx := NewIndex (0 )
109
110
for o1 := 0 ; o1 < 1000 ; o1 ++ {
110
- h1 := s . toHash (o1 )
111
+ h1 := toHash (o1 )
111
112
idx .Add (h1 , uint64 (o1 ), 0 )
112
113
}
113
114
114
115
idx2 := NewIndexFromIdxFile (idx .ToIdxFile ())
115
116
c .Assert (idx , DeepEquals , idx2 )
116
117
}
117
118
118
- func ( s * IndexSuite ) toHash (i int ) plumbing.Hash {
119
+ func toHash (i int ) plumbing.Hash {
119
120
is := strconv .Itoa (i )
120
121
padding := strings .Repeat ("a" , 40 - len (is ))
121
122
return plumbing .NewHash (padding + is )
122
123
}
124
+
125
+ func BenchmarkIndexConstruction (b * testing.B ) {
126
+ b .ReportAllocs ()
127
+
128
+ idx := NewIndex (0 )
129
+ for o := 0 ; o < 1e6 * b .N ; o += 100 {
130
+ h1 := toHash (o )
131
+ idx .Add (h1 , uint64 (o ), 0 )
132
+ }
133
+ }
0 commit comments