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