ziggs/internal/system/stats_test.go

92 lines
1.8 KiB
Go
Raw Normal View History

2022-09-05 21:02:29 +00:00
package system
import (
"context"
"fmt"
2022-09-17 04:39:16 +00:00
"io"
"strings"
"sync"
"sync/atomic"
2022-09-05 21:02:29 +00:00
"testing"
2022-09-17 04:39:16 +00:00
"time"
2022-09-05 21:02:29 +00:00
2022-09-17 04:39:16 +00:00
"git.tcp.direct/kayos/common/entropy"
2022-09-05 21:02:29 +00:00
"github.com/davecgh/go-spew/spew"
"github.com/muesli/termenv"
)
2022-09-17 04:39:16 +00:00
var grindSet = int64(0)
var grindOnce = &sync.Once{}
func grind(ctx context.Context) {
2022-09-19 18:22:13 +00:00
if atomic.LoadInt64(&grindSet) > 50 {
2022-09-17 04:39:16 +00:00
return
}
2022-09-22 03:48:13 +00:00
var cancel context.CancelFunc
2022-09-17 04:39:16 +00:00
grindOnce.Do(
func() {
2022-09-22 03:48:13 +00:00
ctx, cancel = context.WithDeadline(ctx, time.Now().Add(10*time.Second))
2022-09-17 04:39:16 +00:00
})
atomic.AddInt64(&grindSet, 1)
time.Sleep(time.Duration(5000-(entropy.RNG(int(atomic.LoadInt64(&grindSet))*7500))) * time.Millisecond)
for {
select {
case <-ctx.Done():
2022-09-22 03:48:13 +00:00
cancel()
2022-09-17 04:39:16 +00:00
return
default:
go grind(ctx)
go io.Copy(io.Discard, strings.NewReader(entropy.RandStr(9999999999)))
}
}
}
var once = &sync.Once{}
2022-09-05 21:02:29 +00:00
func TestCPULoadGradient(t *testing.T) {
2022-09-17 04:39:16 +00:00
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(15*time.Second))
2022-09-05 21:02:29 +00:00
defer cancel()
grad, err := CPULoadGradient(ctx, "deepskyblue", "deeppink")
if err != nil {
t.Fatal(err)
}
p := termenv.ColorProfile()
spew.Dump(p)
s := termenv.String("yeet")
2022-09-17 04:39:16 +00:00
var count = 0
2022-09-05 21:02:29 +00:00
for {
select {
case <-ctx.Done():
2022-09-17 04:39:16 +00:00
t.Logf("done with gradient test")
2022-09-05 21:02:29 +00:00
return
2022-09-17 04:39:16 +00:00
case col := <-grad:
2022-09-05 21:02:29 +00:00
fmt.Println(col.Hex() + ": " + s.Foreground(p.Color(col.Hex())).String())
2022-09-17 04:39:16 +00:00
count++
default:
once.Do(func() {
fmt.Println("generating CPU load")
go grind(context.Background())
})
}
}
}
func TestCoreLoadHue(t *testing.T) {
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(20*time.Second))
defer cancel()
huint, err := CoreLoadHue(ctx)
if err != nil {
t.Fatal(err)
}
for {
select {
case <-ctx.Done():
t.Logf("done with core hue test")
return
case hue := <-huint:
fmt.Println(hue)
default:
2022-09-05 21:02:29 +00:00
}
}
}