Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

2.1 KB
Raw Blame
package monitoring

import (
	"math/rand"
	"sync"
	"testing"
	"time"
)

func TestMetricsHistoryConcurrentAccess(t *testing.T) {
	mh := NewMetricsHistory(256, time.Minute)

	const iterations = 1000

	var wg sync.WaitGroup
	wg.Add(5)

	go func() {
		defer wg.Done()
		for i := 0; i < iterations; i++ {
			mh.AddGuestMetric("guest-1", "cpu", rand.Float64()*100, time.Now())
			mh.AddGuestMetric("guest-1", "memory", rand.Float64()*100, time.Now())
			mh.AddGuestMetric("guest-1", "memoryused", 4096, time.Now())
			mh.AddGuestMetric("guest-2", "disk", rand.Float64()*100, time.Now())
			time.Sleep(time.Microsecond)
		}
	}()

	go func() {
		defer wg.Done()
		for i := 0; i < iterations; i++ {
			mh.AddNodeMetric("node-1", "cpu", rand.Float64()*100, time.Now())
			mh.AddNodeMetric("node-1", "memory", rand.Float64()*100, time.Now())
			mh.AddNodeMetric("node-1", "memoryused", 8192, time.Now())
			mh.AddStorageMetric("storage-1", "usage", rand.Float64()*100, time.Now())
			time.Sleep(time.Microsecond)
		}
	}()

	go func() {
		defer wg.Done()
		for i := 0; i < iterations; i++ {
			mh.GetGuestMetrics("guest-1", "cpu", time.Minute)
			mh.GetGuestMetrics("guest-1", "memory", time.Minute)
			mh.GetGuestMetrics("guest-1", "memoryused", time.Minute)
			mh.GetGuestMetrics("guest-2", "disk", time.Minute)
			time.Sleep(time.Microsecond)
		}
	}()

	go func() {
		defer wg.Done()
		for i := 0; i < iterations; i++ {
			mh.GetNodeMetrics("node-1", "cpu", time.Minute)
			mh.GetNodeMetrics("node-1", "memory", time.Minute)
			mh.GetNodeMetrics("node-1", "memoryused", time.Minute)
			// Storage metrics are exposed through Monitor, but simulate reads via guest metrics map
			mh.GetGuestMetrics("storage-1", "usage", time.Minute)
			time.Sleep(time.Microsecond)
		}
	}()

	go func() {
		defer wg.Done()
		for i := 0; i < iterations; i++ {
			mh.Cleanup()
			time.Sleep(time.Microsecond)
		}
	}()

	wg.Wait()
	if len(mh.GetGuestMetrics("guest-1", "memoryused", time.Minute)) == 0 ||
		len(mh.GetNodeMetrics("node-1", "memoryused", time.Minute)) == 0 {
		t.Fatal("concurrent cleanup lost live used-memory samples")
	}
}

// rand is automatically seeded in Go 1.20+