TangoLLVM Go 1.22 LLVM 后端 · 基准结果

基准说明 / 测量源码

Sonic 服务循环源码

这里逐字展示测量所用的源码,保留原有注释、缩进和换行。

历史 JSON 夹具与完整构建清单位于受限归档。基准说明列出输入大小和 Sonic 版本。

服务解码循环 · main.go

package main

import (
	"bytes"
	"compress/flate"
	"fmt"
	"io"
	"runtime"
	"runtime/metrics"
	"sync"
	"os"
"time"
"strings"
"github.com/bytedance/sonic/decoder"

	"github.com/bytedance/sonic/ast"
)

var requests = 1024

//go:noinline
func handle(data []byte, depth int) map[string]interface{} {
	if depth > 0 {
		result := handle(data, depth-1)
		runtime.KeepAlive(data)
		return result
	}
	reader := flate.NewReader(bytes.NewReader(data))
	body, err := io.ReadAll(reader)
	reader.Close()
	if err != nil {
		panic(err)
	}
	raw := string(body)
	node := ast.NewRaw(raw)
	if !node.Get("activity_trace").Exists() {
		panic("missing activity_trace")
	}
	result, err := SonicDecoder(raw)
	if err != nil {
		panic(err)
	}
	labels := make(map[string]string)
	for key, value := range result {
		if text, ok := value.(string); ok {
			labels[key] = text
		}
	}
	result["replay_labels"] = labels
	return result
}

func main() {
            if os.Args[1]=="train" {requests=384; jsonStr=strings.Replace(jsonStr,"testValue","training-value",1)}
            startTime:=time.Now()
            defer func(){fmt.Printf("ns/req %.2f\n",float64(time.Since(startTime).Nanoseconds())/float64(requests))}()
	var compressed bytes.Buffer
	w, _ := flate.NewWriter(&compressed, flate.DefaultCompression)
	w.Write([]byte(jsonStr))
	w.Close()
	samples := []metrics.Sample{{Name: "/cpu/classes/gc/total:cpu-seconds"}, {Name: "/sched/pauses/stopping/gc:seconds"}, {Name: "/gc/heap/live:bytes"}}
	runtime.GC()
	metrics.Read(samples)
	finishMetrics := bs2Metrics()
	start := samples[0].Value.Float64()
	before := append([]uint64(nil), samples[1].Value.Float64Histogram().Counts...)
	var held [32][4]map[string]interface{}
	var wg sync.WaitGroup
	for k := 0; k < 32; k++ {
		wg.Add(1)
		go func(worker int) {
			defer wg.Done()
			depth := 26
			if worker%5 == 0 {
				depth = 34
			}
			if worker == 0 {
				depth = 46
			}
			for i := 0; i < requests/32; i++ {
				held[worker][i%4] = decode(depth)
			}
		}(k)
	}
	wg.Wait()
	finishMetrics()
	metrics.Read(samples)
	hist := samples[1].Value.Float64Histogram()
	gcCost := 1e6 * (samples[0].Value.Float64() - start) / float64(requests)
	runtime.GC()
	metrics.Read(samples[2:])
	runtime.KeepAlive(held)
	fmt.Printf("gc-us/req %.5f\nretained-live-B %d\n",gcCost,samples[2].Value.Uint64())
	fmt.Printf("gc_us_req %.4f live %d buckets", gcCost, samples[2].Value.Uint64())
	for i, v := range hist.Counts {
		if v > before[i] {
			fmt.Printf(" %.4f=%d", hist.Buckets[i+1]*1e6, v-before[i])
		}
	}
	fmt.Println()
}

func decode(depth int) map[string]interface{} { if depth>0 {v:=decode(depth-1);runtime.KeepAlive(depth);return v}; v,err:=SonicDecoder(jsonStr);if err!=nil{panic(err)};return v }

func SonicDecoder(jsonStr string) (map[string]interface{}, error) {
	var m map[string]interface{}
	dc := decoder.NewDecoder(jsonStr)
	dc.UseNumber()
	return m, dc.Decode(&m)
}

共用采集 · metrics.go

// Copy into a staged workload package and defer bs2Metrics()() around the workload.
package main

import (
	"fmt"
	"math"
	"os"
	"runtime/metrics"
	"runtime/pprof"
)

func bs2Metrics() func() {
	s := []metrics.Sample{
		{Name: "/cpu/classes/gc/total:cpu-seconds"},
		{Name: "/gc/cycles/total:gc-cycles"},
		{Name: "/gc/scan/heap:bytes"},
		{Name: "/gc/scan/stack:bytes"},
		{Name: "/gc/heap/live:bytes"},
		{Name: "/sched/pauses/stopping/gc:seconds"},
		{Name: "/sched/pauses/total/gc:seconds"},
	}
	var profile *os.File
	if name := os.Getenv("BS2_CPUPROFILE"); name != "" {
		var err error
		profile, err = os.Create(name)
		if err != nil {
			panic(err)
		}
		if err = pprof.StartCPUProfile(profile); err != nil {
			panic(err)
		}
	}
	metrics.Read(s)
	cpu, cycles := s[0].Value.Float64(), s[1].Value.Uint64()
	var before [2][]uint64
	for i := range before {
		before[i] = append([]uint64(nil), s[5+i].Value.Float64Histogram().Counts...)
	}
	return func() {
		if profile != nil {
			defer profile.Close()
			defer pprof.StopCPUProfile()
		}
		metrics.Read(s)
		fmt.Printf("bs2-gc-cpu-s %.9f\nbs2-gc-cycles %d\n", s[0].Value.Float64()-cpu, s[1].Value.Uint64()-cycles)
		for i, label := range []string{"heap-scan", "stack-scan", "live-heap"} {
			fmt.Printf("bs2-%s-B %d\n", label, s[2+i].Value.Uint64())
		}
		for i, label := range []string{"stopping", "total"} {
			h := s[5+i].Value.Float64Histogram()
			var count uint64
			for j, n := range h.Counts {
				count += n - before[i][j]
			}
			for _, q := range []int{50, 99, 100} {
				value := math.NaN()
				var sum uint64
				for j, n := range h.Counts {
					sum += n - before[i][j]
					if count > 0 && sum*100 >= count*uint64(q) {
						value = h.Buckets[j+1] * 1e6
						break
					}
				}
				fmt.Printf("bs2-%s-p%d-upper-us %.3f\n", label, q, value)
			}
		}
	}
}