forked from inaneverb/ekacore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_private_bench_test.go
More file actions
124 lines (102 loc) · 2.15 KB
/
Copy patherror_private_bench_test.go
File metadata and controls
124 lines (102 loc) · 2.15 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright © 2021. All rights reserved.
// Author: Ilya Stroy.
// Contacts: qioalice@gmail.com, https://github.com/qioalice
// License: https://opensource.org/licenses/MIT
package ekaerr
import (
"fmt"
"io/ioutil"
"runtime"
"testing"
"github.com/qioalice/ekago/v2/ekalog"
)
type T struct{}
func (T) String() string {
return "stringer"
}
func foo() *Error {
return foo1().
AddMessage("foo bad").
AddFields("foo_arg", T{}).
Throw()
}
func foo1() *Error {
return foo2().
AddMessage("foo1 bad").
AddFields("foo1_arg", 23).
Throw()
}
func foo2() *Error {
return foo3().
AddMessage("foo2 bad").
AddFields("foo2_arg?", "").
Throw()
}
func foo3() *Error {
return IllegalState.
New("what??", "arg1?", nil).
Throw()
}
func TestError(t *testing.T) {
foo().LogAsWarn()
}
func BenchmarkErrorAllocate(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = allocError()
}
}
func BenchmarkErrorAllocateAndRelease(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
releaseError(allocError().(*Error))
}
}
func BenchmarkErrorAcquire(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = acquireError()
}
}
func BenchmarkErrorAcquireAndRelease(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
releaseError(acquireError())
}
}
//goland:noinspection GoSnakeCaseUsage
const (
BENCHMARK_PRINT_ERROR_POOL_INFO = false
)
func benchError(b *testing.B) {
b.ReportAllocs()
devNullJSONIntegrator := new(ekalog.CommonIntegrator).
WithEncoder(new(ekalog.CI_JSONEncoder).FreezeAndGetEncoder()).
WithMinLevel(ekalog.LEVEL_DEBUG).
WriteTo(ioutil.Discard)
ekalog.ReplaceIntegrator(devNullJSONIntegrator)
b.ResetTimer()
for i := 0; i < b.N; i++ {
foo().LogAsWarn()
}
b.StopTimer()
if BENCHMARK_PRINT_ERROR_POOL_INFO {
defer func() {
runtime.GC()
fmt.Printf("EKAERR: %+v\n", EPS())
fmt.Printf("EKALOG: %+v\n", ekalog.EPS())
fmt.Println()
}()
}
}
func BenchmarkErrorGetStackTraceVer1(b *testing.B) {
benchError(b)
}
func BenchmarkErrorGetStackTraceVer2(b *testing.B) {
_USE_GET_STACKTRACE_VER2 = true
benchError(b)
}