aboutsummaryrefslogtreecommitdiff
path: root/formatter_bench_test.go
blob: 2a6d3cdb151570300abbd20e761b53496ff58a83 (plain) (blame)
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
47
48
49
package main

import (
	"strings"
	"testing"
)

func BenchmarkFormat_Small(benchmarkRunner *testing.B) {
	inputSource := []byte(`package main
func main() {
	x := 1
	y := 2
	if x > 0 {
		z := 3
	}
	a := 4
}
`)
	formatter := &Formatter{CommentMode: CommentsFollow}

	for benchmarkRunner.Loop() {
		_, _ = formatter.Format(inputSource)
	}
}

func BenchmarkFormat_Large(benchmarkRunner *testing.B) {
	var sourceBuilder strings.Builder

	sourceBuilder.WriteString("package main\n\n")

	for functionIndex := range 100 {
		sourceBuilder.WriteString("func foo")
		sourceBuilder.WriteString(string(rune('A' + functionIndex%26)))
		sourceBuilder.WriteString("() {\n")
		sourceBuilder.WriteString("\tx := 1\n")
		sourceBuilder.WriteString("\tif x > 0 {\n")
		sourceBuilder.WriteString("\t\ty := 2\n")
		sourceBuilder.WriteString("\t}\n")
		sourceBuilder.WriteString("\tz := 3\n")
		sourceBuilder.WriteString("}\n\n")
	}

	inputSource := []byte(sourceBuilder.String())
	formatter := &Formatter{CommentMode: CommentsFollow}

	for benchmarkRunner.Loop() {
		_, _ = formatter.Format(inputSource)
	}
}