-
Notifications
You must be signed in to change notification settings - Fork 4
/
console_test.go
136 lines (101 loc) · 2.67 KB
/
console_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
package tlog
import (
"io"
"testing"
"time"
"github.com/nikandfor/assert"
"nikand.dev/go/hacked/low"
"tlog.app/go/loc"
"tlog.app/go/tlog/tlwire"
)
type MultiWriter []io.Writer
func TestConsoleLocations(t *testing.T) {
var buf, raw low.Buf
w := NewConsoleWriter(&buf, 0)
l := New(MultiWriter{&raw, w})
w.PadEmptyMessage = false
c := loc.Caller(-1)
cc := loc.Callers(-1, 2)
{
name, file, line := c.NameFileLine()
t.Logf("caller: %v %v %v", name, file, line)
for _, pc := range cc {
name, file, line := pc.NameFileLine()
t.Logf("callers: %v %v %v", name, file, line)
}
}
_ = l.Event("caller", c)
assert.Equal(t, "caller=location.go:24\n", string(buf))
buf = buf[:0]
_ = l.Event("callers", cc)
assert.Equal(t, "callers=[location.go:71 console_test.go:26]\n", string(buf))
t.Logf("dump:\n%v", tlwire.Dump(raw))
}
func (w MultiWriter) Write(p []byte) (n int, err error) {
for i, w := range w {
m, e := w.Write(p)
if i == 0 {
n = m
}
if err == nil {
err = e
}
}
return
}
func TestConsole(tb *testing.T) {
var e tlwire.Encoder
var b, jb low.Buf
j := NewConsoleWriter(&jb, 0)
b = e.AppendInt(b[:0], 5)
jb, _ = j.ConvertValue(jb[:0], b, 0, 0)
assert.Equal(tb, low.Buf("5"), jb)
b = e.AppendInt(b[:0], -5)
jb, _ = j.ConvertValue(jb[:0], b, 0, 0)
assert.Equal(tb, low.Buf("-5"), jb)
}
func TestAppendDuration(t *testing.T) {
w := NewConsoleWriter(nil, 0)
t.Logf("%-10s", w.AppendDuration(nil, 0))
for _, days := range []int{0, 2} {
for _, h := range []int{0, 12} {
for _, m := range []int{0, 2} {
for _, s := range []int{0, 36} {
d := time.Hour*time.Duration(24*days+h) +
time.Minute*time.Duration(m) +
time.Second*time.Duration(s)
t.Logf("%-10s is %v", w.AppendDuration(nil, d), d)
}
}
}
}
for d := time.Nanosecond; d < 100*time.Second; d *= 7 {
t.Logf("%-10s is %v", w.AppendDuration(nil, d), d)
}
for f := float64(1); f < float64(200*time.Microsecond); f *= 1.378 {
d := time.Duration(f)
t.Logf("%-10s is %v", w.AppendDuration(nil, d), d)
}
d := 99999 * time.Microsecond
t.Logf("%-10s is %v", w.AppendDuration(nil, d), d)
d = 999999 * time.Microsecond
t.Logf("%-10s is %v", w.AppendDuration(nil, d), d)
}
func BenchmarkConsolePrintw(b *testing.B) {
b.ReportAllocs()
w := NewConsoleWriter(io.Discard, LdetFlags)
l := New(w)
for i := 0; i < b.N; i++ {
l.Printw("message", "a", i+1000, "b", i+1000, "c", "str")
}
}
func BenchmarkConsoleStartPrintwFinish(b *testing.B) {
b.ReportAllocs()
w := NewConsoleWriter(io.Discard, LdetFlags)
l := New(w)
for i := 0; i < b.N; i++ {
tr := l.Start("span_name")
tr.Printw("message", "a", i+1000, "b", i+1000, "c", "str")
tr.Finish()
}
}