From 6f89a2daf0c63a529f5e5df73bb6be4489f7999f Mon Sep 17 00:00:00 2001 From: zjregee Date: Tue, 16 Jul 2024 06:25:50 +0000 Subject: [PATCH] feat: add cgo benchmark --- benchmark/microbench/cgo/benchmark.sh | 5 +++ benchmark/microbench/cgo/cgo.go | 12 +++++ .../microbench/cgo/cgo_benchmark_test.go | 44 +++++++++++++++++++ benchmark/microbench/cgo/sum.c | 9 ++++ go.mod | 1 + go.sum | 2 + 6 files changed, 73 insertions(+) create mode 100755 benchmark/microbench/cgo/benchmark.sh create mode 100644 benchmark/microbench/cgo/cgo.go create mode 100644 benchmark/microbench/cgo/cgo_benchmark_test.go create mode 100644 benchmark/microbench/cgo/sum.c diff --git a/benchmark/microbench/cgo/benchmark.sh b/benchmark/microbench/cgo/benchmark.sh new file mode 100755 index 0000000..bdece4d --- /dev/null +++ b/benchmark/microbench/cgo/benchmark.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +gcc -shared -o libsum.so -fPIC sum.c +go test -bench=. +rm libsum.so diff --git a/benchmark/microbench/cgo/cgo.go b/benchmark/microbench/cgo/cgo.go new file mode 100644 index 0000000..915e616 --- /dev/null +++ b/benchmark/microbench/cgo/cgo.go @@ -0,0 +1,12 @@ +package cgo + +/* +#cgo LDFLAGS: -L. -lsum +#include +int64_t sum(int64_t n); +*/ +import "C" + +func cgosum(n int64) int64 { + return int64(C.sum(C.int64_t(n))) +} diff --git a/benchmark/microbench/cgo/cgo_benchmark_test.go b/benchmark/microbench/cgo/cgo_benchmark_test.go new file mode 100644 index 0000000..be5b6af --- /dev/null +++ b/benchmark/microbench/cgo/cgo_benchmark_test.go @@ -0,0 +1,44 @@ +package cgo + +import ( + "testing" + + "github.com/ebitengine/purego" +) + +func sum(n int64) int64 { + var result int64 + result = 0 + for i := int64(0); i < n; i++ { + result += i + } + return result +} + +func BenchmarkGoSum(b *testing.B) { + for i := 0; i < b.N; i++ { + _ = sum(100) + } +} + +func BenchmarkCgoSum(b *testing.B) { + for i := 0; i < b.N; i++ { + _ = cgosum(100) + } +} + +func BenchmarkPuregoSum(b *testing.B) { + libc, err := purego.Dlopen("./libsum.so", purego.RTLD_NOW|purego.RTLD_GLOBAL) + if err != nil { + panic("shouldn't failed here") + } + defer purego.Dlclose(libc) + var puregosum func(int64) int64 + purego.RegisterLibFunc(&puregosum, libc, "sum") + + b.ResetTimer() + + for i := 0; i < b.N; i++ { + _ = puregosum(100) + } +} diff --git a/benchmark/microbench/cgo/sum.c b/benchmark/microbench/cgo/sum.c new file mode 100644 index 0000000..cd8ba94 --- /dev/null +++ b/benchmark/microbench/cgo/sum.c @@ -0,0 +1,9 @@ +#include + +int64_t sum(int64_t n) { + int64_t result = 0; + for (int64_t i = 0; i < n; i++) { + result += i; + } + return result; +} diff --git a/go.mod b/go.mod index e7f15e9..8005b71 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.22.0 require ( github.com/cloudwego/netpoll v0.6.2 + github.com/ebitengine/purego v0.7.1 github.com/google/uuid v1.6.0 github.com/sirupsen/logrus v1.9.3 github.com/stretchr/testify v1.7.0 diff --git a/go.sum b/go.sum index 3087e97..cbc28ef 100644 --- a/go.sum +++ b/go.sum @@ -5,6 +5,8 @@ github.com/cloudwego/netpoll v0.6.2/go.mod h1:kaqvfZ70qd4T2WtIIpCOi5Cxyob8viEpzL github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/ebitengine/purego v0.7.1 h1:6/55d26lG3o9VCZX8lping+bZcmShseiqlh2bnUDiPA= +github.com/ebitengine/purego v0.7.1/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=