File: jsonstring_timing_test.go

package info (click to toggle)
golang-github-valyala-quicktemplate 1.8.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 492 kB
  • sloc: makefile: 16; xml: 15
file content (46 lines) | stat: -rw-r--r-- 1,217 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package quicktemplate

import (
	"strconv"
	"testing"
)

func BenchmarkAppendJSONString(b *testing.B) {
	b.Run("no-special-chars", func(b *testing.B) {
		benchmarkAppendJSONString(b, "foo bar baz abc defkjlkj lkjdfs klsdjflfdjoqjo lkj ss")
	})
	b.Run("with-special-chars", func(b *testing.B) {
		benchmarkAppendJSONString(b, `foo bar baz abc defkjlkj lkjdf" klsdjflfdjoqjo\lkj ss`)
	})
}

func benchmarkAppendJSONString(b *testing.B, s string) {
	b.ReportAllocs()
	b.SetBytes(int64(len(s)))
	b.RunParallel(func(pb *testing.PB) {
		var buf []byte
		for pb.Next() {
			buf = AppendJSONString(buf[:0], s, true)
		}
	})
}

func BenchmarkAppendJSONStringViaStrconv(b *testing.B) {
	b.Run("no-special-chars", func(b *testing.B) {
		benchmarkAppendJSONStringViaStrconv(b, "foo bar baz abc defkjlkj lkjdfs klsdjflfdjoqjo lkj ss")
	})
	b.Run("with-special-chars", func(b *testing.B) {
		benchmarkAppendJSONStringViaStrconv(b, `foo bar baz abc defkjlkj lkjdf" klsdjflfdjoqjo\lkj ss`)
	})
}

func benchmarkAppendJSONStringViaStrconv(b *testing.B, s string) {
	b.ReportAllocs()
	b.SetBytes(int64(len(s)))
	b.RunParallel(func(pb *testing.PB) {
		var buf []byte
		for pb.Next() {
			buf = strconv.AppendQuote(buf[:0], s)
		}
	})
}