Last active
August 19, 2018 18:20
-
-
Save kostix/fc2f850c1a50fb31eacd66a04498b256 to your computer and use it in GitHub Desktop.
A real test for cgo code https://github.com/dyu/ffi-overhead
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
/* | |
#cgo CFLAGS: -Wall -O2 -fPIC | |
#cgo linux CFLAGS: -DLINUX=1 | |
#cgo LDFLAGS: -Lnewplus -lnewplus -Wl,-rpath='$ORIGIN'/newplus | |
#include <stdlib.h> | |
#include "newplus/plus.h" | |
*/ | |
import "C" | |
import "testing" | |
func plusOne(b *testing.B) { | |
// load | |
C.plusone(C.int(C.current_timestamp())) | |
//start := C.current_timestamp() | |
var x C.int = 0 | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
x = C.plusone(x) | |
} | |
//fmt.Println(C.current_timestamp() - start) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "testing" | |
func BenchmarkPlusOne(b *testing.B) { | |
plusOne(b) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ffi-overhead$ rm hello.c hello.cpp # ...to make cgo not try to compile them. | |
ffi-overhead$ cd newplus | |
newplus$ gcc -c -o libnewplus.a -Wall -O2 -fPIC plus.c | |
newplus$ cd .. | |
ffi-overhead$ go test -c | |
ffi-overhead$ ./ffi-overhead.test -test.bench . -test.run=xxx -test.cpuprofile=hello.pprof | |
goos: linux | |
goarch: amd64 | |
pkg: github.com/dyu/ffi-overhead | |
BenchmarkPlusOne-4 20000000 81.1 ns/op | |
PASS | |
ffi-overhead$ go tool pprof hello.pprof | |
File: ffi-overhead.test | |
Build ID: 80a7ea67207dac6817521db32cea644d410500d4 | |
Type: cpu | |
Time: Aug 19, 2018 at 8:50pm (MSK) | |
Duration: 1.90s, Total samples = 1.71s (89.81%) | |
Entering interactive mode (type "help" for commands, "o" for options) | |
(pprof) top20 | |
Showing nodes accounting for 1.71s, 100% of 1.71s total | |
flat flat% sum% cum cum% | |
0.92s 53.80% 53.80% 1.64s 95.91% runtime.cgocall | |
0.25s 14.62% 68.42% 0.25s 14.62% runtime.casgstatus | |
0.19s 11.11% 79.53% 0.22s 12.87% runtime.exitsyscallfast | |
0.12s 7.02% 86.55% 0.13s 7.60% runtime.reentersyscall | |
0.11s 6.43% 92.98% 0.58s 33.92% runtime.exitsyscall | |
0.04s 2.34% 95.32% 1.70s 99.42% github.com/dyu/ffi-overhead.plusOne | |
0.03s 1.75% 97.08% 0.03s 1.75% runtime.exitsyscallfast_reacquired | |
0.02s 1.17% 98.25% 1.66s 97.08% github.com/dyu/ffi-overhead._Cfunc_plusone | |
0.01s 0.58% 98.83% 0.14s 8.19% runtime.entersyscall | |
0.01s 0.58% 99.42% 0.01s 0.58% runtime.save | |
0.01s 0.58% 100% 0.01s 0.58% runtime.usleep | |
0 0% 100% 1.70s 99.42% github.com/dyu/ffi-overhead.BenchmarkPlusOne | |
0 0% 100% 0.01s 0.58% runtime.mstart | |
0 0% 100% 0.01s 0.58% runtime.mstart1 | |
0 0% 100% 0.01s 0.58% runtime.sysmon | |
0 0% 100% 1.70s 99.42% testing.(*B).launch | |
0 0% 100% 1.70s 99.42% testing.(*B).runN | |
(pprof) | |
# Let's see how the test function looks like: | |
$ go tool objdump ffi-overhead.test | |
... | |
TEXT github.com/dyu/ffi-overhead.BenchmarkPlusOne(SB) .../go/src/github.com/dyu/ffi-overhead/hello_test | |
hello_test.go:5 0x4e6c30 64488b0c25f8ffffff MOVQ FS:0xfffffff8, CX | |
hello_test.go:5 0x4e6c39 483b6110 CMPQ 0x10(CX), SP | |
hello_test.go:5 0x4e6c3d 7626 JBE 0x4e6c65 | |
hello_test.go:5 0x4e6c3f 4883ec10 SUBQ $0x10, SP | |
hello_test.go:5 0x4e6c43 48896c2408 MOVQ BP, 0x8(SP) | |
hello_test.go:5 0x4e6c48 488d6c2408 LEAQ 0x8(SP), BP | |
hello_test.go:5 0x4e6c4d 488b442418 MOVQ 0x18(SP), AX | |
hello_test.go:6 0x4e6c52 48890424 MOVQ AX, 0(SP) | |
hello_test.go:6 0x4e6c56 e825010000 CALL github.com/dyu/ffi-overhead.plusOne(SB) | |
hello_test.go:7 0x4e6c5b 488b6c2408 MOVQ 0x8(SP), BP | |
hello_test.go:7 0x4e6c60 4883c410 ADDQ $0x10, SP | |
hello_test.go:7 0x4e6c64 c3 RET | |
hello_test.go:5 0x4e6c65 e8a6c3f6ff CALL runtime.morestack_noctxt(SB) | |
hello_test.go:5 0x4e6c6a ebc4 JMP github.com/dyu/ffi-overhead.BenchmarkPlusO | |
... | |
# ...as we can see, the compiler did not inline our plusOne() wrapper. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment