Skip to content

Commit

Permalink
Cleanup benchmark interface
Browse files Browse the repository at this point in the history
  • Loading branch information
bhperry committed Aug 23, 2024
1 parent 3604b70 commit 8b7335d
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 77 deletions.
7 changes: 3 additions & 4 deletions tools/benchmark/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Config struct {
Description string

// New returns the benchmark to be executed
New func() Benchmark
New func(win *opengl.Window) (Benchmark, error)
// Duration sets the maximum duration to run the benchmark
Duration time.Duration
// WindowConfig defines the input parameters to the benchmark's window
Expand Down Expand Up @@ -53,8 +53,8 @@ func (c Config) Run() (*Stats, error) {
}
defer win.Destroy()

benchmark := c.New()
if err := benchmark.Init(win); err != nil {
benchmark, err := c.New(win)
if err != nil {
return nil, err
}

Expand Down Expand Up @@ -90,7 +90,6 @@ loop:

// Benchmark provides hooks into the stages of a window's lifecycle
type Benchmark interface {
Init(win *opengl.Window) error
Step(win *opengl.Window)
}

Expand Down
64 changes: 33 additions & 31 deletions tools/benchmark/imdraw_bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,25 @@ func init() {
)
}

func newStaticTriangles() Benchmark {
return &staticTriangles{rows: 32, cols: 32}
func newStaticTriangles(win *opengl.Window) (Benchmark, error) {
bounds := win.Bounds()
width := bounds.W()
height := bounds.H()
rows, cols := 32, 32
cell := gridCell(width, height, rows, cols)
benchmark := &staticTriangles{
imd: tri(cell),
rows: rows,
cols: cols,
cell: cell,
}
return benchmark, nil
}

type staticTriangles struct {
imd *imdraw.IMDraw
rows, cols int
width, height float64
cell pixel.Vec
}

func (st *staticTriangles) Init(win *opengl.Window) error {
bounds := win.Bounds()
st.width = bounds.W()
st.height = bounds.H()
st.cell = gridCell(st.width, st.height, st.rows, st.cols)
st.imd = tri(st.cell)
return nil
imd *imdraw.IMDraw
rows, cols int
cell pixel.Vec
}

func (st *staticTriangles) Step(win *opengl.Window) {
Expand All @@ -62,25 +63,26 @@ func (st *staticTriangles) Step(win *opengl.Window) {
}
}

func newMovingTriangles() Benchmark {
return &movingTriangles{rows: 32, cols: 32}
func newMovingTriangles(win *opengl.Window) (Benchmark, error) {
bounds := win.Bounds()
width := bounds.W()
height := bounds.H()
rows, cols := 32, 32
cell := gridCell(width, height, rows, cols)
benchmark := &movingTriangles{
imd: tri(cell),
rows: rows,
cols: cols,
cell: cell,
}
return benchmark, nil
}

type movingTriangles struct {
imd *imdraw.IMDraw
rows, cols int
width, height float64
cell pixel.Vec
counter int
}

func (mt *movingTriangles) Init(win *opengl.Window) error {
bounds := win.Bounds()
mt.width = bounds.W()
mt.height = bounds.H()
mt.cell = gridCell(mt.width, mt.height, mt.rows, mt.cols)
mt.imd = tri(mt.cell)
return nil
imd *imdraw.IMDraw
rows, cols int
cell pixel.Vec
counter int
}

func (mt *movingTriangles) Step(win *opengl.Window) {
Expand Down
88 changes: 46 additions & 42 deletions tools/benchmark/sprite_bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,66 +40,70 @@ func init() {
)
}

func newSpriteStatic() Benchmark {
return &spriteStatic{rows: 32, cols: 32}
}

type spriteStatic struct {
sprite *pixel.Sprite
rows, cols int
width, height float64
cell pixel.Vec
}

func (ss *spriteStatic) Init(win *opengl.Window) error {
func newSpriteStatic(win *opengl.Window) (Benchmark, error) {
sprite, err := loadSprite(logoPath, logoFrame)
if err != nil {
return err
return nil, err
}
ss.sprite = sprite

bounds := win.Bounds()
ss.width = bounds.W()
ss.height = bounds.H()
ss.cell = gridCell(ss.width, ss.height, ss.rows, ss.cols)
return nil
width := bounds.W()
height := bounds.H()
rows, cols := 32, 32
benchmark := &spriteStatic{
sprite: sprite,
rows: rows,
cols: rows,
cell: gridCell(width, height, rows, cols),
}
return benchmark, nil
}

type spriteStatic struct {
sprite *pixel.Sprite
rows, cols int
cell pixel.Vec
}

func (ss *spriteStatic) Step(win *opengl.Window) {
win.Clear(backgroundColor)
spriteGrid(ss.sprite, win, ss.rows, ss.cols, ss.cell)
}

func newSpriteStaticBatched() Benchmark {
return &spriteStaticBatched{rows: 32, cols: 32}
}

type spriteStaticBatched struct {
sprite *pixel.Sprite
batch *pixel.Batch
rows, cols int
width, height float64
cell pixel.Vec
}

func (sb *spriteStaticBatched) Init(win *opengl.Window) error {
func newSpriteStaticBatched(win *opengl.Window) (Benchmark, error) {
sprite, err := loadSprite(logoPath, logoFrame)
if err != nil {
return err
return nil, err
}
sb.sprite = sprite
sb.batch = pixel.NewBatch(&pixel.TrianglesData{}, sprite.Picture())

bounds := win.Bounds()
sb.width = bounds.W()
sb.height = bounds.H()
sb.cell = gridCell(sb.width, sb.height, sb.rows, sb.cols)
return nil
width := bounds.W()
height := bounds.H()
rows, cols := 32, 32

benchmark := &spriteStaticBatched{
sprite: sprite,
batch: pixel.NewBatch(&pixel.TrianglesData{}, sprite.Picture()),
rows: rows,
cols: cols,
cell: gridCell(width, height, rows, cols),
}

return benchmark, nil
}

type spriteStaticBatched struct {
sprite *pixel.Sprite
batch *pixel.Batch
rows, cols int
cell pixel.Vec
}

func (sb *spriteStaticBatched) Step(win *opengl.Window) {
func (ssb *spriteStaticBatched) Step(win *opengl.Window) {
win.Clear(backgroundColor)
sb.batch.Clear()
spriteGrid(sb.sprite, sb.batch, sb.rows, sb.cols, sb.cell)
sb.batch.Draw(win)
ssb.batch.Clear()
spriteGrid(ssb.sprite, ssb.batch, ssb.rows, ssb.cols, ssb.cell)
ssb.batch.Draw(win)
}

func spriteGrid(sprite *pixel.Sprite, target pixel.Target, rows, cols int, cell pixel.Vec) {
Expand Down

0 comments on commit 8b7335d

Please sign in to comment.