Skip to content

Commit 1a81899

Browse files
committed
first bench tests for currentX() that uses TLS which is slow
1 parent 8d57640 commit 1a81899

File tree

2 files changed

+71
-10
lines changed

2 files changed

+71
-10
lines changed

assert/assert.go

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,17 @@ func init() {
137137
}
138138

139139
type (
140+
mapAsserter = map[int]asserter
141+
140142
testersMap = map[int]testing.TB
141143
function = func()
142144
)
143145

144146
var (
145147
// testers must be set if assertion package is used for the unit testing.
146148
testers = x.NewRWMap[testersMap]()
149+
150+
asserterMap = x.NewRWMap[mapAsserter]()
147151
)
148152

149153
const (
@@ -286,6 +290,38 @@ func ThatNot(term bool, a ...any) {
286290
}
287291
}
288292

293+
func ThatX(term bool, a ...any) {
294+
if !term {
295+
thatXDo(a)
296+
}
297+
}
298+
299+
func ZeroX[T Number](val T, a ...any) {
300+
if val != 0 {
301+
doZeroX(val, a)
302+
}
303+
}
304+
305+
func doZeroX[T Number](val T, a []any) {
306+
defMsg := fmt.Sprintf(assertionMsg+": got '%v', want (== '0')", val)
307+
currentX().reportAssertionFault(1, defMsg, a)
308+
}
309+
310+
func thatXDo(a []any) {
311+
defMsg := assertionMsg
312+
currentX().reportAssertionFault(1, defMsg, a)
313+
}
314+
315+
func currentX() asserter {
316+
// we need thread local storage, maybe we'll implement that to x.package?
317+
// study `tester` and copy ideas from it.
318+
return asserterMap.Get(goid())
319+
}
320+
321+
func SetDefaultX(i defInd) {
322+
asserterMap.Set(goid(), defAsserter[i])
323+
}
324+
289325
// That asserts that the term is true. If not it panics with the given
290326
// formatting string. Thanks to inlining, the performance penalty is equal to a
291327
// single 'if-statement' that is almost nothing.
@@ -884,11 +920,26 @@ func NoError(err error, a ...any) {
884920
// are used to override the auto-generated assert violation message.
885921
func Error(err error, a ...any) {
886922
if err == nil {
887-
defMsg := "Error:" + assertionMsg + ": missing error"
888-
current().reportAssertionFault(0, defMsg, a)
923+
doErrorX(a)
924+
}
925+
}
926+
927+
func ErrorX(err error, a ...any) {
928+
if err == nil {
929+
doError(a)
889930
}
890931
}
891932

933+
func doErrorX(a []any) {
934+
defMsg := "Error:" + assertionMsg + ": missing error"
935+
currentX().reportAssertionFault(0, defMsg, a)
936+
}
937+
938+
func doError(a []any) {
939+
defMsg := "Error:" + assertionMsg + ": missing error"
940+
current().reportAssertionFault(0, defMsg, a)
941+
}
942+
892943
// Greater asserts that the value is greater than want. If it is not it panics
893944
// and builds a violation message. Thanks to inlining, the performance penalty
894945
// is equal to a single 'if-statement' that is almost nothing.

assert/assert_test.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -401,27 +401,37 @@ func BenchmarkThat(b *testing.B) {
401401
}
402402
}
403403

404-
func BenchmarkGreater(b *testing.B) {
404+
func BenchmarkZeroX(b *testing.B) {
405+
assert.SetDefaultX(assert.Production)
406+
const zero = 0
405407
for n := 0; n < b.N; n++ {
406-
assert.Greater(1, 0)
408+
assert.ZeroX(zero)
407409
}
408410
}
409411

410-
func BenchmarkLess(b *testing.B) {
412+
func BenchmarkZero(b *testing.B) {
413+
const zero = 0
411414
for n := 0; n < b.N; n++ {
412-
assert.Less(0, 1)
415+
assert.Zero(zero)
413416
}
414417
}
415418

416-
func BenchmarkZero(b *testing.B) {
419+
func BenchmarkGreater(b *testing.B) {
417420
for n := 0; n < b.N; n++ {
418-
assert.Zero(0)
421+
assert.Greater(1, 0)
422+
}
423+
}
424+
425+
func BenchmarkLess(b *testing.B) {
426+
for n := 0; n < b.N; n++ {
427+
assert.Less(0, 1)
419428
}
420429
}
421430

422-
func BenchmarkNotZero(b *testing.B) {
431+
func BenchmarkErrorX(b *testing.B) {
432+
assert.SetDefaultX(assert.Production)
423433
for n := 0; n < b.N; n++ {
424-
assert.NotZero(n + 1)
434+
assert.ErrorX(err2.ErrNotAccess)
425435
}
426436
}
427437

0 commit comments

Comments
 (0)