@@ -3,6 +3,7 @@ package packfile
33import (
44 "strconv"
55 "strings"
6+ "testing"
67
78 "gopkg.in/src-d/go-git.v4/plumbing"
89
@@ -26,12 +27,12 @@ func (s *IndexSuite) TestLookupOffset(c *C) {
2627 e , ok := idx .LookupOffset (uint64 (o2 ))
2728 c .Assert (ok , Equals , true )
2829 c .Assert (e , NotNil )
29- c .Assert (e .Hash , Equals , s . toHash (o2 ))
30+ c .Assert (e .Hash , Equals , toHash (o2 ))
3031 c .Assert (e .Offset , Equals , uint64 (o2 ))
3132 }
3233 }
3334
34- h1 := s . toHash (o1 )
35+ h1 := toHash (o1 )
3536 idx .Add (h1 , uint64 (o1 ), 0 )
3637
3738 for o2 := 0 ; o2 < 10000 ; o2 += 100 {
@@ -43,7 +44,7 @@ func (s *IndexSuite) TestLookupOffset(c *C) {
4344 e , ok := idx .LookupOffset (uint64 (o2 ))
4445 c .Assert (ok , Equals , true )
4546 c .Assert (e , NotNil )
46- c .Assert (e .Hash , Equals , s . toHash (o2 ))
47+ c .Assert (e .Hash , Equals , toHash (o2 ))
4748 c .Assert (e .Offset , Equals , uint64 (o2 ))
4849 }
4950 }
@@ -56,31 +57,31 @@ func (s *IndexSuite) TestLookupHash(c *C) {
5657 for o1 := 0 ; o1 < 10000 ; o1 += 100 {
5758 for o2 := 0 ; o2 < 10000 ; o2 += 100 {
5859 if o2 >= o1 {
59- e , ok := idx .LookupHash (s . toHash (o2 ))
60+ e , ok := idx .LookupHash (toHash (o2 ))
6061 c .Assert (ok , Equals , false )
6162 c .Assert (e , IsNil )
6263 } else {
63- e , ok := idx .LookupHash (s . toHash (o2 ))
64+ e , ok := idx .LookupHash (toHash (o2 ))
6465 c .Assert (ok , Equals , true )
6566 c .Assert (e , NotNil )
66- c .Assert (e .Hash , Equals , s . toHash (o2 ))
67+ c .Assert (e .Hash , Equals , toHash (o2 ))
6768 c .Assert (e .Offset , Equals , uint64 (o2 ))
6869 }
6970 }
7071
71- h1 := s . toHash (o1 )
72+ h1 := toHash (o1 )
7273 idx .Add (h1 , uint64 (o1 ), 0 )
7374
7475 for o2 := 0 ; o2 < 10000 ; o2 += 100 {
7576 if o2 > o1 {
76- e , ok := idx .LookupHash (s . toHash (o2 ))
77+ e , ok := idx .LookupHash (toHash (o2 ))
7778 c .Assert (ok , Equals , false )
7879 c .Assert (e , IsNil )
7980 } else {
80- e , ok := idx .LookupHash (s . toHash (o2 ))
81+ e , ok := idx .LookupHash (toHash (o2 ))
8182 c .Assert (ok , Equals , true )
8283 c .Assert (e , NotNil )
83- c .Assert (e .Hash , Equals , s . toHash (o2 ))
84+ c .Assert (e .Hash , Equals , toHash (o2 ))
8485 c .Assert (e .Offset , Equals , uint64 (o2 ))
8586 }
8687 }
@@ -92,7 +93,7 @@ func (s *IndexSuite) TestSize(c *C) {
9293
9394 for o1 := 0 ; o1 < 1000 ; o1 ++ {
9495 c .Assert (idx .Size (), Equals , o1 )
95- h1 := s . toHash (o1 )
96+ h1 := toHash (o1 )
9697 idx .Add (h1 , uint64 (o1 ), 0 )
9798 }
9899}
@@ -107,16 +108,26 @@ func (s *IndexSuite) TestIdxFileEmpty(c *C) {
107108func (s * IndexSuite ) TestIdxFile (c * C ) {
108109 idx := NewIndex (0 )
109110 for o1 := 0 ; o1 < 1000 ; o1 ++ {
110- h1 := s . toHash (o1 )
111+ h1 := toHash (o1 )
111112 idx .Add (h1 , uint64 (o1 ), 0 )
112113 }
113114
114115 idx2 := NewIndexFromIdxFile (idx .ToIdxFile ())
115116 c .Assert (idx , DeepEquals , idx2 )
116117}
117118
118- func ( s * IndexSuite ) toHash (i int ) plumbing.Hash {
119+ func toHash (i int ) plumbing.Hash {
119120 is := strconv .Itoa (i )
120121 padding := strings .Repeat ("a" , 40 - len (is ))
121122 return plumbing .NewHash (padding + is )
122123}
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