Skip to content

Commit e1a3db7

Browse files
committed
Add benchmarks in go port and corrected minor bug.
1 parent 929faa1 commit e1a3db7

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

source/ports/go_port/source/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,33 @@ func main() {
5050
}
5151
}
5252
```
53+
54+
## Building
55+
56+
[Build and install MetaCall from source](https://github.com/metacall/core/blob/develop/docs/README.md#6-build-system) or [install precompiled binaries](https://github.com/metacall/install#install). Then run:
57+
58+
```sh
59+
go build
60+
```
61+
62+
In case of using precompiled binaries (in Linux), when running any application using MetaCall, you must set the environment variable `LD_LIBRARY_PATH` pointing to the MetaCall library (a part of setting any other required environment variable related to MetaCall if needed). For example:
63+
64+
```sh
65+
export LD_LIBRARY_PATH="/gnu/store/`ls /gnu/store/ | grep metacall | head -n 1`/lib"
66+
```
67+
68+
## Testing
69+
70+
For running tests:
71+
72+
```sh
73+
go test
74+
```
75+
76+
## Benchmarks
77+
78+
For running tests:
79+
80+
```sh
81+
go test -bench=.
82+
```

source/ports/go_port/source/go_port.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ package metacall
3333
// their address to pass to clibrary; thus, they are moved to a separate Go
3434
// file.
3535
36+
// Based on: https://eli.thegreenplace.net/2019/passing-callbacks-and-pointers-to-cgo/
37+
3638
extern void *resolveCgo(void *, void *);
3739
extern void *rejectCgo(void *, void *);
3840
@@ -354,7 +356,7 @@ func getFunction(function string) (unsafe.Pointer, error) {
354356
defer C.free(unsafe.Pointer(cFunction))
355357
cFunc := C.metacall_function(cFunction)
356358
if cFunc == nil {
357-
return nil, errors.New("function %s not found: " + function)
359+
return nil, errors.New("function not found: " + function)
358360
}
359361
return cFunc, nil
360362
}

source/ports/go_port/source/go_port_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ func TestMain(m *testing.M) {
1111
if err := Initialize(); err != nil {
1212
log.Fatal(err)
1313
}
14+
15+
// if benchmark {
16+
buffer := "module.exports = { benchmark: async x => x }"
17+
18+
if err := LoadFromMemory("node", buffer); err != nil {
19+
log.Fatal(err)
20+
return
21+
}
22+
// }
23+
1424
code := m.Run()
1525
Destroy()
1626
os.Exit(code)
@@ -72,3 +82,42 @@ func TestNodeJS(t *testing.T) {
7282

7383
wg.Wait()
7484
}
85+
86+
func benchmarkNodeJS(b *testing.B, n int) {
87+
var wg sync.WaitGroup
88+
89+
wg.Add(n)
90+
91+
for i := 0; i < n; i++ {
92+
_, err := Await("benchmark",
93+
func(interface{}, interface{}) interface{} {
94+
wg.Done()
95+
return nil
96+
},
97+
func(interface{}, interface{}) interface{} {
98+
wg.Done()
99+
return nil
100+
},
101+
nil,
102+
)
103+
104+
if err != nil {
105+
b.Fatal(err)
106+
return
107+
}
108+
}
109+
110+
wg.Wait()
111+
}
112+
113+
func BenchmarkNodeJSSequential(b *testing.B) {
114+
benchmarkNodeJS(b, 5)
115+
}
116+
117+
func BenchmarkNodeJSParallel(b *testing.B) {
118+
b.RunParallel(func(pb *testing.PB) {
119+
for pb.Next() {
120+
benchmarkNodeJS(b, 5)
121+
}
122+
})
123+
}

0 commit comments

Comments
 (0)