// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. //go:build script // +build script package js_test import ( "github.com/TeaOSLab/EdgeNode/internal/js" "github.com/TeaOSLab/EdgeNode/internal/utils/testutils" "github.com/iwind/TeaGo/types" "runtime" "runtime/debug" "testing" "time" ) func TestIsolatePool_GetContext(t *testing.T) { pool, err := js.NewIsolatePool(1) if err != nil { t.Fatal(err) } ctx, err := pool.GetContext() if err != nil { t.Fatal(err) } defer pool.PutContext(ctx) v, err := ctx.Run("1+1", "") if err != nil { t.Fatal(err) } t.Log("result:", v) } func TestIsolatePool_Memory(t *testing.T) { if !testutils.IsSingleTesting() { return } t.Log("creating pool") var req = &FakeRequest{} var before = time.Now() pool, err := js.NewAutoIsolatePool() if err != nil { t.Fatal(err) } t.Log("created pool", time.Since(before).Seconds()*1000, "ms") time.Sleep(10 * time.Second) t.Log("starting") for i := 0; i < 10_000_000; i++ { if i > 0 && i%50000 == 0 { t.Log(i) time.Sleep(1 * time.Second) } ctx, err := pool.GetContext() if err != nil { t.Fatal(err) } var requestObjectId = ctx.AddGoRequestObject(req) _, err = ctx.Run(`(function () { let req = new gojs.net.http.Request() req.setGoObject(`+types.String(requestObjectId)+`) let resp = new gojs.net.http.Response() if (req.path.match(/webhook/)) { req.deleteHeader("Cookie") req.setHeader("User-Agent", "gojs/1.0") resp.send(200, "Hello, World") } if (req.path == "/favicon.ico") { req.done() } })()`, "") if err != nil { t.Fatal(err) } pool.PutContext(ctx) } t.Log("created", time.Since(before).Seconds(), "s") if testutils.IsSingleTesting() { time.Sleep(5 * time.Second) t.Log("gc") runtime.GC() debug.FreeOSMemory() time.Sleep(120 * time.Second) t.Log("gc again") runtime.GC() debug.FreeOSMemory() time.Sleep(5 * time.Hour) t.Log("2 minutes") } } func TestIsolatePool_Memory2(t *testing.T) { if !testutils.IsSingleTesting() { return } pool, err := js.NewIsolatePool(256) if err != nil { t.Fatal(err) } time.Sleep(4 * time.Second) var gcPause = func() { var before = time.Now() runtime.GC() var costSeconds = time.Since(before).Seconds() var stats = &debug.GCStats{} debug.ReadGCStats(stats) t.Log("GC pause:", stats.PauseTotal.Seconds()*1000, "ms", "cost:", costSeconds*1000, "ms") } gcPause() time.Sleep(10 * time.Second) _ = pool t.Log(pool.MaxSize()) } func BenchmarkIsolatePool_GetContext(b *testing.B) { pool, err := js.NewIsolatePool(32) if err != nil { b.Fatal(err) } b.RunParallel(func(pb *testing.PB) { for pb.Next() { func() { ctx, err := pool.GetContext() if err != nil { return } defer pool.PutContext(ctx) // TODO 需要实现测试用例 /**err = ctx.AddGoObject(&FakeRequest{ host: "example.com", remoteAddr: "127.0.0.1", }) if err != nil { b.Log(err) return } _, err = ctx.Run("req.host() + ' @ ' + req.remoteAddr() + ' @ ' + req.proto() + ' @ ' + req.url()", "") if err != nil { b.Log(err) return }**/ }() } }) }