1
1
package ebpf
2
2
3
3
import (
4
- "errors"
5
4
"fmt"
6
5
"os"
7
6
"reflect"
@@ -404,56 +403,40 @@ func BenchmarkScanFdInfoReader(b *testing.B) {
404
403
}
405
404
}
406
405
407
- // TestStats loads a BPF program once and executes back-to-back test runs
406
+ // TestProgramStats loads a BPF program once and executes back-to-back test runs
408
407
// of the program. See testStats for details.
409
- func TestStats (t * testing.T ) {
408
+ func TestProgramStats (t * testing.T ) {
410
409
testutils .SkipOnOldKernel (t , "5.8" , "BPF_ENABLE_STATS" )
411
410
412
411
prog := createBasicProgram (t )
413
412
414
- pi , err := prog .Info ()
415
- if err != nil {
416
- t .Errorf ("failed to get ProgramInfo: %v" , err )
417
- }
418
-
419
- rc , ok := pi .RunCount ()
420
- if ! ok {
421
- t .Errorf ("expected run count info to be available" )
422
- }
423
- if rc != 0 {
424
- t .Errorf ("expected a run count of 0 but got %d" , rc )
425
- }
413
+ s , err := prog .Stats ()
414
+ qt .Assert (t , qt .IsNil (err ))
426
415
427
- rt , ok := pi .Runtime ()
428
- if ! ok {
429
- t .Errorf ("expected runtime info to be available" )
430
- }
431
- if runtime .GOARCH != "arm64" && rt != 0 {
432
- t .Errorf ("expected a runtime of 0ns but got %v" , rt )
433
- }
416
+ qt .Assert (t , qt .Equals (s .RunCount , 0 ))
417
+ qt .Assert (t , qt .Equals (s .RecursionMisses , 0 ))
434
418
435
- rm , ok := pi .RecursionMisses ()
436
- if ! ok {
437
- t .Errorf ("expected recursion misses info to be available" )
438
- }
439
- if rm != 0 {
440
- t .Errorf ("expected a recursion misses of 0 but got %v" , rm )
419
+ if runtime .GOARCH != "arm64" {
420
+ // Runtime is flaky on arm64.
421
+ qt .Assert (t , qt .Equals (s .Runtime , 0 ))
441
422
}
442
423
443
- if err := testStats (prog ); err != nil {
424
+ if err := testStats (t , prog ); err != nil {
444
425
testutils .SkipIfNotSupportedOnOS (t , err )
445
426
t .Error (err )
446
427
}
447
428
}
448
429
449
430
// BenchmarkStats is a benchmark of TestStats. See testStats for details.
450
431
func BenchmarkStats (b * testing.B ) {
432
+ b .ReportAllocs ()
433
+
451
434
testutils .SkipOnOldKernel (b , "5.8" , "BPF_ENABLE_STATS" )
452
435
453
436
prog := createBasicProgram (b )
454
437
455
438
for n := 0 ; n < b .N ; n ++ {
456
- if err := testStats (prog ); err != nil {
439
+ if err := testStats (b , prog ); err != nil {
457
440
testutils .SkipIfNotSupportedOnOS (b , err )
458
441
b .Fatal (fmt .Errorf ("iter %d: %w" , n , err ))
459
442
}
@@ -470,7 +453,9 @@ func BenchmarkStats(b *testing.B) {
470
453
// resulting in RunCount incrementing by more than one. Expecting RunCount to
471
454
// be of a specific value after a call to Test() is therefore not possible.
472
455
// See https://golang.org/doc/go1.14#runtime for more details.
473
- func testStats (prog * Program ) error {
456
+ func testStats (tb testing.TB , prog * Program ) error {
457
+ tb .Helper ()
458
+
474
459
in := internal .EmptyBPFContext
475
460
476
461
stats , err := EnableStats (uint32 (sys .BPF_STATS_RUN_TIME ))
@@ -485,61 +470,25 @@ func testStats(prog *Program) error {
485
470
return fmt .Errorf ("failed to trigger program: %w" , err )
486
471
}
487
472
488
- pi , err := prog .Info ()
489
- if err != nil {
490
- return fmt .Errorf ("failed to get ProgramInfo: %w" , err )
491
- }
473
+ s1 , err := prog .Stats ()
474
+ qt .Assert (tb , qt .IsNil (err ))
492
475
493
- rc , ok := pi .RunCount ()
494
- if ! ok {
495
- return errors .New ("expected run count info to be available" )
496
- }
497
- if rc < 1 {
498
- return fmt .Errorf ("expected a run count of at least 1 but got %d" , rc )
499
- }
500
- // Store the run count for the next invocation.
501
- lc := rc
476
+ qt .Assert (tb , qt .Not (qt .Equals (s1 .RunCount , 0 )), qt .Commentf ("expected run count to be at least 1 after first invocation" ))
477
+ qt .Assert (tb , qt .Not (qt .Equals (s1 .Runtime , 0 )), qt .Commentf ("expected runtime to be at least 1ns after first invocation" ))
502
478
503
- rt , ok := pi .Runtime ()
504
- if ! ok {
505
- return errors .New ("expected runtime info to be available" )
506
- }
507
- if rt == 0 {
508
- return errors .New ("expected a runtime other than 0ns" )
509
- }
510
- // Store the runtime value for the next invocation.
511
- lt := rt
512
-
513
- if err := stats .Close (); err != nil {
514
- return fmt .Errorf ("failed to disable statistics: %w" , err )
515
- }
479
+ qt .Assert (tb , qt .IsNil (stats .Close ()))
516
480
517
481
// Second program execution, with runtime statistics gathering disabled.
518
482
// Total runtime and run counters are not expected to increase.
519
483
if _ , _ , err := prog .Test (in ); err != nil {
520
484
return fmt .Errorf ("failed to trigger program: %w" , err )
521
485
}
522
486
523
- pi , err = prog .Info ()
524
- if err != nil {
525
- return fmt .Errorf ("failed to get ProgramInfo: %w" , err )
526
- }
527
-
528
- rc , ok = pi .RunCount ()
529
- if ! ok {
530
- return errors .New ("expected run count info to be available" )
531
- }
532
- if rc != lc {
533
- return fmt .Errorf ("run count unexpectedly increased over previous value (current: %v, prev: %v)" , rc , lc )
534
- }
487
+ s2 , err := prog .Stats ()
488
+ qt .Assert (tb , qt .IsNil (err ))
535
489
536
- rt , ok = pi .Runtime ()
537
- if ! ok {
538
- return errors .New ("expected runtime info to be available" )
539
- }
540
- if rt != lt {
541
- return fmt .Errorf ("runtime unexpectedly increased over the previous value (current: %v, prev: %v)" , rt , lt )
542
- }
490
+ qt .Assert (tb , qt .Equals (s2 .RunCount , s1 .RunCount ), qt .Commentf ("run count (%d) increased after first invocation (%d)" , s2 .RunCount , s1 .RunCount ))
491
+ qt .Assert (tb , qt .Equals (s2 .Runtime , s1 .Runtime ), qt .Commentf ("runtime (%d) increased after first invocation (%d)" , s2 .Runtime , s1 .Runtime ))
543
492
544
493
return nil
545
494
}
0 commit comments