-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplugin_test.go
247 lines (209 loc) · 5.2 KB
/
plugin_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package benchmark
import (
"fmt"
"github.com/dullgiulio/pingo"
"github.com/hashicorp/go-hclog"
hashicorpplugin "github.com/hashicorp/go-plugin"
"github.com/natefinch/pie"
"github.com/pkujhd/goloader"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
"github.com/traefik/yaegi/interp"
"github.com/traefik/yaegi/stdlib"
plugplugin "github.com/uberswe/go-plugin-benchmark/plug"
"context"
"net/rpc/jsonrpc"
"os"
"os/exec"
"plugin"
"runtime"
"testing"
"unsafe"
)
// BenchmarkPluginRandInt uses a go plugin and tests math/rand for generating random integers
func BenchmarkPluginRandInt(b *testing.B) {
plug, err := plugin.Open("./plugin.so")
if err != nil {
panic(err)
}
randInt, err := plug.Lookup("RandInt")
if err != nil {
panic(err)
}
randFunc, ok := randInt.(func() int)
if !ok {
panic("unexpected type from module symbol")
}
b.Run("golang-plugin", func(b *testing.B) {
for i := 0; i < b.N; i++ {
randFunc()
}
})
}
func BenchmarkHashicorpGoPluginRandInt(b *testing.B) {
logger := hclog.New(&hclog.LoggerOptions{
Name: "plugin",
Output: os.Stdout,
Level: hclog.Off,
})
var handshakeConfig = hashicorpplugin.HandshakeConfig{
ProtocolVersion: 1,
MagicCookieKey: "RAND_PLUGIN",
MagicCookieValue: "int",
}
// pluginMap is the map of plugins we can dispense.
var pluginMap = map[string]hashicorpplugin.Plugin{
"responder": &RandIntPlugin{},
}
// We're a host! Start by launching the plugin process.
client := hashicorpplugin.NewClient(&hashicorpplugin.ClientConfig{
HandshakeConfig: handshakeConfig,
Plugins: pluginMap,
Cmd: exec.Command("./hashicorpgoplugin"),
Logger: logger,
})
defer client.Kill()
// Connect via RPC
rpcClient, err := client.Client()
if err != nil {
panic(err)
}
// Request the plugin
raw, err := rpcClient.Dispense("responder")
if err != nil {
panic(err)
}
// We should have a Greeter now! This feels like a normal interface
// implementation but is in fact over an RPC connection.
intResponder := raw.(RandIntResponder)
b.Run("hashicorp-go-plugin", func(b *testing.B) {
for i := 0; i < b.N; i++ {
intResponder.Respond()
}
})
}
func BenchmarkPieRandInt(b *testing.B) {
path := "./pieplugin"
if runtime.GOOS == "windows" {
path = path + ".exe"
}
client, err := pie.StartProviderCodec(jsonrpc.NewClientCodec, os.Stderr, path)
if err != nil {
panic(err)
}
defer client.Close()
p := plug{client}
b.Run("pie", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = p.RandInt(i)
}
})
}
func BenchmarkPingoTcpRandInt(b *testing.B) {
p := pingo.NewPlugin("tcp", "./pingoplugin")
p.Start()
var resp int
b.Run("pingo-tcp", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = p.Call("MyPlugin.RandInt", 1, &resp)
}
})
p.Stop()
p2 := pingo.NewPlugin("unix", "./pingoplugin")
p2.SetSocketDirectory("./")
p2.Start()
b.Run("pingo-unix", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = p2.Call("MyPlugin.RandInt", 1, &resp)
}
})
p2.Stop()
}
func BenchmarkPlugRandInt(b *testing.B) {
service, _ := plugplugin.LoadRandomIntService("./plugplugin")
b.Run("plug", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = service.Get(1)
}
})
}
func BenchmarkYaegiRandInt(b *testing.B) {
var src = `package test
import "math/rand"
func RandInt(i int) int { return rand.Int() }`
i := interp.New(interp.Options{})
// To handle import of "math/rand"
i.Use(stdlib.Symbols)
_, err := i.Eval(src)
if err != nil {
panic(err)
}
v, err := i.Eval("test.RandInt")
if err != nil {
panic(err)
}
randIntFunc := v.Interface().(func(int) int)
b.Run("yaegi", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = randIntFunc(1)
}
})
}
func BenchmarkGoloaderRandInt(b *testing.B) {
linker, err := goloader.ReadObjs([]string{"goloader.o"}, []string{"main"})
if err != nil {
b.Error(err)
return
}
run := "main.RandInt"
symPtr := make(map[string]uintptr)
err = goloader.RegSymbol(symPtr)
if err != nil {
b.Error(err)
return
}
b.Run("goloader", func(b *testing.B) {
codeModule, err := goloader.Load(linker, symPtr)
if err != nil {
fmt.Println("Load error:", err)
return
}
runFuncPtr, ok := codeModule.Syms[run]
if !ok || runFuncPtr == 0 {
fmt.Println("Load error! not find function:", run)
return
}
funcPtrContainer := (uintptr)(unsafe.Pointer(&runFuncPtr))
runFunc := *(*func() int)(unsafe.Pointer(&funcPtrContainer))
for i := 0; i < b.N; i++ {
_ = runFunc()
}
os.Stdout.Sync()
codeModule.Unload()
})
}
func BenchmarkWazeroRandInt(b *testing.B) {
wasmFile, err := os.ReadFile("wazero.wasm")
if err != nil {
fmt.Println("failed to read file:", err)
return
}
ctx := context.Background()
runtime := wazero.NewRuntime(ctx)
defer runtime.Close(ctx)
wasi_snapshot_preview1.MustInstantiate(ctx, runtime)
module, err := runtime.Instantiate(ctx, wasmFile)
if err != nil {
fmt.Println("failed to instantiate module:", err)
return
}
b.Run("wazero", func(b *testing.B) {
randIntFunction := module.ExportedFunction("RandInt")
for i := 0; i < b.N; i++ {
if _, err := randIntFunction.Call(ctx); err != nil {
fmt.Println("failed to call function:", err)
return
}
}
})
}