Debian Codegraph @

pkg/color/color_bench_test.go

raw
1 package color
2
3 import (
4 "testing"
5 )
6
7 // BenchmarkNew benchmarks the creation of new Color instances.
8 func BenchmarkNew(b *testing.B) {
9 for b.Loop() {
10 _ = New(FgRed, Bold)
11 }
12 }
13
14 // BenchmarkColor_Add benchmarks the Add method for chaining SGR parameters.
15 func BenchmarkColor_Add(b *testing.B) {
16 c := New()
17 for b.Loop() {
18 _ = c.Add(FgRed, Bold)
19 }
20 }
21
22 // BenchmarkColor_Sprint benchmarks the Sprint method for converting colored strings.
23 func BenchmarkColor_Sprint(b *testing.B) {
24 cfg := &Config{
25 NoColor: false,
26 Output: nil,
27 Error: nil,
28 }
29
30 c := NewWithConfig(cfg, FgRed)
31 for b.Loop() {
32 _ = c.Sprint("hello")
33 }
34 }
35
36 // BenchmarkColor_Sprintf benchmarks the Sprintf method for formatted colored strings.
37 func BenchmarkColor_Sprintf(b *testing.B) {
38 cfg := &Config{
39 NoColor: false,
40 Output: nil,
41 Error: nil,
42 }
43
44 c := NewWithConfig(cfg, FgRed)
45 for b.Loop() {
46 _ = c.Sprintf("value: %d", 42)
47 }
48 }
49
50 // BenchmarkColor_sequence benchmarks the sequence method for generating ANSI codes.
51 func BenchmarkColor_sequence(b *testing.B) {
52 c := New(FgRed, Bold, Underline)
53 for b.Loop() {
54 _ = c.sequence()
55 }
56 }
57
58 // BenchmarkColor_format benchmarks the format method for generating full ANSI escape sequences.
59 func BenchmarkColor_format(b *testing.B) {
60 cfg := &Config{
61 NoColor: false,
62 Output: nil,
63 Error: nil,
64 }
65
66 c := NewWithConfig(cfg, FgRed, Bold)
67 for b.Loop() {
68 _ = c.format()
69 }
70 }