aboutsummaryrefslogtreecommitdiff
path: root/web
diff options
context:
space:
mode:
authorXe <me@christine.website>2022-11-22 16:47:27 -0500
committerXe <me@christine.website>2022-11-22 16:47:27 -0500
commitb61b59318be6544632ac1f64b1237bb17b2e7a32 (patch)
treee6579112f325d30873227d7a1e9d5cf42361c867 /web
parentce1e2f44562a1c19b9052123fc9f346ae60fccf3 (diff)
downloadx-b61b59318be6544632ac1f64b1237bb17b2e7a32.tar.xz
x-b61b59318be6544632ac1f64b1237bb17b2e7a32.zip
web/mastosan: make parallel
Signed-off-by: Xe <me@christine.website>
Diffstat (limited to 'web')
-rw-r--r--web/mastosan/mastosan.go39
-rw-r--r--web/mastosan/mastosan_test.go8
2 files changed, 32 insertions, 15 deletions
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 = `<p>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.</p><p>To patch these vulnerabilities, upgrade Tailscale on your Windows machines to Tailscale v1.32.3 or later, or v1.33.257 or later (unstable).</p><p><a href="https://tailscale.com/blog/windows-security-vulnerabilities/" target="_blank" rel="nofollow noopener noreferrer"><span class="invisible">https://</span><span class="ellipsis">tailscale.com/blog/windows-sec</span><span class="invisible">urity-vulnerabilities/</span></a></p>`
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)
}