From 6c29b5003337ba98b076fe663185107cef65210b Mon Sep 17 00:00:00 2001 From: Drahoslav Date: Wed, 6 Jun 2018 00:41:50 +0200 Subject: [PATCH] Update benchmark --- rpio_test.go | 123 ++++++++++++++++++++++++--------------------------- 1 file changed, 58 insertions(+), 65 deletions(-) diff --git a/rpio_test.go b/rpio_test.go index bd98eb2..d534f5f 100644 --- a/rpio_test.go +++ b/rpio_test.go @@ -120,7 +120,7 @@ func TestEvent(t *testing.T) { } -func BenchmarkToggle(b *testing.B) { +func BenchmarkGpio(b *testing.B) { src := Pin(3) src.Mode(Output) src.Low() @@ -129,72 +129,65 @@ func BenchmarkToggle(b *testing.B) { pin.Mode(Input) pin.PullDown() - b.Run("old", func(b *testing.B) { - for i := 0; i < b.N; i++ { - oldToggle(src) + oldWrite := func(pin Pin, state State) { + p := uint8(pin) + + setReg := p/32 + 7 + clearReg := p/32 + 10 + + memlock.Lock() + defer memlock.Unlock() + + if state == Low { + gpioMem[clearReg] = 1 << (p & 31) + } else { + gpioMem[setReg] = 1 << (p & 31) } - }) - - b.Run("current", func(b *testing.B) { - for i := 0; i < b.N; i++ { - TogglePin(src) - } - }) - -} - -func BenchmarkWrite(b *testing.B) { - src := Pin(3) - src.Mode(Output) - src.Low() - - pin := Pin(2) - pin.Mode(Input) - pin.PullDown() - - b.Run("old", func(b *testing.B) { - for i := 0; i < b.N; i++ { - if i%2 == 0 { - oldWrite(src, High) - } else { - oldWrite(src, Low) - } - } - }) - - b.Run("current", func(b *testing.B) { - for i := 0; i < b.N; i++ { - if i%2 == 0 { - WritePin(src, High) - } else { - WritePin(src, Low) - } - } - }) - -} - -func oldToggle(pin Pin) { - switch ReadPin(pin) { - case Low: - oldWrite(pin, High) - case High: - oldWrite(pin, Low) } -} -func oldWrite(pin Pin, state State) { - p := uint8(pin) - - setReg := p/32 + 7 - clearReg := p/32 + 10 - - memlock.Lock() - defer memlock.Unlock() - - if state == Low { - gpioMem[clearReg] = 1 << (p & 31) - } else { - gpioMem[setReg] = 1 << (p & 31) + oldToggle := func(pin Pin) { + switch ReadPin(pin) { + case Low: + oldWrite(pin, High) + case High: + oldWrite(pin, Low) + } } + + b.Run("write", func(b *testing.B) { + b.Run("old", func(b *testing.B) { + for i := 0; i < b.N; i++ { + if i%2 == 0 { + oldWrite(src, High) + } else { + oldWrite(src, Low) + } + } + }) + + b.Run("new", func(b *testing.B) { + for i := 0; i < b.N; i++ { + if i%2 == 0 { + WritePin(src, High) + } else { + WritePin(src, Low) + } + } + }) + }) + + b.Run("toggle", func(b *testing.B) { + b.Run("old", func(b *testing.B) { + for i := 0; i < b.N; i++ { + oldToggle(src) + } + }) + + b.Run("new", func(b *testing.B) { + for i := 0; i < b.N; i++ { + TogglePin(src) + } + }) + }) + }