Skip to content

Instantly share code, notes, and snippets.

@schlamar
Created April 4, 2019 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schlamar/b1f6e83725ec3b83346bf9861e7693a2 to your computer and use it in GitHub Desktop.
Save schlamar/b1f6e83725ec3b83346bf9861e7693a2 to your computer and use it in GitHub Desktop.
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