-
Notifications
You must be signed in to change notification settings - Fork 36
Add TinyGo JSON/Regex serialization benchmarks #302
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
Open
posborne
wants to merge
2
commits into
bytecodealliance:main
Choose a base branch
from
posborne:tinygo-benchmarks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| ARG TINYGO_VERSION=0.40.0 | ||
| FROM tinygo/tinygo:${TINYGO_VERSION} | ||
| WORKDIR /home/tinygo | ||
| ADD --chown=tinygo:tinygo go-benchmark go-benchmark | ||
| WORKDIR /home/tinygo/go-benchmark | ||
| RUN tinygo build -o benchmark.wasm -target=wasi . | ||
| USER root | ||
| RUN mkdir -p /benchmark && cp benchmark.wasm /benchmark/ | ||
| # We output the Wasm file to the `/benchmark` directory, where the client expects it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # TinyGo benchmarks - benchmarks compiled with TinyGo to WebAssembly. | ||
| # | ||
| # TinyGo is a Go compiler designed for small places, producing compact | ||
| # WebAssembly binaries suitable for resource-constrained environments. | ||
|
|
||
| tinygo/tinygo-json.wasm | ||
| tinygo/tinygo-regex.wasm |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| ARG TINYGO_VERSION=0.40.0 | ||
| FROM tinygo/tinygo:${TINYGO_VERSION} | ||
|
|
||
| WORKDIR /usr/src | ||
|
|
||
| # Copy all benchmark source and build script | ||
| COPY --chown=tinygo:tinygo json json | ||
| COPY --chown=tinygo:tinygo regex regex | ||
| COPY --chown=tinygo:tinygo build.sh . | ||
|
|
||
| # Create output directory as root and set permissions | ||
| USER root | ||
| RUN mkdir -p /benchmark && chown tinygo:tinygo /benchmark | ||
|
|
||
| # Switch back to tinygo user and build | ||
| USER tinygo | ||
| RUN OUTPUT_DIR=/benchmark ./build.sh | ||
|
|
||
| # Output goes to /benchmark where sightglass expects it |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # TinyGo Benchmarks | ||
|
|
||
| This directory contains multiple benchmarks compiled with TinyGo to WebAssembly. | ||
|
|
||
| TinyGo is a Go compiler designed for small places, producing compact | ||
| WebAssembly binaries suitable for resource-constrained environments. | ||
|
|
||
| ## Benchmarks | ||
|
|
||
| ### tinygo-json.wasm | ||
| JSON serialization and deserialization using Go's `encoding/json` package. | ||
| - Input: ~2.1MB JSON file with 100 user records | ||
| - Tests: Parse and stringify operations | ||
|
|
||
| ### tinygo-regex.wasm | ||
| Regular expression matching using Go's `regexp` package. | ||
| - Input: ~6.5MB text corpus (same as `benchmarks/regex/default.input`) | ||
| - Tests: 3 regex patterns (emails, URIs, IPs) matching the Rust benchmark | ||
|
|
||
| ## Building | ||
|
|
||
| From the `benchmarks` directory: | ||
|
|
||
| ```bash | ||
| ./build.sh tinygo/ | ||
| ``` | ||
|
|
||
| ## Adding New Benchmarks | ||
|
|
||
| To add a new TinyGo benchmark, create a new directory containing `main.go`, | ||
| `go.mod`, `default.input`, and expected output files. The build script will | ||
| automatically discover and build it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| #!/bin/sh | ||
| set -e | ||
|
|
||
| # Configuration | ||
| TINYGO="${TINYGO:-tinygo}" | ||
| OUTPUT_DIR="${OUTPUT_DIR:-$(pwd)}" | ||
| SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" | ||
|
|
||
| # Create output directory if it doesn't exist | ||
| mkdir -p "$OUTPUT_DIR" | ||
|
|
||
| cd "$SCRIPT_DIR" | ||
|
|
||
| # Auto-discover benchmarks: any directory with a main.go | ||
| for dir in */; do | ||
| # Remove trailing slash | ||
| benchmark=$(basename "$dir") | ||
|
|
||
| # Skip if not a benchmark directory (no main.go) | ||
| [ ! -f "$dir/main.go" ] && continue | ||
|
|
||
| echo "Building TinyGo $benchmark benchmark..." | ||
|
|
||
| cd "$dir" | ||
|
|
||
| # Build with TinyGo | ||
| "$TINYGO" build -o "$OUTPUT_DIR/tinygo-$benchmark.wasm" -target=wasi -opt=2 -gc=leaking . | ||
|
|
||
| cd "$SCRIPT_DIR" | ||
|
|
||
| echo "> Built tinygo-$benchmark.wasm" | ||
| done | ||
|
|
||
| echo "All TinyGo benchmarks built successfully" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| module benchmark | ||
|
|
||
| go 1.21 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| // TinyGo JSON serialization and deserialization benchmark. | ||
| // | ||
| // This benchmark tests JSON parsing and serialization performance using | ||
| // the standard library encoding/json package. | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| ) | ||
|
|
||
| // WASM imports for sightglass API | ||
| // | ||
| //go:wasm-module bench | ||
| //export start | ||
| func benchStart() | ||
|
|
||
| //go:wasm-module bench | ||
| //export end | ||
| func benchEnd() | ||
|
|
||
| // Data structures matching the Rust benchmark | ||
| type User struct { | ||
| ID uint64 `json:"id"` | ||
| Username string `json:"username"` | ||
| Email string `json:"email"` | ||
| FullName string `json:"full_name"` | ||
| IsActive bool `json:"is_active"` | ||
| CreatedAt string `json:"created_at"` | ||
| UpdatedAt string `json:"updated_at"` | ||
| Profile Profile `json:"profile"` | ||
| Settings Settings `json:"settings"` | ||
| Posts []Post `json:"posts"` | ||
| } | ||
|
|
||
| type Profile struct { | ||
| Bio string `json:"bio"` | ||
| AvatarURL string `json:"avatar_url"` | ||
| Location string `json:"location"` | ||
| Website *string `json:"website"` | ||
| SocialLinks []string `json:"social_links"` | ||
| } | ||
|
|
||
| type Settings struct { | ||
| Theme string `json:"theme"` | ||
| Language string `json:"language"` | ||
| NotificationsEnabled bool `json:"notifications_enabled"` | ||
| PrivacyLevel string `json:"privacy_level"` | ||
| Preferences map[string]string `json:"preferences"` | ||
| } | ||
|
|
||
| type Post struct { | ||
| ID uint64 `json:"id"` | ||
| Title string `json:"title"` | ||
| Content string `json:"content"` | ||
| Tags []string `json:"tags"` | ||
| Likes uint32 `json:"likes"` | ||
| CommentsCount uint32 `json:"comments_count"` | ||
| PublishedAt string `json:"published_at"` | ||
| } | ||
|
|
||
| func main() { | ||
| // Read the JSON input file | ||
| jsonData, err := os.ReadFile("tinygo-json.input") | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error reading input: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| benchStart() | ||
|
|
||
| // Deserialize: Parse JSON into structured data | ||
| var users []User | ||
| if err := json.Unmarshal(jsonData, &users); err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error parsing JSON: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| // Serialize: Convert back to JSON | ||
| serialized, err := json.Marshal(users) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error serializing JSON: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| benchEnd() | ||
|
|
||
| fmt.Fprintf(os.Stderr, "[tinygo-json] processed %d users\n", len(users)) | ||
| fmt.Fprintf(os.Stderr, "[tinygo-json] serialized size: %d bytes\n", len(serialized)) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| module tinygo-regex | ||
|
|
||
| go 1.21 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // TinyGo regex benchmark. | ||
| // | ||
| // This benchmark tests regular expression matching performance using | ||
| // the standard library regexp package. | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "regexp" | ||
| ) | ||
|
|
||
| // WASM imports for sightglass API | ||
| // | ||
| //go:wasm-module bench | ||
| //export start | ||
| func benchStart() | ||
|
|
||
| //go:wasm-module bench | ||
| //export end | ||
| func benchEnd() | ||
|
|
||
| func main() { | ||
| // Read the input text file | ||
| path := "tinygo-regex.input" | ||
| fmt.Fprintf(os.Stderr, "[regex] matching %s\n", path) | ||
| data, err := os.ReadFile(path) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error reading input: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| text := string(data) | ||
|
|
||
| // Use the same patterns as the Rust benchmark | ||
| emailPattern := `[\w\.+-]+@[\w\.-]+\.[\w\.-]+` | ||
| uriPattern := `[\w]+://[^/\s?#]+[^\s?#]+(?:\?[^\s#]*)?(?:#[^\s]*)?` | ||
| ipPattern := `(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9])` | ||
|
|
||
| benchStart() | ||
|
|
||
| emails := countMatches(text, emailPattern) | ||
| uris := countMatches(text, uriPattern) | ||
| ips := countMatches(text, ipPattern) | ||
|
|
||
| benchEnd() | ||
|
|
||
| fmt.Fprintf(os.Stderr, "[regex] found %d emails\n", emails) | ||
| fmt.Fprintf(os.Stderr, "[regex] found %d URIs\n", uris) | ||
| fmt.Fprintf(os.Stderr, "[regex] found %d IPs\n", ips) | ||
| } | ||
|
|
||
| func countMatches(text, pattern string) int { | ||
| re := regexp.MustCompile(pattern) | ||
| return len(re.FindAllString(text, -1)) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a comment here documenting why
-gc=leakingis used here?