Add a benchmark option.
This commit is contained in:
@@ -21,6 +21,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/kylelemons/gousb/usb"
|
"github.com/kylelemons/gousb/usb"
|
||||||
"github.com/kylelemons/gousb/usbid"
|
"github.com/kylelemons/gousb/usbid"
|
||||||
@@ -36,7 +38,8 @@ var (
|
|||||||
setup = flag.Uint("setup", 0, "Endpoint to which to connect")
|
setup = flag.Uint("setup", 0, "Endpoint to which to connect")
|
||||||
endpoint = flag.Uint("endpoint", 1, "Endpoint to which to connect")
|
endpoint = flag.Uint("endpoint", 1, "Endpoint to which to connect")
|
||||||
debug = flag.Int("debug", 3, "Debug level for libusb")
|
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() {
|
func main() {
|
||||||
@@ -117,17 +120,41 @@ func main() {
|
|||||||
log.Printf(" --------------\n")
|
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))
|
ep, err := dev.OpenEndpoint(uint8(*config), uint8(*iface), uint8(*setup), uint8(*endpoint)|uint8(usb.ENDPOINT_DIR_IN))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("open: %s", err)
|
log.Fatalf("open: %s", err)
|
||||||
}
|
}
|
||||||
|
log.Print("Reading...")
|
||||||
|
|
||||||
buf := make([]byte, *size)
|
var total uint64
|
||||||
num, err := ep.Read(buf)
|
if *bench {
|
||||||
if err != nil {
|
go func() {
|
||||||
log.Fatalf("Reading from device failed: %v", err)
|
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])
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user