From b61b59318be6544632ac1f64b1237bb17b2e7a32 Mon Sep 17 00:00:00 2001 From: Xe Date: Tue, 22 Nov 2022 16:47:27 -0500 Subject: web/mastosan: make parallel Signed-off-by: Xe --- web/mastosan/mastosan.go | 39 ++++++++++++++++++++++++++------------- web/mastosan/mastosan_test.go | 8 ++++++-- 2 files changed, 32 insertions(+), 15 deletions(-) (limited to 'web') diff --git a/web/mastosan/mastosan.go b/web/mastosan/mastosan.go index 5ded39f..7e0d539 100644 --- a/web/mastosan/mastosan.go +++ b/web/mastosan/mastosan.go @@ -25,14 +25,34 @@ import ( "context" _ "embed" "log" + "math/rand" + "strconv" "strings" "github.com/tetratelabs/wazero" "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" ) -//go:embed testdata/mastosan.wasm -var mastosanWasm []byte +var ( + //go:embed testdata/mastosan.wasm + mastosanWasm []byte + + r wazero.Runtime + code wazero.CompiledModule +) + +func init() { + ctx := context.Background() + r = wazero.NewRuntime(ctx) + + wasi_snapshot_preview1.MustInstantiate(ctx, r) + + var err error + code, err = r.CompileModule(ctx, mastosanWasm) + if err != nil { + log.Panicln(err) + } +} // HTML2Slackdown converts a string full of HTML text to slack-flavored markdown. // @@ -41,24 +61,17 @@ var mastosanWasm []byte // This has an added latency of about 0.2 seconds per invocation, but this is as // fast as I can make it for now. func HTML2Slackdown(ctx context.Context, text string) (string, error) { - r := wazero.NewRuntime(ctx) - defer r.Close(ctx) - fout := &bytes.Buffer{} fin := bytes.NewBufferString(text) - config := wazero.NewModuleConfig().WithStdout(fout).WithStdin(fin).WithArgs("mastosan") - - wasi_snapshot_preview1.MustInstantiate(ctx, r) + name := strconv.Itoa(rand.Int()) + config := wazero.NewModuleConfig().WithStdout(fout).WithStdin(fin).WithArgs("mastosan").WithName(name) - code, err := r.CompileModule(ctx, mastosanWasm) + mod, err := r.InstantiateModule(ctx, code, config) if err != nil { - log.Panicln(err) - } - - if _, err = r.InstantiateModule(ctx, code, config); err != nil { return "", err } + defer mod.Close(ctx) return strings.TrimSpace(fout.String()), nil } diff --git a/web/mastosan/mastosan_test.go b/web/mastosan/mastosan_test.go index 5f1f9fa..e5f2d53 100644 --- a/web/mastosan/mastosan_test.go +++ b/web/mastosan/mastosan_test.go @@ -36,10 +36,14 @@ func TestHTML2Slackdown(t *testing.T) { const msg = `

Tailscale has recently been notified of security vulnerabilities in the Tailscale Windows client which allow a malicious website visited by a device running Tailscale to change the Tailscale daemon configuration and access information in the Tailscale local and peer APIs.

To patch these vulnerabilities, upgrade Tailscale on your Windows machines to Tailscale v1.32.3 or later, or v1.33.257 or later (unstable).

tailscale.com/blog/windows-sec

` func BenchmarkHTML2Slackdown(b *testing.B) { - for i := 0; i < b.N; i++ { + b.RunParallel(benchStep) +} + +func benchStep(pb *testing.PB) { + for pb.Next() { result, err := HTML2Slackdown(context.Background(), msg) if err != nil { - b.Fatal(err) + panic(err) } fmt.Fprintln(io.Discard, result) } -- cgit v1.2.3