From 2793c71b31ee27d5e572f64087280e16d7021ddf Mon Sep 17 00:00:00 2001 From: Xuanwo Date: Thu, 28 Sep 2023 18:04:55 +0800 Subject: [PATCH] docs: Add basic readme for go binding Signed-off-by: Xuanwo --- bindings/c/.gitignore | 4 ++- bindings/c/examples/.gitignore | 1 + bindings/go/.gitignore | 1 + bindings/go/README.md | 55 ++++++++++++++++++++++++++++++++++ bindings/go/opendal.go | 1 - bindings/go/opendal_test.go | 23 ++++++++++++-- 6 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 bindings/c/examples/.gitignore create mode 100644 bindings/go/.gitignore create mode 100644 bindings/go/README.md diff --git a/bindings/c/.gitignore b/bindings/c/.gitignore index c4ec047b909f..8f5beed0ec41 100644 --- a/bindings/c/.gitignore +++ b/bindings/c/.gitignore @@ -1,2 +1,4 @@ build/ -docs/ \ No newline at end of file +docs/ + +*.o diff --git a/bindings/c/examples/.gitignore b/bindings/c/examples/.gitignore new file mode 100644 index 000000000000..887b0251a8fe --- /dev/null +++ b/bindings/c/examples/.gitignore @@ -0,0 +1 @@ +basicrw diff --git a/bindings/go/.gitignore b/bindings/go/.gitignore new file mode 100644 index 000000000000..c79b323537a1 --- /dev/null +++ b/bindings/go/.gitignore @@ -0,0 +1 @@ +opendal_c.pc diff --git a/bindings/go/README.md b/bindings/go/README.md new file mode 100644 index 000000000000..352a0749c0e0 --- /dev/null +++ b/bindings/go/README.md @@ -0,0 +1,55 @@ +# OpenDAL Go Binding (WIP) + +opendal-go requires opendal-c to be installed. + +```shell +cd bindings/c +make build +``` + +You will find `libopendal_c.so` under `{root}/target`. + +Then, we need to add a `opendal_c.pc` files + +```pc +libdir=/path/to/opendal/target/debug/ +includedir=/path/to/opendal/bindings/c/include/ + +Name: opendal_c +Description: opendal c binding +Version: + +Libs: -L${libdir} -lopendal_c +Cflags: -I${includedir} +``` + +And set the `PKG_CONFIG_PATH` environment variable to the directory where `opendal_c.pc` is located. + +```shell +export PKG_CONFIG_PATH=/dir/of/opendal_c.pc +``` + +Then, we can build the go binding. + +```shell +cd bindings/go +go build -tags dynamic . +``` + +To running the go binding tests, we need to tell the linker where to find the `libopendal_c.so` file. + +```shell +expose LD_LIBRARY_PATH=/path/to/opendal/target/debug/ +``` + +Then, we can run the tests. + +```shell +go test -tags dynamic . +``` + +For benchmark + +```shell +go test -bench=. -tags dynamic . +``` diff --git a/bindings/go/opendal.go b/bindings/go/opendal.go index 45f6b2f05717..1494ab170837 100644 --- a/bindings/go/opendal.go +++ b/bindings/go/opendal.go @@ -41,7 +41,6 @@ type Operator struct { } func NewOperator(scheme string, opt Options) (*Operator, error) { - fmt.Println("libopendal_c info: ", LibopendalLinkInfo) if len(scheme) == 0 { return nil, errInvalidScheme } diff --git a/bindings/go/opendal_test.go b/bindings/go/opendal_test.go index 30b50bd94f74..6723b67e2ce4 100644 --- a/bindings/go/opendal_test.go +++ b/bindings/go/opendal_test.go @@ -20,6 +20,7 @@ package opendal import ( + "bytes" "testing" "github.com/stretchr/testify/assert" @@ -31,9 +32,27 @@ func TestOperations(t *testing.T) { operator, err := NewOperator("memory", opts) assert.NoError(t, err) defer operator.Close() - err = operator.Write("test", []byte("Hello World")) + err = operator.Write("test", []byte("Hello World from OpenDAL CGO!")) assert.NoError(t, err) value, err := operator.Read("test") assert.NoError(t, err) - assert.Equal(t, "Hello World", string(value)) + assert.Equal(t, "Hello World from OpenDAL CGO!", string(value)) + + println(string(value)) +} + +func BenchmarkOperator_Write(b *testing.B) { + opts := make(Options) + opts["root"] = "/myroot" + op, _ := NewOperator("memory", opts) + defer op.Close() + + bs := bytes.Repeat([]byte("a"), 1024*1024) + op.Write("test", bs) + + b.SetBytes(1024*1024) + b.ResetTimer() + for i := 0; i < b.N; i++ { + op.Read("test") + } }