summaryrefslogtreecommitdiff
path: root/deprecated-webircgateway/plugins/stats/plugin.go
blob: bdafc41e454dfdb04fdbc4f46314f8a5690e300a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main

import (
	"fmt"
	"math"
	"os"
	"runtime"
	"sync"
	"time"

	"github.com/kiwiirc/webircgateway/pkg/webircgateway"
)

func Start(gateway *webircgateway.Gateway, pluginsQuit *sync.WaitGroup) {
	gateway.Log(2, "Stats reporting plugin loading")
	go reportUsage(gateway)

	pluginsQuit.Done()
}

func reportUsage(gateway *webircgateway.Gateway) {
	started := time.Now()

	out := func(line string) {
		file, _ := os.OpenFile("stats_"+fmt.Sprintf("%v", started.Unix())+".csv",
			os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
		file.WriteString(line)
		file.Close()
	}

	out("time,rss,heapinuse,heapalloc,numroutines,numclients\n")

	for {
		time.Sleep(time.Second * 5)

		numClients := gateway.Clients.Count()
		mem := &runtime.MemStats{}
		runtime.ReadMemStats(mem)

		line := fmt.Sprintf(
			"%v,%v,%v,%v,%v,%v\n",
			math.Round(time.Now().Sub(started).Seconds()),
			mem.Sys/1024,
			mem.HeapInuse/1024,
			mem.HeapAlloc/1024,
			runtime.NumGoroutine(),
			numClients,
		)

		out(line)
	}
}