From 91119ca790e4691ef07c180ada441ae969495596 Mon Sep 17 00:00:00 2001 From: Sebastian Zagrodzki Date: Wed, 8 Feb 2017 23:28:52 +0100 Subject: [PATCH] Add a benchmark option. --- rawread/main.go | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/rawread/main.go b/rawread/main.go index 55ab813..4e56e1e 100644 --- a/rawread/main.go +++ b/rawread/main.go @@ -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]) }