cat / 012
clone

a spreadsheet for the terminal: lotus 1-2-3 looks, google sheets keys and formulas, one go binary

main
012 / internal / chart / stress_test.go
stress_test.go 2.7K
1//go:build stress23// Stress benchmarks of chart drawing: every chart type as text and as an4// image, at a typical size and a large one, over a few categories and5// over a full column of them (only what fits is drawn). Only built with6// -tags stress (see `make stress` and docs/contributing/limits.md).7package chart89import (10 "fmt"11 "image/color"12 "math"13 "strconv"14 "testing"1516 "src.rest/cat/012/internal/sheet"17)1819// stressData is n categories of three series of wavy values.20func stressData(n int) sheet.ChartData {21 d := sheet.ChartData{Categories: make([]string, n)}22 for i := range n {23 d.Categories[i] = "Cat " + strconv.Itoa(i+1)24 }25 for j := range 3 {26 s := sheet.ChartSeries{Name: "Series " + strconv.Itoa(j+1), Values: make([]float64, n)}27 for i := range n {28 s.Values[i] = 1000*math.Sin(float64(i+j)/7) + 200*float64(j)29 }30 d.Series = append(d.Series, s)31 }32 return d33}3435var stressTypes = []struct {36 name string37 t sheet.ChartType38}{39 {"column", sheet.ChartColumn},40 {"bar", sheet.ChartBar},41 {"line", sheet.ChartLine},42 {"pie", sheet.ChartPie},43 {"area", sheet.ChartArea},44 {"scatter", sheet.ChartScatter},45}4647var stressSizes = []struct{ w, h int }{{24, 10}, {120, 40}}4849// BenchmarkDraw is a chart drawn as text, as every frame showing it does.50func BenchmarkDraw(b *testing.B) {51 for _, n := range []int{12, 8192} {52 d := stressData(n)53 for _, ct := range stressTypes {54 for _, sz := range stressSizes {55 b.Run(fmt.Sprintf("%s/%dcats/%dx%d", ct.name, n, sz.w, sz.h), func(b *testing.B) {56 for b.Loop() {57 Draw(ct.t, d, sz.w, sz.h, Options{})58 }59 })60 }61 }62 }63}6465// BenchmarkImage is a chart's plot drawn as a kitty image.66func BenchmarkImage(b *testing.B) {67 pal := Palette{Grid: color.RGBA{128, 128, 128, 80}}68 d := stressData(12)69 for _, ct := range stressTypes {70 for _, sz := range stressSizes {71 b.Run(fmt.Sprintf("%s/%dx%d", ct.name, sz.w, sz.h), func(b *testing.B) {72 for b.Loop() {73 Image(ct.t, d, sz.w, sz.h, Options{CellW: 10, CellH: 20}, pal)74 }75 })76 }77 }78}7980// BenchmarkSixel is a chart's image encoded as sixel, in 256 colors, as81// it is whenever its data, size or colors change.82func BenchmarkSixel(b *testing.B) {83 pal := Palette{Series: [Colors]color.RGBA{{205, 49, 49, 255}, {13, 188, 121, 255}, {36, 114, 200, 255}}, Grid: color.RGBA{128, 128, 128, 80}}84 d := stressData(12)85 for _, ct := range stressTypes {86 for _, sz := range stressSizes {87 img := Image(ct.t, d, sz.w, sz.h, Options{CellW: 10, CellH: 20}, pal)88 b.Run(fmt.Sprintf("%s/%dx%d", ct.name, sz.w, sz.h), func(b *testing.B) {89 n := 090 for b.Loop() {91 n = len(Sixel(img, color.RGBA{0, 0, 0, 255}, 256))92 }93 b.ReportMetric(float64(n), "bytes")94 })95 }96 }97}