Skip to content

Commit

Permalink
Add benchmarks for CatFile, parseObject, and VerifyPack
Browse files Browse the repository at this point in the history
  • Loading branch information
ChimeraCoder committed Oct 29, 2016
1 parent 4acb495 commit 1077249
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
44 changes: 44 additions & 0 deletions cat-file_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gitgo

import (
"compress/zlib"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -238,3 +239,46 @@ func Test_Subdirectory(t *testing.T) {
t.Error(err)
}
}

// BenchmarkCatFile benchmarks the entire CatFile operation,
// including opening the current directory and searching for the location
// of the closest parent git repository
func BenchmarkCatFile(b *testing.B) {
const inputSha = SHA("af6e4fe91a8f9a0f3c03cbec9e1d2aac47345d67")
for i := 0; i < b.N; i++ {
_, _ = CatFile(inputSha) //, *RepoDir)
}
}

// BenchmarkParseObject benchmarks the operation that reads a compressed object file
// and parses it into a blob GitObject. The benchmark includes the zlib inflate operation.
// It uses an object that is not stored within a packfile.
func BenchmarkParseObject(b *testing.B) {
const inputSha = SHA("af6e4fe91a8f9a0f3c03cbec9e1d2aac47345d67")
basedir, err := os.Open(filepath.Join("test_data", ".git"))
if err != nil {
b.Error(err)
}
defer basedir.Close()
f, err := os.Open(filepath.Join("test_data", ".git", "objects", "af", "6e4fe91a8f9a0f3c03cbec9e1d2aac47345d67"))
if err != nil {
b.Error(err)
}
defer f.Close()

for i := 0; i < b.N; i++ {
f.Seek(0, io.SeekStart)
r, err := zlib.NewReader(f)
if err != nil {
b.Fatalf("error on iteration %d: %s", i, err)
}
obj, err := parseObj(r, inputSha, *basedir)
if err != nil {
b.Fatalf("error on iteration %d: %s", i, err)
}
blb := obj.(Blob)
if blb.size != "18" {
b.Errorf("Expected size 18 and found size %d", blb.size)
}
}
}
1 change: 0 additions & 1 deletion repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ func findGitDir(pwd *os.File) (dir *os.File, err error) {
candidate, err = os.Open(candidateName)
if err == nil {
return candidate, nil
break
}
if !os.IsNotExist(err) {
return nil, err
Expand Down
24 changes: 24 additions & 0 deletions verify-pack_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gitgo

import (
"io"
"log"
"os"
"path"
Expand Down Expand Up @@ -127,5 +128,28 @@ chain length = 2: 1 object
t.Errorf("Expected and result don't match:\n%+v\n%+v", expected, result)
}
*/
}

func BenchmarkVerifyPack(b *testing.B) {
packFile, err := os.Open(path.Join(RepoDir.Name(), "objects/pack/pack-d310969c4ba0ebfe725685fa577a1eec5ecb15b2.pack"))
if err != nil {
b.Error(err)
return
}
defer packFile.Close()

idxFile, err := os.Open(path.Join(RepoDir.Name(), "objects/pack/pack-d310969c4ba0ebfe725685fa577a1eec5ecb15b2.idx"))
if err != nil {
return
}

defer idxFile.Close()
for i := 0; i < b.N; i++ {
_, err := VerifyPack(packFile, idxFile)
if err != nil {
b.Errorf("error in iteration %d: %s", i, err)
}
packFile.Seek(0, io.SeekStart)
idxFile.Seek(0, io.SeekStart)
}
}

0 comments on commit 1077249

Please sign in to comment.