zlog/diode/diode_test.go

63 lines
1.2 KiB
Go
Raw Permalink Normal View History

package diode_test
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"testing"
"time"
2022-11-03 15:18:09 +00:00
"tuxpa.in/a/zlog"
"tuxpa.in/a/zlog/diode"
"tuxpa.in/a/zlog/internal/cbor"
)
func TestNewWriter(t *testing.T) {
buf := bytes.Buffer{}
w := diode.NewWriter(&buf, 1000, 0, func(missed int) {
fmt.Printf("Dropped %d messages\n", missed)
})
2022-03-20 19:19:42 +00:00
log := zlog.New(w)
log.Print("test")
w.Close()
want := "{\"level\":\"debug\",\"message\":\"test\"}\n"
got := cbor.DecodeIfBinaryToString(buf.Bytes())
if got != want {
t.Errorf("Diode New Writer Test failed. got:%s, want:%s!", got, want)
}
}
func TestClose(t *testing.T) {
buf := bytes.Buffer{}
w := diode.NewWriter(&buf, 1000, 0, func(missed int) {})
2022-12-28 09:41:00 +00:00
log := zlog.New(w)
log.Print("test")
w.Close()
}
func Benchmark(b *testing.B) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stderr)
benchs := map[string]time.Duration{
"Waiter": 0,
"Pooler": 10 * time.Millisecond,
}
for name, interval := range benchs {
b.Run(name, func(b *testing.B) {
w := diode.NewWriter(ioutil.Discard, 100000, interval, nil)
2022-03-20 19:19:42 +00:00
log := zlog.New(w)
defer w.Close()
b.SetParallelism(1000)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
log.Print("test")
}
})
})
}
}