LCOV - code coverage report
Current view: top level - app/test - test_rand_perf.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 1 15 6.7 %
Date: 2025-03-01 20:23:48 Functions: 1 2 50.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 8 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2019 Ericsson AB
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <inttypes.h>
       6                 :            : #include <stdio.h>
       7                 :            : 
       8                 :            : #include <rte_common.h>
       9                 :            : #include <rte_cycles.h>
      10                 :            : #include <rte_random.h>
      11                 :            : 
      12                 :            : #include "test.h"
      13                 :            : 
      14                 :            : static volatile uint64_t vsum;
      15                 :            : 
      16                 :            : #define ITERATIONS (100000000)
      17                 :            : 
      18                 :            : #define BEST_CASE_BOUND (1<<16)
      19                 :            : #define WORST_CASE_BOUND (BEST_CASE_BOUND + 1)
      20                 :            : 
      21                 :            : enum rand_type {
      22                 :            :         rand_type_64,
      23                 :            :         rand_type_float,
      24                 :            :         rand_type_bounded_best_case,
      25                 :            :         rand_type_bounded_worst_case
      26                 :            : };
      27                 :            : 
      28                 :            : static const char *
      29                 :            : rand_type_desc(enum rand_type rand_type)
      30                 :            : {
      31                 :            :         switch (rand_type) {
      32                 :            :         case rand_type_64:
      33                 :            :                 return "Full 64-bit [rte_rand()]";
      34                 :            :         case rand_type_float:
      35                 :            :                 return "Floating point [rte_drand()]";
      36                 :            :         case rand_type_bounded_best_case:
      37                 :            :                 return "Bounded average best-case [rte_rand_max()]";
      38                 :            :         case rand_type_bounded_worst_case:
      39                 :            :                 return "Bounded average worst-case [rte_rand_max()]";
      40                 :            :         default:
      41                 :            :                 return NULL;
      42                 :            :         }
      43                 :            : }
      44                 :            : 
      45                 :            : static __rte_always_inline void
      46                 :            : test_rand_perf_type(enum rand_type rand_type)
      47                 :            : {
      48                 :            :         uint64_t start;
      49                 :            :         uint32_t i;
      50                 :            :         uint64_t end;
      51                 :            :         uint64_t sum = 0;
      52                 :            :         uint64_t op_latency;
      53                 :            : 
      54                 :            :         start = rte_rdtsc();
      55                 :            : 
      56   [ #  #  #  #  :          0 :         for (i = 0; i < ITERATIONS; i++) {
             #  #  #  # ]
      57                 :            :                 switch (rand_type) {
      58                 :          0 :                 case rand_type_64:
      59                 :          0 :                         sum += rte_rand();
      60                 :            :                         break;
      61                 :          0 :                 case rand_type_float:
      62                 :          0 :                         sum += 1000. * rte_drand();
      63                 :            :                         break;
      64                 :          0 :                 case rand_type_bounded_best_case:
      65                 :          0 :                         sum += rte_rand_max(BEST_CASE_BOUND);
      66                 :            :                         break;
      67                 :          0 :                 case rand_type_bounded_worst_case:
      68                 :          0 :                         sum += rte_rand_max(WORST_CASE_BOUND);
      69                 :            :                         break;
      70                 :            :                 }
      71                 :            :         }
      72                 :            : 
      73                 :            :         end = rte_rdtsc();
      74                 :            : 
      75                 :            :         /* to avoid an optimizing compiler removing the whole loop */
      76                 :          0 :         vsum = sum;
      77                 :            : 
      78                 :          0 :         op_latency = (end - start) / ITERATIONS;
      79                 :            : 
      80                 :            :         printf("%s: %"PRId64" TSC cycles/op\n", rand_type_desc(rand_type),
      81                 :            :                op_latency);
      82                 :            : }
      83                 :            : 
      84                 :            : static int
      85                 :          0 : test_rand_perf(void)
      86                 :            : {
      87                 :          0 :         rte_srand(42);
      88                 :            : 
      89                 :            :         printf("Pseudo-random number generation latencies:\n");
      90                 :            : 
      91                 :            :         test_rand_perf_type(rand_type_64);
      92                 :            :         test_rand_perf_type(rand_type_float);
      93                 :            :         test_rand_perf_type(rand_type_bounded_best_case);
      94                 :            :         test_rand_perf_type(rand_type_bounded_worst_case);
      95                 :            : 
      96                 :          0 :         return 0;
      97                 :            : }
      98                 :            : 
      99                 :        252 : REGISTER_PERF_TEST(rand_perf_autotest, test_rand_perf);

Generated by: LCOV version 1.14