tlog.app/go/tlog@v0.23.1/convert/json_test.go (about)

     1  package convert
     2  
     3  import (
     4  	"io"
     5  	"regexp"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/nikandfor/hacked/low"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  
    14  	"tlog.app/go/tlog"
    15  	"tlog.app/go/tlog/tlwire"
    16  )
    17  
    18  func TestJSON(tb *testing.T) {
    19  	var e tlwire.Encoder
    20  	var b, jb low.Buf
    21  
    22  	j := NewJSON(&jb)
    23  
    24  	b = e.AppendInt(b[:0], 5)
    25  	jb, _ = j.ConvertValue(jb[:0], b, 0)
    26  	assert.Equal(tb, low.Buf("5"), jb)
    27  
    28  	b = e.AppendInt(b[:0], -5)
    29  	jb, _ = j.ConvertValue(jb[:0], b, 0)
    30  	assert.Equal(tb, low.Buf("-5"), jb)
    31  }
    32  
    33  func TestJSONLogger(t *testing.T) {
    34  	tm := time.Date(2020, time.December, 25, 22, 8, 13, 0, time.FixedZone("Europe/Moscow", int(3*time.Hour/time.Second)))
    35  
    36  	var b low.Buf
    37  
    38  	j := NewJSON(&b)
    39  	j.TimeZone = time.FixedZone("MSK", int(3*time.Hour/time.Second))
    40  	j.TimeFormat = time.RFC3339Nano
    41  
    42  	l := tlog.New(j)
    43  
    44  	tlog.LoggerSetTimeNow(l, func() time.Time { return tm }, tm.UnixNano)
    45  
    46  	l.SetLabels(tlog.ParseLabels("a=b,c")...)
    47  
    48  	// l.Printw("user labels", "", tlog.Labels{"user_label"})
    49  
    50  	l.Printw("message", "str", "arg", "int", 5, "struct", struct {
    51  		A string `json:"a"`
    52  		B int    `tlog:"bb" yaml:"b"`
    53  		C *int   `tlog:"c,omitempty"`
    54  	}{
    55  		A: "A field",
    56  		B: 9,
    57  	})
    58  
    59  	exp := `{"_t":"2020-12-25T22:08:13\+03:00","_c":"[\w./-]*json_test.go:\d+","_m":"message","str":"arg","int":5,"struct":{"a":"A field","bb":9},"a":"b","c":""}
    60  `
    61  
    62  	exps := strings.Split(exp, "\n")
    63  	ls := strings.Split(string(b), "\n")
    64  	for i := 0; i < len(exps); i++ {
    65  		re := regexp.MustCompile("^" + exps[i] + "$")
    66  
    67  		var have string
    68  		if i < len(ls) {
    69  			have = ls[i]
    70  		}
    71  
    72  		assert.True(t, re.MatchString(have), "expected\n%s\ngot\n%s", exps[i], have)
    73  	}
    74  
    75  	for i := len(exps); i < len(ls); i++ {
    76  		assert.True(t, false, "expected\n%s\ngot\n%s", "", ls[i])
    77  	}
    78  }
    79  
    80  func TestJSONRename(t *testing.T) {
    81  	tm := time.Date(2020, time.December, 25, 22, 8, 13, 0, time.FixedZone("Europe/Moscow", int(3*time.Hour/time.Second)))
    82  
    83  	var b low.Buf
    84  
    85  	j := NewJSON(&b)
    86  	j.TimeZone = time.FixedZone("MSK", int(3*time.Hour/time.Second))
    87  	j.TimeFormat = time.RFC3339Nano
    88  
    89  	renamer := simpleTestRenamer()
    90  
    91  	j.Rename = renamer.Rename
    92  
    93  	l := tlog.New(j)
    94  
    95  	tlog.LoggerSetTimeNow(l, func() time.Time { return tm }, tm.UnixNano)
    96  
    97  	l.SetLabels(tlog.ParseLabels("a=b,c")...)
    98  
    99  	//	l.Printw("user labels", "", tlog.Labels{"user_label"})
   100  
   101  	l.Printw("message", "str", "arg", "int", 5, "struct", struct {
   102  		A string `json:"a"`
   103  		B int    `tlog:"bb" yaml:"b"`
   104  		C *int   `tlog:"c,omitempty"`
   105  	}{
   106  		A: "A field",
   107  		B: 9,
   108  	})
   109  
   110  	exp := `{"time":"2020-12-25T22:08:13\+03:00","caller":"[\w./-]*json_test.go:\d+","message":"message","str_key":"arg","int":5,"struct":{"a":"A field","bb":9},"L_a":"b","L_c":""}
   111  `
   112  
   113  	exps := strings.Split(exp, "\n")
   114  	ls := strings.Split(string(b), "\n")
   115  	for i := 0; i < len(exps); i++ {
   116  		re := regexp.MustCompile("^" + exps[i] + "$")
   117  
   118  		var have string
   119  		if i < len(ls) {
   120  			have = ls[i]
   121  		}
   122  
   123  		assert.True(t, re.MatchString(have), "expected\n%s\ngot\n%s", exps[i], have)
   124  	}
   125  
   126  	for i := len(exps); i < len(ls); i++ {
   127  		assert.True(t, false, "expected\n%s\ngot\n%s", "", ls[i])
   128  	}
   129  }
   130  
   131  func BenchmarkJSONConvert(b *testing.B) {
   132  	var buf low.Buf
   133  	var e tlwire.Encoder
   134  
   135  	appendMap := func(b []byte, kvs ...interface{}) []byte {
   136  		b = e.AppendMap(b, -1)
   137  		b = tlog.AppendKVs(b, kvs)
   138  		b = e.AppendBreak(b)
   139  
   140  		return b
   141  	}
   142  
   143  	buf = appendMap(buf, tlog.KeyTimestamp, 10000000000, tlog.KeyEventKind, tlog.EventSpanStart, "a", "b", "c", "d", "e", "d", "h", "g")
   144  	buf = appendMap(buf, tlog.KeySpan, tlog.ID{}, tlog.KeyTimestamp, 10000000000, tlog.KeyMessage, "message text", "arg", "value", "arg2", 5)
   145  
   146  	var d tlwire.Decoder
   147  
   148  	st := d.Skip(buf, 0)
   149  
   150  	w := NewJSON(io.Discard)
   151  
   152  	_, err := w.Write(buf[:st])
   153  	require.NoError(b, err)
   154  
   155  	_, err = w.Write(buf[st:])
   156  	require.NoError(b, err)
   157  
   158  	b.ResetTimer()
   159  	b.ReportAllocs()
   160  
   161  	for i := 0; i < b.N; i++ {
   162  		_, _ = w.Write(buf[st:])
   163  	}
   164  }
   165  
   166  func BenchmarkJSONPrintw(b *testing.B) {
   167  	w := NewJSON(io.Discard)
   168  	l := tlog.New(w)
   169  	//	l.NoCaller = true
   170  	//	l.NoTime = true
   171  
   172  	l.SetLabels("a", "b", "c", "d", "e", "f", "g", "h")
   173  
   174  	b.ResetTimer()
   175  	b.ReportAllocs()
   176  
   177  	for i := 0; i < b.N; i++ {
   178  		l.Printw("message", "a", i+1000, "b", i+1000)
   179  	}
   180  }