forked from google/mtail
/
ex_test.go
127 lines (111 loc) · 2.96 KB
/
ex_test.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2011 Google Inc. All Rights Reserved.
// This file is available under the Apache license.
package main
import (
"flag"
"os"
"strconv"
"testing"
"github.com/google/mtail/metrics"
"github.com/google/mtail/mtail"
"github.com/google/mtail/testdata"
"github.com/google/mtail/vm"
"github.com/google/mtail/watcher"
"github.com/kylelemons/godebug/pretty"
)
var (
recordBenchmark = flag.Bool("record_benchmark", false, "Record the benchmark results to 'benchmark_results.csv'.")
)
var exampleProgramTests = []struct {
programfile string // Example program file.
logfile string // Sample log input.
jsonfile string // Expected metrics after processing.
}{
{
"examples/rsyncd.mtail",
"testdata/rsyncd.log",
"testdata/rsyncd.golden",
},
{
"examples/sftp.mtail",
"testdata/sftp_chroot.log",
"testdata/sftp_chroot.golden",
},
{
"examples/dhcpd.mtail",
"testdata/anonymised_dhcpd_log",
"testdata/anonymised_dhcpd_log.golden",
},
{
"examples/ntpd.mtail",
"testdata/ntp4",
"testdata/ntp4.golden",
},
{
"examples/ntpd.mtail",
"testdata/xntp3_peerstats",
"testdata/xntp3_peerstats.golden",
},
}
func TestExamplePrograms(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode")
}
for _, tc := range exampleProgramTests {
w := watcher.NewFakeWatcher()
store := &metrics.Store{}
o := mtail.Options{Progs: tc.programfile, W: w, Store: store}
mtail, err := mtail.New(o)
if err != nil {
t.Fatalf("create mtail failed: %s", err)
}
if err := mtail.OneShot(tc.logfile); err != nil {
t.Errorf("Oneshot failed for %s: %s", tc.logfile, err)
continue
}
j, err := os.Open(tc.jsonfile)
if err != nil {
t.Fatalf("%s: could not open json file: %s", tc.jsonfile, err)
}
defer j.Close()
golden_store := &metrics.Store{}
testdata.ReadTestData(j, tc.programfile, golden_store)
mtail.Close()
diff := pretty.Compare(golden_store, store)
if len(diff) > 0 {
t.Errorf("%s: metrics don't match:\n%s", tc.programfile, diff)
t.Errorf("Store metrics: %#v", store.Metrics)
}
}
}
func benchmarkProgram(b *testing.B, programfile string, logfile string) {
w := watcher.NewFakeWatcher()
o := mtail.Options{Progs: programfile, W: w}
mtail, err := mtail.New(o)
if err != nil {
b.Fatalf("Failed to create mtail: %s", err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := mtail.OneShot(logfile); err != nil {
b.Errorf("OneShot log parse failed: %s", err)
}
}
b.StopTimer()
mtail.Close()
l, err := strconv.ParseInt(vm.LineCount.String(), 10, 64)
if err != nil {
b.Fatalf("strconv.ParseInt failed: %s", err)
return
}
b.SetBytes(l)
}
func BenchmarkRsyncdProgram(b *testing.B) {
benchmarkProgram(b, "examples/rsync.mtail", "testdata/rsyncd.log")
}
func BenchmarkSftpProgram(b *testing.B) {
benchmarkProgram(b, "examples/sftp.mtail", "testdata/sftp_chroot.log")
}
func BenchmarkDhcpdProgram(b *testing.B) {
benchmarkProgram(b, "examples/dhcpd.mtail", "testdata/anonymised_dhcpd_log")
}