Skip to content

Commit

Permalink
Add benchmarking to tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmorgan committed May 29, 2024
1 parent b0b9237 commit 8d2137d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/go-test-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ jobs:
- name: Test
run: go test -v ./...

- name: Benchmark
run: go test -bench=. -count 5 ./...

build:
needs: test
strategy:
Expand Down
28 changes: 26 additions & 2 deletions parser/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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}
Expand All @@ -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)
}
}
13 changes: 13 additions & 0 deletions parser/tokenise_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package parser
import (
"bufio"
"errors"
"os"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -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)

}
}

0 comments on commit 8d2137d

Please sign in to comment.