forked from google/mtail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex_test.go
153 lines (136 loc) · 3.78 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2011 Google Inc. All Rights Reserved.
// This file is available under the Apache license.
package main
import (
"bytes"
"flag"
"fmt"
"os"
"path/filepath"
"strconv"
"testing"
"github.com/google/mtail/metrics"
"github.com/google/mtail/mtail"
"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.json",
},
{
"examples/sftp.mtail",
"testdata/sftp_chroot.log",
"testdata/sftp_chroot.json",
},
{
"examples/dhcpd.mtail",
"testdata/anonymised_dhcpd_log",
"testdata/anonymised_dhcpd_log.json",
},
}
func CompileAndLoad(programfile string, ms *metrics.Store, lines chan string) (*vm.Loader, error) {
p, err := os.Open(programfile)
if err != nil {
return nil, fmt.Errorf("%s: could not open program file: %s", programfile, err)
}
defer p.Close()
name := filepath.Base(programfile)
w := watcher.NewFakeWatcher()
o := vm.LoaderOptions{W: w, Store: ms, Lines: lines, SyslogUseCurrentYear: false}
l, err := vm.NewLoader(o)
if err != nil {
return nil, fmt.Errorf("couldn't create program loader: %s", err)
}
if pErr := l.CompileAndRun(name, p); pErr != nil {
return nil, fmt.Errorf("couldn't compile program: %s: %s", programfile, pErr)
}
return l, nil
}
func TestExamplePrograms(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode")
}
for _, tc := range exampleProgramTests {
w := watcher.NewFakeWatcher()
o := mtail.Options{Progs: tc.programfile, W: w}
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
}
// Dirty hack to create json files :)
if false {
j, err := os.Create(tc.jsonfile)
if err != nil {
t.Errorf("%s: could not open json file: %s", tc.jsonfile, err)
continue
}
defer j.Close()
if err := mtail.WriteMetrics(j); err != nil {
t.Errorf("couldn't marshall metrics: %q", 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()
var m, ex bytes.Buffer
mtail.Close()
mtail.WriteMetrics(&m)
m.WriteString("\n") // Golden data has trailing newline.
if _, err := ex.ReadFrom(j); err != nil {
t.Fatalf("Coldn't read from json: %s", err)
}
diff := pretty.Compare(m.String(), ex.String())
if len(diff) > 0 {
t.Errorf("%s: metrics don't match:\n%s", tc.programfile, diff)
}
}
}
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")
}