|
| 1 | +// TinyGo JSON serialization and deserialization benchmark. |
| 2 | +// |
| 3 | +// This benchmark tests JSON parsing and serialization performance using |
| 4 | +// the standard library encoding/json package. |
| 5 | + |
| 6 | +package main |
| 7 | + |
| 8 | +import ( |
| 9 | + "encoding/json" |
| 10 | + "fmt" |
| 11 | + "os" |
| 12 | +) |
| 13 | + |
| 14 | +// WASM imports for sightglass API |
| 15 | +// |
| 16 | +//go:wasm-module bench |
| 17 | +//export start |
| 18 | +func benchStart() |
| 19 | + |
| 20 | +//go:wasm-module bench |
| 21 | +//export end |
| 22 | +func benchEnd() |
| 23 | + |
| 24 | +// Data structures matching the Rust benchmark |
| 25 | +type User struct { |
| 26 | + ID uint64 `json:"id"` |
| 27 | + Username string `json:"username"` |
| 28 | + Email string `json:"email"` |
| 29 | + FullName string `json:"full_name"` |
| 30 | + IsActive bool `json:"is_active"` |
| 31 | + CreatedAt string `json:"created_at"` |
| 32 | + UpdatedAt string `json:"updated_at"` |
| 33 | + Profile Profile `json:"profile"` |
| 34 | + Settings Settings `json:"settings"` |
| 35 | + Posts []Post `json:"posts"` |
| 36 | +} |
| 37 | + |
| 38 | +type Profile struct { |
| 39 | + Bio string `json:"bio"` |
| 40 | + AvatarURL string `json:"avatar_url"` |
| 41 | + Location string `json:"location"` |
| 42 | + Website *string `json:"website"` |
| 43 | + SocialLinks []string `json:"social_links"` |
| 44 | +} |
| 45 | + |
| 46 | +type Settings struct { |
| 47 | + Theme string `json:"theme"` |
| 48 | + Language string `json:"language"` |
| 49 | + NotificationsEnabled bool `json:"notifications_enabled"` |
| 50 | + PrivacyLevel string `json:"privacy_level"` |
| 51 | + Preferences map[string]string `json:"preferences"` |
| 52 | +} |
| 53 | + |
| 54 | +type Post struct { |
| 55 | + ID uint64 `json:"id"` |
| 56 | + Title string `json:"title"` |
| 57 | + Content string `json:"content"` |
| 58 | + Tags []string `json:"tags"` |
| 59 | + Likes uint32 `json:"likes"` |
| 60 | + CommentsCount uint32 `json:"comments_count"` |
| 61 | + PublishedAt string `json:"published_at"` |
| 62 | +} |
| 63 | + |
| 64 | +func main() { |
| 65 | + // Read the JSON input file |
| 66 | + jsonData, err := os.ReadFile("tinygo-json.input") |
| 67 | + if err != nil { |
| 68 | + fmt.Fprintf(os.Stderr, "Error reading input: %v\n", err) |
| 69 | + os.Exit(1) |
| 70 | + } |
| 71 | + |
| 72 | + benchStart() |
| 73 | + |
| 74 | + // Deserialize: Parse JSON into structured data |
| 75 | + var users []User |
| 76 | + if err := json.Unmarshal(jsonData, &users); err != nil { |
| 77 | + fmt.Fprintf(os.Stderr, "Error parsing JSON: %v\n", err) |
| 78 | + os.Exit(1) |
| 79 | + } |
| 80 | + |
| 81 | + // Serialize: Convert back to JSON |
| 82 | + serialized, err := json.Marshal(users) |
| 83 | + if err != nil { |
| 84 | + fmt.Fprintf(os.Stderr, "Error serializing JSON: %v\n", err) |
| 85 | + os.Exit(1) |
| 86 | + } |
| 87 | + |
| 88 | + benchEnd() |
| 89 | + |
| 90 | + fmt.Fprintf(os.Stderr, "[tinygo-json] processed %d users\n", len(users)) |
| 91 | + fmt.Fprintf(os.Stderr, "[tinygo-json] serialized size: %d bytes\n", len(serialized)) |
| 92 | +} |
0 commit comments