-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (54 loc) · 1.12 KB
/
main.go
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
53
54
55
56
57
58
59
60
61
package main
import (
"fmt"
"os"
"os/signal"
"sync"
"time"
)
func main() {
var metrics = map[int]int{}
var sizemetrics = map[int]int{}
mut := sync.Mutex{}
var closer = make(chan os.Signal)
signal.Notify(closer, os.Interrupt)
go func() {
var bytevol int
infloop:
for {
var bs = make([]byte, 512, 512)
numbytes, err := os.Stdin.Read(bs)
if err != nil {
fmt.Fprintln(os.Stderr, "Error reading from stdin:", err)
close(closer)
return
}
for i := 0; i < numbytes; i++ {
if bs[i] == '\n' {
bytevol += i
mut.Lock()
secondbucket := time.Now().Second()
metrics[secondbucket]++
sizemetrics[secondbucket] += bytevol
bytevol = 0
mut.Unlock()
continue infloop
}
}
bytevol += numbytes
}
}()
go func() {
t := time.NewTicker(time.Second)
defer t.Stop()
for linet := range t.C {
prevbucket := time.Now().Second() - 1
mut.Lock()
linepersec := metrics[prevbucket]
bytespersec := sizemetrics[prevbucket]
mut.Unlock()
fmt.Printf("%s - %d lines/sec %d kilobytes/sec\n", linet.String(), linepersec, bytespersec/1000)
}
}()
<-closer
}