diff --git a/.github/workflows/go-test-build.yml b/.github/workflows/go-test-build.yml index c8d024d..f4ff3f0 100644 --- a/.github/workflows/go-test-build.yml +++ b/.github/workflows/go-test-build.yml @@ -30,6 +30,9 @@ jobs: - name: Test run: go test -v ./... + - name: Benchmark + run: go test -bench=. -count 5 ./... + build: needs: test strategy: diff --git a/parser/execute_test.go b/parser/execute_test.go index 8da91f1..b6a9dfb 100644 --- a/parser/execute_test.go +++ b/parser/execute_test.go @@ -20,7 +20,7 @@ func TestExecuteSmall(t *testing.T) { {opNoop, 2}, } startdata := make([]int, 65536) - outputBuf := bufio.NewWriter(os.Stdout) + outputBuf := bufio.NewWriter(&bufferWriter{}) inputBuf := bufio.NewReader(strings.NewReader("no input.")) data := Execute(startdata, program, inputBuf, outputBuf)[:10] want := []int{0, 0, 0, 0, 0, 0, 10, 0, 0, 0} @@ -42,7 +42,7 @@ func TestTokeniseExecuteSmall(t *testing.T) { >[<+>-]< and move the bit counter -]`))) startdata := make([]int, 65536) - outputBuf := bufio.NewWriter(os.Stdout) + outputBuf := bufio.NewWriter(&bufferWriter{}) inputBuf := bufio.NewReader(strings.NewReader("no input.")) data := Execute(startdata, program, inputBuf, outputBuf)[:10] want := []int{0, 0, 0, 170, 0, 0, 0, 0, 0, 0} @@ -51,3 +51,27 @@ func TestTokeniseExecuteSmall(t *testing.T) { t.Errorf("got %v want %v", data, want) } } + +type bufferWriter struct { + buf []byte +} + +func (writer *bufferWriter) Write(p []byte) (int, error) { + writer.buf = append(writer.buf, p...) + return 0, nil +} + +func BenchmarkExecute(b *testing.B) { + sourceFile, err := os.Open("../sample/pi-digits.bf") + if err != nil { + b.Errorf("error opening program: err:") + } + buff := bufio.NewReader(sourceFile) + inputBuf := bufio.NewReader(strings.NewReader("80\n")) + outputBuf := bufio.NewWriter(&bufferWriter{}) + program, _ := Tokenise(buff) + for i := 0; i < b.N; i++ { + startdata := make([]int, 65536) + Execute(startdata, program, inputBuf, outputBuf) + } +} diff --git a/parser/tokenise_test.go b/parser/tokenise_test.go index d0cff37..7ae7c27 100644 --- a/parser/tokenise_test.go +++ b/parser/tokenise_test.go @@ -3,6 +3,7 @@ package parser import ( "bufio" "errors" + "os" "reflect" "strings" "testing" @@ -164,3 +165,15 @@ func TestTokeniseError(t *testing.T) { }) } } + +func BenchmarkTokenise(b *testing.B) { + sourceFile, err := os.Open("../sample/pi-digits.bf") + if err != nil { + b.Errorf("error opening program: err:") + } + buff := bufio.NewReader(sourceFile) + for i := 0; i < b.N; i++ { + Tokenise(buff) + + } +}