Skip to content

Instantly share code, notes, and snippets.

@ehfeng
Created June 27, 2023 17:22
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ehfeng/bf86f71ff59019406e433d84ff4ea5fb to your computer and use it in GitHub Desktop.
Parallelism, JSON parsing, and v8
package main
import (
"encoding/json"
"fmt"
"runtime"
"sync"
"time"
v8 "rogchap.com/v8go"
)
func generateJSON(size int) string {
result := []map[string]string{}
for i := 0; i < size; i++ {
result = append(result, map[string]string{
"key": fmt.Sprintf("value-%d", i),
})
}
b, err := json.Marshal(result)
if err != nil {
panic(err)
}
return string(b)
}
func startIsolatesInParallel(n int, json string) {
start := time.Now()
var wg sync.WaitGroup
for i := 0; i < n; i++ {
wg.Add(1)
go func() {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
isolate := v8.NewIsolate()
defer isolate.Dispose()
ctx := v8.NewContext(isolate)
_, err := v8.JSONParse(ctx, json)
if err != nil {
panic(err)
}
wg.Done()
}()
}
wg.Wait()
var size string
if len(json) > 1000 {
size = fmt.Sprintf("%dKB", len(json)/1000)
} else if len(json) > 1_000_000 {
size = fmt.Sprintf("%dMB", len(json)/1000000)
}
fmt.Println("json size", size, n, ":", time.Since(start))
}
func main() {
var start time.Time
start = time.Now()
isolate := v8.NewIsolate()
v8.NewContext(isolate)
isolate.Dispose()
fmt.Println("Warmup:", time.Since(start))
startIsolatesInParallel(1, generateJSON(100))
startIsolatesInParallel(2, generateJSON(100))
startIsolatesInParallel(4, generateJSON(100))
startIsolatesInParallel(6, generateJSON(100))
startIsolatesInParallel(1, generateJSON(1000))
startIsolatesInParallel(2, generateJSON(1000))
startIsolatesInParallel(4, generateJSON(1000))
startIsolatesInParallel(6, generateJSON(1000))
startIsolatesInParallel(1, generateJSON(10000))
startIsolatesInParallel(2, generateJSON(10000))
startIsolatesInParallel(4, generateJSON(10000))
startIsolatesInParallel(6, generateJSON(10000))
}
// run on a 8-core machine
// Warmup: 6.625875ms
// json size 1KB 1 : 1.324083ms
// json size 1KB 2 : 1.303583ms
// json size 1KB 4 : 1.668041ms
// json size 1KB 6 : 2.613834ms
// json size 19KB 1 : 1.2825ms
// json size 19KB 2 : 1.416292ms
// json size 19KB 4 : 1.868833ms
// json size 19KB 6 : 2.824125ms
// json size 208KB 1 : 3.199583ms
// json size 208KB 2 : 3.1735ms
// json size 208KB 4 : 4.83775ms
// json size 208KB 6 : 6.479958ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment