Skip to content

Commit 30094fc

Browse files
author
Miccah Castorina
committed
Add benchmark and fix tests
1 parent bef745d commit 30094fc

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

gitdiff/apply_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,14 @@ func (at applyTest) run(t *testing.T, apply func(io.Writer, *Applier, *File) err
234234

235235
cmd := exec.Command("echo", "hello")
236236

237-
files, err := Parse(cmd, io.NopCloser(bytes.NewReader(patch)))
237+
fileChan, err := Parse(cmd, io.NopCloser(bytes.NewReader(patch)))
238238
if err != nil {
239239
t.Fatalf("failed to parse patch file: %v", err)
240240
}
241+
var files []*File
242+
for file := range fileChan {
243+
files = append(files, file)
244+
}
241245
if len(files) != 1 {
242246
t.Fatalf("patch should contain exactly one file, but it has %d", len(files))
243247
}

gitdiff/parser_test.go

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import (
44
"bytes"
55
"encoding/binary"
66
"encoding/json"
7+
"fmt"
78
"io"
89
"os"
910
"os/exec"
1011
"reflect"
12+
"strings"
1113
"testing"
1214
)
1315

@@ -463,7 +465,7 @@ Date: Tue Apr 2 22:55:40 2019 -0700
463465

464466
cmd := exec.Command("echo", "hello")
465467

466-
files, err := Parse(cmd, f)
468+
fileChan, err := Parse(cmd, f)
467469
if test.Err {
468470
if err == nil || err == io.EOF {
469471
t.Fatalf("expected error parsing patch, but got %v", err)
@@ -473,6 +475,10 @@ Date: Tue Apr 2 22:55:40 2019 -0700
473475
if err != nil {
474476
t.Fatalf("unexpected error parsing patch: %v", err)
475477
}
478+
var files []*File
479+
for file := range fileChan {
480+
files = append(files, file)
481+
}
476482

477483
if len(test.Output) != len(files) {
478484
t.Fatalf("incorrect number of parsed files: expected %d, actual %d", len(test.Output), len(files))
@@ -488,6 +494,61 @@ Date: Tue Apr 2 22:55:40 2019 -0700
488494
}
489495
}
490496

497+
func BenchmarkParse(b *testing.B) {
498+
var inputDiff string
499+
{
500+
builder := strings.Builder{}
501+
builder.WriteString(`commit 5d9790fec7d95aa223f3d20936340bf55ff3dcbe
502+
Author: Morton Haypenny <mhaypenny@example.com>
503+
Date: Tue Apr 2 22:55:40 2019 -0700
504+
505+
A file with multiple fragments.
506+
507+
The content is arbitrary.
508+
509+
`)
510+
fileDiff := func(i int) string {
511+
return fmt.Sprintf(`diff --git a/dir/file%[1]d.txt b/dir/file%[1]d.txt
512+
index ebe9fa54..fe103e1d 100644
513+
--- a/dir/file%[1]d.txt
514+
+++ b/dir/file%[1]d.txt
515+
@@ -3,6 +3,8 @@ fragment 1
516+
context line
517+
-old line 1
518+
-old line 2
519+
context line
520+
+new line 1
521+
+new line 2
522+
+new line 3
523+
context line
524+
-old line 3
525+
+new line 4
526+
+new line 5
527+
@@ -31,2 +33,2 @@ fragment 2
528+
context line
529+
-old line 4
530+
+new line 6
531+
`, i)
532+
}
533+
for i := 0; i < 1000; i++ {
534+
_, err := builder.WriteString(fileDiff(i))
535+
if err != nil {
536+
panic(err)
537+
}
538+
}
539+
inputDiff = builder.String()
540+
}
541+
for i := 0; i < b.N; i++ {
542+
reader := io.NopCloser(strings.NewReader(inputDiff))
543+
ch, err := Parse(&exec.Cmd{}, reader)
544+
if err != nil {
545+
panic(err)
546+
}
547+
for range ch {
548+
}
549+
}
550+
}
551+
491552
func newTestParser(input string, init bool) *parser {
492553
p := newParser(bytes.NewBufferString(input))
493554
if init {

0 commit comments

Comments
 (0)