2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
31
"github.com/tetratelabs/wabin/binary"
32
"github.com/tetratelabs/wabin/wasm"
34
config "mosn.io/mosn/pkg/config/v2"
35
_ "mosn.io/mosn/pkg/filter/network/proxy"
36
_ "mosn.io/mosn/pkg/filter/stream/proxywasm"
37
_ "mosn.io/mosn/pkg/stream/http"
38
_ "mosn.io/mosn/pkg/stream/http2"
39
_ "mosn.io/mosn/pkg/wasm/abi/proxywasm020"
40
"mosn.io/mosn/test/util/mosn"
45
log.DefaultLogger.SetLogLevel(log.ERROR)
54
const pathResponseHeaderV1 = "testdata/req-header-v1/main.wasm"
56
func test_ProxyWasmV1(t *testing.T, engine string) {
57
// Ensure the module was compiled with the correct ABI as this is hard to verify at runtime.
58
requireModuleExport(t, pathResponseHeaderV1, "proxy_abi_version_0_1_0")
60
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
61
if r.Header.Get("Wasm-Context") == "" {
62
t.Fatalf("expected to see request header from wasm: %v", r.Header)
66
logPath := filepath.Join(t.TempDir(), "mosn.log")
68
mosn, err := startMosn(backend.Listener.Addr().String(), engine, pathResponseHeaderV1, logPath)
74
resp, err := http.Get(mosn.url)
78
defer resp.Body.Close()
81
func Benchmark_BaseCase(b *testing.B) {
85
func benchmark_ProxyWasmV1(b *testing.B, engine string) {
86
benchmark(b, engine, pathResponseHeaderV1)
89
func benchmark(b *testing.B, engine, wasmPath string) {
90
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
92
logPath := filepath.Join(b.TempDir(), "mosn.log")
94
mosn, err := startMosn(backend.Listener.Addr().String(), engine, wasmPath, logPath)
101
for i := 0; i < b.N; i++ {
102
resp, err := http.Get(mosn.url)
110
func startMosn(backendAddr string, engine, wasmPath, logPath string) (testMosn, error) {
112
adminPort := freePort()
113
c := &config.MOSNConfig{
114
Servers: []config.ServerConfig{
116
DefaultLogPath: logPath,
117
DefaultLogLevel: "ERROR",
118
Routers: []*config.RouterConfiguration{
120
RouterConfigurationConfig: config.RouterConfigurationConfig{
121
RouterConfigName: "server_router",
123
VirtualHosts: []config.VirtualHost{
126
Domains: []string{"*"},
127
Routers: []config.Router{
129
RouterConfig: config.RouterConfig{
130
Match: config.RouterMatch{
133
Route: config.RouteAction{
134
RouterActionConfig: config.RouterActionConfig{
135
ClusterName: "serverCluster",
145
Listeners: []config.Listener{
147
ListenerConfig: config.ListenerConfig{
148
Name: "serverListener",
149
AddrConfig: fmt.Sprintf("127.0.0.1:%d", port),
151
FilterChains: []config.FilterChain{
153
FilterChainConfig: config.FilterChainConfig{
154
Filters: []config.Filter{
157
Config: map[string]interface{}{
158
"downstream_protocol": "Http1",
159
"upstream_protocol": "Http1",
160
"router_config_name": "server_router",
172
ClusterManager: config.ClusterManagerConfig{
173
Clusters: []config.Cluster{
175
Name: "serverCluster",
176
ClusterType: "SIMPLE",
178
MaxRequestPerConn: 1024,
179
ConnBufferLimitBytes: 32768,
180
Hosts: []config.Host{
182
HostConfig: config.HostConfig{
183
Address: backendAddr,
190
RawAdmin: &config.Admin{
191
Address: &config.AddressInfo{
192
SocketAddress: config.SocketAddress{
193
Address: "127.0.0.1",
194
PortValue: uint32(adminPort),
198
DisableUpgrade: true,
201
c.Servers[0].Listeners[0].ListenerConfig.StreamFilters = []config.Filter{
204
Config: map[string]interface{}{
206
"vm_config": map[string]interface{}{
214
app := mosn.NewMosn(c)
216
for i := 0; i < 100; i++ {
217
time.Sleep(200 * time.Millisecond)
218
resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:%d", adminPort))
222
defer resp.Body.Close()
223
if resp.StatusCode == http.StatusOK {
224
time.Sleep(1 * time.Second)
226
url: fmt.Sprintf("http://127.0.0.1:%d", port),
232
return testMosn{}, errors.New("mosn start failed")
236
l, _ := net.Listen("tcp", ":0")
238
return l.Addr().(*net.TCPAddr).Port
241
func requireModuleExport(t *testing.T, wasmPath, want string) {
242
bin, err := os.ReadFile(wasmPath)
246
mod, err := binary.DecodeModule(bin, wasm.CoreFeaturesV2)
248
for _, e := range mod.ExportSection {
252
exports = append(exports, e.Name)
254
t.Errorf("export not found, want: %v, have: %v", want, exports)