-
Notifications
You must be signed in to change notification settings - Fork 3
/
e2e_test.go
136 lines (106 loc) · 2.25 KB
/
e2e_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
package tests_test
import (
"bytes"
goContext "context"
"fmt"
"io"
"sync"
"testing"
"gotest.tools/v3/assert"
)
//TODO: migrate to using vm-orbit test suite
func init() {
if err := initializeAssetPaths(); err != nil {
panic(err)
}
if err := initializeWasm("predict"); err != nil {
panic(err)
}
if err := buildPlugin("cublas"); err != nil {
panic(err)
}
}
func TestPredict(t *testing.T) {
ctx := goContext.Background()
tvm := newTVM(ctx)
plugin := plugin(t, ctx)
defer plugin.Close()
instance, err := newVM(ctx, tvm)
assert.NilError(t, err)
defer instance.Close()
rt, err := instance.Runtime(nil)
assert.NilError(t, err)
defer rt.Close()
_, _, err = rt.Attach(plugin)
assert.NilError(t, err)
fi := getFunction(t, "predict", rt, plugin)
var buf bytes.Buffer
go func() {
reader := io.MultiReader(io.TeeReader(rt.Stdout(), &buf), rt.Stderr())
p := make([]byte, 1024)
for {
n, err := reader.Read(p)
if err != nil {
if n > 0 {
fmt.Print(string(p[:n]))
}
return
}
fmt.Print(string(p[:n]))
}
}()
ret := fi.Call(ctx, 1)
assert.NilError(t, ret.Error())
fmt.Println(
"\n--\n",
buf.String(),
)
}
func TestParallelPredict(t *testing.T) {
threads := 8
ctx := goContext.Background()
tvm := newTVM(ctx)
plugin := plugin(t, ctx)
defer plugin.Close()
var wg sync.WaitGroup
wg.Add(threads)
bufs := make([]bytes.Buffer, threads)
for i := 0; i < threads; i++ {
go func(i int) {
defer wg.Done()
instance, err := newVM(ctx, tvm)
assert.NilError(t, err)
defer instance.Close()
rt, err := instance.Runtime(nil)
assert.NilError(t, err)
defer rt.Close()
_, _, err = rt.Attach(plugin)
assert.NilError(t, err)
fi := getFunction(t, "predict", rt, plugin)
wg.Add(1)
go func() {
defer wg.Done()
reader := io.MultiReader(io.TeeReader(rt.Stdout(), &bufs[i]), rt.Stderr())
p := make([]byte, 1024)
for {
n, err := reader.Read(p)
if err != nil {
if n > 0 {
fmt.Print(string(p[:n]))
}
return
}
fmt.Print(string(p[:n]))
}
}()
ret := fi.Call(ctx, 1+(i%4))
assert.NilError(t, ret.Error())
}(i)
}
wg.Wait()
for i := 0; i < threads; i++ {
fmt.Println()
fmt.Println(bufs[i].String())
fmt.Println()
}
}