A benchmark on the trivial cgo call, to assess overhead of different
ways of calling out to cgo.
This commit is contained in:
@@ -381,3 +381,8 @@ func xfer_callback(cptr unsafe.Pointer) {
|
|||||||
ch := *(*chan struct{})(cptr)
|
ch := *(*chan struct{})(cptr)
|
||||||
close(ch)
|
close(ch)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// for benchmarking
|
||||||
|
func libusbSetDebug(c *libusbContext, lvl int) {
|
||||||
|
C.libusb_set_debug((*C.libusb_context)(c), C.int(lvl))
|
||||||
|
}
|
||||||
|
46
usb/libusb_cgo_benchmark_test.go
Normal file
46
usb/libusb_cgo_benchmark_test.go
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
package usb
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func BenchmarkCGo(b *testing.B) {
|
||||||
|
for _, bc := range []struct {
|
||||||
|
name string
|
||||||
|
bfunc func(*libusbContext, int)
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "simple function",
|
||||||
|
bfunc: func(ctx *libusbContext, N int) {
|
||||||
|
for i := 0; i < N; i++ {
|
||||||
|
libusbSetDebug(ctx, i&1)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "method",
|
||||||
|
bfunc: func(ctx *libusbContext, N int) {
|
||||||
|
impl := libusbImpl{}
|
||||||
|
for i := 0; i < N; i++ {
|
||||||
|
impl.setDebug(ctx, i&1)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "interface",
|
||||||
|
bfunc: func(ctx *libusbContext, N int) {
|
||||||
|
var intf libusbIntf = libusbImpl{}
|
||||||
|
for i := 0; i < N; i++ {
|
||||||
|
intf.setDebug(ctx, i&1)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
b.Run(bc.name, func(b *testing.B) {
|
||||||
|
ctx, err := libusbImpl{}.init()
|
||||||
|
if err != nil {
|
||||||
|
b.Fatalf("libusb_init() failed: %v", err)
|
||||||
|
}
|
||||||
|
b.ResetTimer()
|
||||||
|
bc.bfunc(ctx, b.N)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user