github.com/KEINOS/go-countline@v1.1.0/cl/benchmark_test.go (about)

     1  package cl_test
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/KEINOS/go-countline/cl"
     9  )
    10  
    11  func BenchmarkCountLines(b *testing.B) {
    12  	// 1 GiB size file
    13  	pathFile := filepath.Join("testdata", "data_Giant.txt")
    14  
    15  	expectNumLines := 72323529
    16  
    17  	// Open file
    18  	fileReader, err := os.Open(pathFile)
    19  	if err != nil {
    20  		b.Fatal(err)
    21  	}
    22  
    23  	b.Cleanup(func() {
    24  		fileReader.Close()
    25  	})
    26  
    27  	b.ResetTimer() // Begin benchmark
    28  
    29  	// Run function
    30  	actualNumLines, err := cl.CountLines(fileReader)
    31  	if err != nil {
    32  		b.Fatal(err)
    33  	}
    34  
    35  	b.StopTimer() // End benchmark
    36  
    37  	if expectNumLines != actualNumLines {
    38  		b.Fatalf(
    39  			"test %v failed: expect=%d, actual=%d",
    40  			b.Name(), expectNumLines, actualNumLines,
    41  		)
    42  	}
    43  }