@@ -4,10 +4,12 @@ import (
4
4
"bytes"
5
5
"encoding/binary"
6
6
"encoding/json"
7
+ "fmt"
7
8
"io"
8
9
"os"
9
10
"os/exec"
10
11
"reflect"
12
+ "strings"
11
13
"testing"
12
14
)
13
15
@@ -463,7 +465,7 @@ Date: Tue Apr 2 22:55:40 2019 -0700
463
465
464
466
cmd := exec .Command ("echo" , "hello" )
465
467
466
- files , err := Parse (cmd , f )
468
+ fileChan , err := Parse (cmd , f )
467
469
if test .Err {
468
470
if err == nil || err == io .EOF {
469
471
t .Fatalf ("expected error parsing patch, but got %v" , err )
@@ -473,6 +475,10 @@ Date: Tue Apr 2 22:55:40 2019 -0700
473
475
if err != nil {
474
476
t .Fatalf ("unexpected error parsing patch: %v" , err )
475
477
}
478
+ var files []* File
479
+ for file := range fileChan {
480
+ files = append (files , file )
481
+ }
476
482
477
483
if len (test .Output ) != len (files ) {
478
484
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
488
494
}
489
495
}
490
496
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
+
491
552
func newTestParser (input string , init bool ) * parser {
492
553
p := newParser (bytes .NewBufferString (input ))
493
554
if init {
0 commit comments