-
Notifications
You must be signed in to change notification settings - Fork 14
/
tester.go
97 lines (89 loc) · 2.94 KB
/
tester.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* skogul, test receiver
*
* Copyright (c) 2019 Telenor Norge AS
* Author(s):
* - Kristian Lyngstøl <kly@kly.no>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
package receiver
import (
"fmt"
"math/rand"
"runtime"
"time"
"github.com/sirupsen/logrus"
"github.com/telenornms/skogul"
)
var testerLog = skogul.Logger("receiver", "tester")
// Tester synthesise dummy data.
type Tester struct {
Metrics int64 `doc:"Number of metrics in each container"`
Values int64 `doc:"Number of unique values for each metric"`
Threads int `doc:"Threads to spawn"`
Delay skogul.Duration `doc:"Sleep time between each metric is generated, if any."`
Handler skogul.HandlerRef `doc:"Reference to a handler where the data is sent"`
logger *logrus.Entry
}
func (tst *Tester) generate(t time.Time) skogul.Container {
c := skogul.Container{}
c.Metrics = make([]*skogul.Metric, tst.Metrics)
for i := int64(0); i < tst.Metrics; i++ {
m := skogul.Metric{}
m.Time = &t
m.Metadata = map[string]interface{}{}
m.Metadata["id"] = skogul.Identity[tst]
m.Metadata["key1"] = i
m.Data = map[string]interface{}{}
for key := int64(0); key < tst.Values; key++ {
m.Data[fmt.Sprintf("metric%d", key)] = rand.Int63()
}
c.Metrics[i] = &m
}
return c
}
// Start never returns.
func (tst *Tester) Start() error {
tst.logger = skogul.Logger("receiver", "tester").WithField("name", skogul.Identity[tst])
if tst.Threads == 0 {
tst.Threads = runtime.NumCPU()
tst.logger.WithField("threads", tst.Threads).Debug("No threads set, defaulting to runtime.NumCPU()")
}
if tst.Metrics < 1 {
tst.Metrics = 10
testerLog.WithField("metrics", tst.Metrics).Debug("No Metrics specified for testing, defaulting to default value")
}
if tst.Values < 1 {
tst.Values = 50
testerLog.WithField("values", tst.Values).Debug("No Values specified for testing, defaulting to default value")
}
for i := 1; i < tst.Threads; i++ {
go tst.run()
}
tst.run()
return nil
}
// run() is a signle thread of the tester that runs for ever.
func (tst *Tester) run() {
for {
c := tst.generate(time.Now())
if err := tst.Handler.H.TransformAndSend(&c); err != nil {
testerLog.Errorf("Failed to transform and send metrics: %v", err)
}
time.Sleep(tst.Delay.Duration)
}
}