Skip to content

Instantly share code, notes, and snippets.

@schlamar
Created Apr 4, 2019
Embed
What would you like to do?
GPIO read performance test
package main
import (
"fmt"
"runtime/debug"
"sort"
"time"
"periph.io/x/periph/conn/gpio/gpioreg"
"periph.io/x/periph/host"
)
func main() {
_, err := host.Init()
if err != nil {
fmt.Println("HostInit error:", err)
return
}
pin := gpioreg.ByName("GPIO4")
if pin == nil {
fmt.Println("Pin is nil")
return
}
start := time.Now()
durations := make([]int, 10000)
gcPercent := debug.SetGCPercent(-1)
numberReads := 0
for {
readStart := time.Now()
pin.Read()
durations = append(durations, int(time.Since(readStart)/time.Nanosecond))
numberReads++
if time.Since(start) > 10*time.Millisecond {
break
}
}
debug.SetGCPercent(gcPercent)
sort.Ints(durations)
fmt.Println("n reads:", numberReads)
fmt.Println("avg read duration", 10000.0/float64(numberReads))
fmt.Println("duration", time.Since(start))
fmt.Println("max read durations (ns):", durations[len(durations)-6:])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment