Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds integrated benchmarks for proxywasm #2164

Merged
merged 1 commit into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
213 changes: 213 additions & 0 deletions test/integrate/proxywasm/proxywasm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
//go:build wasmer
// +build wasmer

package wasm_test

import (
"errors"
"fmt"
"net"
"net/http"
"net/http/httptest"
"path/filepath"
"testing"
"time"

config "mosn.io/mosn/pkg/config/v2"
_ "mosn.io/mosn/pkg/filter/network/proxy"
_ "mosn.io/mosn/pkg/filter/stream/proxywasm"
_ "mosn.io/mosn/pkg/stream/http"
_ "mosn.io/mosn/pkg/stream/http2"
_ "mosn.io/mosn/pkg/wasm/runtime/wasmer"
"mosn.io/mosn/test/util/mosn"
)

type testMosn struct {
url string
logPath string
*mosn.MosnWrapper
}

const pathResponseHeaderV1 = "testdata/req-header-v1/main.wasm"

func Test_ProxyWasmV1(t *testing.T) {
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Wasm-Context") == "" {
t.Fatalf("expected to see request header from wasm: %v", r.Header)
}
}))
defer backend.Close()
logPath := filepath.Join(t.TempDir(), "mosn.log")

mosn, err := startMosn(backend.Listener.Addr().String(), pathResponseHeaderV1, logPath)
if err != nil {
t.Fatal(err)
}
defer mosn.Close()

resp, err := http.Get(mosn.url)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
}

func Benchmark_BaseCase(b *testing.B) {
benchmark(b, "")
}

func Benchmark_ProxyWasmV1(b *testing.B) {
benchmark(b, pathResponseHeaderV1)
}

func benchmark(b *testing.B, wasmPath string) {
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
defer backend.Close()
logPath := filepath.Join(b.TempDir(), "mosn.log")

mosn, err := startMosn(backend.Listener.Addr().String(), wasmPath, logPath)
if err != nil {
b.Fatal(err)
}
defer mosn.Close()

b.ResetTimer()
for i := 0; i < b.N; i++ {
resp, err := http.Get(mosn.url)
if err != nil {
b.Fatal(err)
}
resp.Body.Close()
}
}

func startMosn(backendAddr string, wasmPath, logPath string) (testMosn, error) {
port := freePort()
adminPort := freePort()
c := &config.MOSNConfig{
Servers: []config.ServerConfig{
{
DefaultLogPath: logPath,
DefaultLogLevel: "ERROR",
Routers: []*config.RouterConfiguration{
{
RouterConfigurationConfig: config.RouterConfigurationConfig{
RouterConfigName: "server_router",
},
VirtualHosts: []config.VirtualHost{
{
Name: "serverHost",
Domains: []string{"*"},
Routers: []config.Router{
{
RouterConfig: config.RouterConfig{
Match: config.RouterMatch{
Prefix: "/",
},
Route: config.RouteAction{
RouterActionConfig: config.RouterActionConfig{
ClusterName: "serverCluster",
},
},
},
},
},
},
},
},
},
Listeners: []config.Listener{
{
ListenerConfig: config.ListenerConfig{
Name: "serverListener",
AddrConfig: fmt.Sprintf("127.0.0.1:%d", port),
BindToPort: true,
FilterChains: []config.FilterChain{
{
FilterChainConfig: config.FilterChainConfig{
Filters: []config.Filter{
{
Type: "proxy",
Config: map[string]interface{}{
"downstream_protocol": "Http1",
"upstream_protocol": "Http1",
"router_config_name": "server_router",
},
},
},
},
},
},
},
},
},
},
},
ClusterManager: config.ClusterManagerConfig{
Clusters: []config.Cluster{
{
Name: "serverCluster",
ClusterType: "SIMPLE",
LbType: "LB_RANDOM",
MaxRequestPerConn: 1024,
ConnBufferLimitBytes: 32768,
Hosts: []config.Host{
{
HostConfig: config.HostConfig{
Address: backendAddr,
},
},
},
},
},
},
RawAdmin: &config.Admin{
Address: &config.AddressInfo{
SocketAddress: config.SocketAddress{
Address: "127.0.0.1",
PortValue: uint32(adminPort),
},
},
},
DisableUpgrade: true,
}
if wasmPath != "" {
c.Servers[0].Listeners[0].ListenerConfig.StreamFilters = []config.Filter{
{
Type: "proxywasm",
Config: map[string]interface{}{
"instance_num": 1,
"vm_config": map[string]interface{}{
"engine": "wasmer",
"path": wasmPath,
},
},
},
}
}
app := mosn.NewMosn(c)
app.Start()
for i := 0; i < 100; i++ {
time.Sleep(200 * time.Millisecond)
resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:%d", adminPort))
if err != nil {
continue
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
time.Sleep(1 * time.Second)
return testMosn{
url: fmt.Sprintf("http://127.0.0.1:%d", port),
logPath: logPath,
MosnWrapper: app,
}, nil
}
}
return testMosn{}, errors.New("mosn start failed")
}

func freePort() int {
l, _ := net.Listen("tcp", ":0")
defer l.Close()
return l.Addr().(*net.TCPAddr).Port
}
7 changes: 7 additions & 0 deletions test/integrate/proxywasm/testdata/req-header-v1/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module mosn.io/mosn/example

// maximum supported go version of TinyGo 0.19.0
go 1.16

// Oldest version of proxy-wasm-go-sdk which allows overriding with abi_010
require github.com/tetratelabs/proxy-wasm-go-sdk v0.0.13
12 changes: 12 additions & 0 deletions test/integrate/proxywasm/testdata/req-header-v1/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tetratelabs/proxy-wasm-go-sdk v0.0.13 h1:DOKidvWd0WTBvdzTvU6L7wtig+RktRj3S77ObF2YwNQ=
github.com/tetratelabs/proxy-wasm-go-sdk v0.0.13/go.mod h1:y1ZQT4bQEBnR8Do4nSOzb3roczzPvcAp8UrF6NEYWNY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
55 changes: 55 additions & 0 deletions test/integrate/proxywasm/testdata/req-header-v1/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package main

import (
"strconv"

"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
)

// build main like below with proxy-wasm-go-sdk v0.0.13 which works with
// TinyGo 0.19.0 (which works with Go 1.16).
//
// tinygo build -o main.wasm -scheduler=none -target=wasi --no-debug -wasm-abi=generic -tags 'abi_010' ./main.go
//
// Note: Old tooling is needed because mosn.io/proxy-wasm-go-host v2 is
// incompatible with current proxy-wasm SDKs.
func main() {
proxywasm.SetNewHttpContext(newHttpContext)
}

type myHttpContext struct {
// you must embed the default context so that you need not to re-implement all the methods by yourself
proxywasm.DefaultHttpContext
contextID uint32
}

func newHttpContext(rootContextID, contextID uint32) proxywasm.HttpContext {
return &myHttpContext{contextID: contextID}
}

func (ctx *myHttpContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action {
key := "Wasm-Context"
err := proxywasm.SetHttpRequestHeader(key, strconv.Itoa(int(ctx.contextID)))
if err != nil {
proxywasm.LogCritical("failed to set request header: " + key)
}
return types.ActionContinue
}
Binary file not shown.