@@ -283,3 +283,45 @@ func TestEncoderToValidUTF8(t *testing.T) {
283283 assert .Equal (t , "a����z" , toValidUtf8 ([]byte ("a\xf0 \x8f \xbf \xbf z" )))
284284 assert .Equal (t , "世界����z 世界" , toValidUtf8 ([]byte ("世界\xf0 \x8f \xbf \xbf z 世界" )))
285285}
286+
287+ func BenchmarkJSONEncoder_Encode (b * testing.B ) {
288+ logsConfig := & config.LogsConfig {
289+ Service : "Service" ,
290+ Source : "Source" ,
291+ SourceCategory : "SourceCategory" ,
292+ Tags : []string {"foo:bar" , "baz" },
293+ }
294+ source := sources .NewLogSource ("" , logsConfig )
295+
296+ b .Run ("valid" , func (b * testing.B ) {
297+ content := []byte (strings .Repeat ("x" , 100 ))
298+ var msg * message.Message
299+
300+ b .ResetTimer ()
301+ b .ReportAllocs ()
302+ for range b .N {
303+ msg = newMessage (content , source , message .StatusError )
304+ msg .State = message .StateRendered // we can only encode rendered message
305+ msg .Origin .LogSource = source
306+ msg .Origin .SetTags ([]string {"a" , "b:c" })
307+
308+ assert .Nil (b , JSONEncoder .Encode (msg , "unknown" ))
309+ }
310+ })
311+
312+ b .Run ("invalid" , func (b * testing.B ) {
313+ content := []byte (strings .Repeat ("x" , 100 ) + "\uFFFD " )
314+ var msg * message.Message
315+
316+ b .ResetTimer ()
317+ b .ReportAllocs ()
318+ for range b .N {
319+ msg = newMessage (content , source , message .StatusError )
320+ msg .State = message .StateRendered // we can only encode rendered message
321+ msg .Origin .LogSource = source
322+ msg .Origin .SetTags ([]string {"a" , "b:c" })
323+
324+ assert .Nil (b , JSONEncoder .Encode (msg , "unknown" ))
325+ }
326+ })
327+ }
0 commit comments