Add a benchmark option.

This commit is contained in:
Sebastian Zagrodzki
2017-02-08 23:28:52 +01:00
parent c27a77b547
commit 91119ca790

View File

@@ -21,6 +21,8 @@ import (
"fmt"
"log"
"os"
"sync/atomic"
"time"
"github.com/kylelemons/gousb/usb"
"github.com/kylelemons/gousb/usbid"
@@ -36,7 +38,8 @@ var (
setup = flag.Uint("setup", 0, "Endpoint to which to connect")
endpoint = flag.Uint("endpoint", 1, "Endpoint to which to connect")
debug = flag.Int("debug", 3, "Debug level for libusb")
size = flag.Uint("read_size", 1024, "Maximum number of bytes of data to read. Collected will be printed to STDOUT.")
size = flag.Uint("read_size", 1024, "Number of bytes of data to read in a single transaction.")
bench = flag.Bool("benchmark", false, "When true, keep reading from the endpoint and periodically report the measured throughput. If false, only one read operation is performed and obtained data is printed to STDOUT.")
)
func main() {
@@ -117,17 +120,41 @@ func main() {
log.Printf(" --------------\n")
}
log.Printf("Connecting to endpoint...")
log.Print("Connecting to endpoint...")
ep, err := dev.OpenEndpoint(uint8(*config), uint8(*iface), uint8(*setup), uint8(*endpoint)|uint8(usb.ENDPOINT_DIR_IN))
if err != nil {
log.Fatalf("open: %s", err)
}
log.Print("Reading...")
buf := make([]byte, *size)
num, err := ep.Read(buf)
if err != nil {
log.Fatalf("Reading from device failed: %v", err)
var total uint64
if *bench {
go func() {
var last uint64
var before = time.Now()
for {
time.Sleep(4 * time.Second)
cur := atomic.LoadUint64(&total)
now := time.Now()
dur := now.Sub(before)
log.Printf("%.2f B/s\n", float64(cur-last)/(float64(dur)/float64(time.Second)))
before = now
last = cur
}
}()
}
for {
buf := make([]byte, *size)
num, err := ep.Read(buf)
if err != nil {
log.Fatalf("Reading from device failed: %v", err)
}
if !*bench {
log.Printf("Read %d bytes of data", num)
os.Stdout.Write(buf[:num])
return
}
atomic.AddUint64(&total, uint64(num))
}
log.Printf("Read %d bytes of data", num)
os.Stdout.Write(buf[:num])
}