2018-02-20 09:52:12 +00:00
|
|
|
package diode_test
|
|
|
|
|
|
|
|
import (
|
2018-03-28 18:49:41 +00:00
|
|
|
"bytes"
|
2018-02-20 09:52:12 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-03-20 19:19:42 +00:00
|
|
|
"git.tuxpa.in/a/zlog"
|
|
|
|
"git.tuxpa.in/a/zlog/diode"
|
|
|
|
"git.tuxpa.in/a/zlog/internal/cbor"
|
2018-02-20 09:52:12 +00:00
|
|
|
)
|
|
|
|
|
2018-03-28 18:49:41 +00:00
|
|
|
func TestNewWriter(t *testing.T) {
|
|
|
|
buf := bytes.Buffer{}
|
2018-10-31 23:57:15 +00:00
|
|
|
w := diode.NewWriter(&buf, 1000, 0, func(missed int) {
|
2018-05-25 01:50:57 +00:00
|
|
|
fmt.Printf("Dropped %d messages\n", missed)
|
|
|
|
})
|
2022-03-20 19:19:42 +00:00
|
|
|
log := zlog.New(w)
|
2018-02-20 09:52:12 +00:00
|
|
|
log.Print("test")
|
|
|
|
|
|
|
|
w.Close()
|
2018-03-28 18:49:41 +00:00
|
|
|
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)
|
|
|
|
}
|
2018-02-20 09:52:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Benchmark(b *testing.B) {
|
|
|
|
log.SetOutput(ioutil.Discard)
|
|
|
|
defer log.SetOutput(os.Stderr)
|
2018-10-31 23:57:15 +00:00
|
|
|
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)
|
2018-10-31 23:57:15 +00:00
|
|
|
defer w.Close()
|
2018-02-20 09:52:12 +00:00
|
|
|
|
2018-10-31 23:57:15 +00:00
|
|
|
b.SetParallelism(1000)
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
log.Print("test")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2018-02-20 09:52:12 +00:00
|
|
|
}
|