apa102: refactor benchmarks (#252)

pull/1/head
Ben Lazarus 8 years ago committed by M-A
parent a948b6ab2e
commit a90b2edbb3

@ -636,15 +636,16 @@ func TestInit(t *testing.T) {
// //
func BenchmarkWriteWhite(b *testing.B) { type genColor func(int) [3]byte
b.ReportAllocs()
pixels := [150 * 3]byte{} func benchmarkWrite(b *testing.B, o Opts, length int, f genColor) {
for i := range pixels { var pixels []byte
pixels[i] = 0xFF for i := 0; i < length; i++ {
c := f(i)
pixels = append(pixels, c[:]...)
} }
o := DefaultOpts o.NumPixels = length
o.NumPixels = len(pixels) / 3 b.ReportAllocs()
o.Intensity = 250
d, _ := New(spitest.NewRecordRaw(ioutil.Discard), &o) d, _ := New(spitest.NewRecordRaw(ioutil.Discard), &o)
_, _ = d.Write(pixels[:]) _, _ = d.Write(pixels[:])
b.ResetTimer() b.ResetTimer()
@ -653,40 +654,43 @@ func BenchmarkWriteWhite(b *testing.B) {
} }
} }
func BenchmarkWriteWhite(b *testing.B) {
o := DefaultOpts
o.Intensity = 250
benchmarkWrite(b, o, 150, func(i int) [3]byte { return [3]byte{0xFF, 0xFF, 0xFF} })
}
func BenchmarkWriteDim(b *testing.B) { func BenchmarkWriteDim(b *testing.B) {
b.ReportAllocs()
pixels := [150 * 3]byte{}
for i := range pixels {
pixels[i] = 1
}
o := DefaultOpts o := DefaultOpts
o.NumPixels = len(pixels) / 3
o.Intensity = 250 o.Intensity = 250
d, _ := New(spitest.NewRecordRaw(ioutil.Discard), &o) benchmarkWrite(b, o, 150, func(i int) [3]byte { return [3]byte{0x01, 0x01, 0x01} })
_, _ = d.Write(pixels[:])
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = d.Write(pixels[:])
}
} }
func BenchmarkWriteBlack(b *testing.B) { func BenchmarkWriteBlack(b *testing.B) {
b.ReportAllocs()
pixels := [150 * 3]byte{}
o := DefaultOpts o := DefaultOpts
o.NumPixels = len(pixels) / 3
o.Intensity = 250 o.Intensity = 250
d, _ := New(spitest.NewRecordRaw(ioutil.Discard), &o) benchmarkWrite(b, o, 150, func(i int) [3]byte { return [3]byte{0x0, 0x0, 0x0} })
_, _ = d.Write(pixels[:]) }
b.ResetTimer()
for i := 0; i < b.N; i++ { func genColorfulPixel(x int) [3]byte {
_, _ = d.Write(pixels[:]) i := x * 3
return [3]byte{uint8(i) + uint8(i>>8),
uint8(i+1) + uint8(i+1>>8),
uint8(i+2) + uint8(i+2>>8),
} }
} }
func BenchmarkWriteColorful(b *testing.B) { func BenchmarkWriteColorful(b *testing.B) {
o := DefaultOpts
o.Intensity = 250
o.Temperature = 5000
benchmarkWrite(b, o, 150, genColorfulPixel)
}
func BenchmarkWriteColorfulVariation(b *testing.B) {
// Continuously vary the lookup tables.
b.ReportAllocs() b.ReportAllocs()
pixels := [150 * 3]byte{} pixels := [256 * 3]byte{}
for i := range pixels { for i := range pixels {
pixels[i] = uint8(i) + uint8(i>>8) pixels[i] = uint8(i) + uint8(i>>8)
} }
@ -698,21 +702,37 @@ func BenchmarkWriteColorful(b *testing.B) {
_, _ = d.Write(pixels[:]) _, _ = d.Write(pixels[:])
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
d.Intensity = uint8(i)
d.Temperature = uint16((3000 + i) & 0x1FFF)
_, _ = d.Write(pixels[:]) _, _ = d.Write(pixels[:])
} }
} }
func BenchmarkDrawNRGBAColorful(b *testing.B) { func fillImage(img image.Image, f genColor) {
// Takes the fast path. switch im := img.(type) {
b.ReportAllocs() case *image.NRGBA:
img := image.NewNRGBA(image.Rect(0, 0, 150, 1)) for x := 0; x < im.Bounds().Dx(); x++ {
for i := range img.Pix { for y := 0; y < im.Bounds().Dy(); y++ {
img.Pix[i] = uint8(i) + uint8(i>>8) pix := f(x)
c := color.NRGBA{R: pix[0], G: pix[1], B: pix[2], A: 255}
im.Set(x, y, c)
}
}
case *image.RGBA:
for x := 0; x < im.Bounds().Dx(); x++ {
for y := 0; y < im.Bounds().Dy(); y++ {
pix := f(x)
c := color.NRGBA{R: pix[0], G: pix[1], B: pix[2], A: 255}
im.Set(x, y, c)
}
}
} }
o := DefaultOpts }
func benchmarkDraw(b *testing.B, o Opts, img image.Image, f genColor) {
fillImage(img, f)
o.NumPixels = img.Bounds().Max.X o.NumPixels = img.Bounds().Max.X
o.Intensity = 250 b.ReportAllocs()
o.Temperature = 5000
d, _ := New(spitest.NewRecordRaw(ioutil.Discard), &o) d, _ := New(spitest.NewRecordRaw(ioutil.Discard), &o)
r := d.Bounds() r := d.Bounds()
p := image.Point{} p := image.Point{}
@ -727,50 +747,18 @@ func BenchmarkDrawNRGBAColorful(b *testing.B) {
} }
} }
func BenchmarkDrawRGBAColorful(b *testing.B) { func BenchmarkDrawNRGBAColorful(b *testing.B) {
// Takes the slow path.
b.ReportAllocs()
img := image.NewRGBA(image.Rect(0, 0, 256, 1))
for i := range img.Pix {
img.Pix[i] = uint8(i) + uint8(i>>8)
}
o := DefaultOpts o := DefaultOpts
o.NumPixels = img.Bounds().Max.X
o.Intensity = 250 o.Intensity = 250
o.Temperature = 5000 o.Temperature = 5000
d, _ := New(spitest.NewRecordRaw(ioutil.Discard), &o) benchmarkDraw(b, o, image.NewNRGBA(image.Rect(0, 0, 150, 1)), genColorfulPixel)
r := d.Bounds()
p := image.Point{}
if err := d.Draw(r, img, p); err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := d.Draw(r, img, p); err != nil {
b.Fatal(err)
}
}
} }
func BenchmarkWriteColorfulVariation(b *testing.B) { func BenchmarkDrawRGBAColorful(b *testing.B) {
// Continuously vary the lookup tables.
b.ReportAllocs()
pixels := [256 * 3]byte{}
for i := range pixels {
pixels[i] = uint8(i) + uint8(i>>8)
}
o := DefaultOpts o := DefaultOpts
o.NumPixels = len(pixels) / 3
o.Intensity = 250 o.Intensity = 250
o.Temperature = 5000 o.Temperature = 5000
d, _ := New(spitest.NewRecordRaw(ioutil.Discard), &o) benchmarkDraw(b, o, image.NewRGBA(image.Rect(0, 0, 256, 1)), genColorfulPixel)
_, _ = d.Write(pixels[:])
b.ResetTimer()
for i := 0; i < b.N; i++ {
d.Intensity = uint8(i)
d.Temperature = uint16((3000 + i) & 0x1FFF)
_, _ = d.Write(pixels[:])
}
} }
func BenchmarkHalt(b *testing.B) { func BenchmarkHalt(b *testing.B) {

Loading…
Cancel
Save