How to Measure How Many Results App Can Produce Per Second

71 Views Asked by At

I have one c++ app called IDCreator which takes two arguments for it to produce IDs. It prints its return value to the STDOUT ---- using printf ---- from where the Calling process fetches.

Now, I wonder how many IDs the IDCreator is able to produce per second (must be done outside this application), how can i achieve this? Is below code proper for doing such a job? there any other way?

string getid;
getid.append("./IDCreator arg1 arg2");

int count = 0;    
const int PERIOD = 100;

const int LEN = 512;
char buff[LEN] = {0};

time_t tick1 = time(NULL);
while(1)
{
     time_t tick2 = time(NULL);
     if ((tick2 - tick1) > PERIOD)
         break;

     FILE* res = popen(cmd.c_str(), "r");
     if (res)
     {
           fgets(res, buff, sizeof(buf));
           pclose(res);

           count++;
     }
}

printf("Products Per Second is: %d", count/PERIOD);
2

There are 2 best solutions below

0
On BEST ANSWER

Instead of creating a function which will indepedently measure the function time and doing that fancy/complex stuff, I would like to suggest a simple way.

  1. Add logger/timers at the start and end of your id generation module like,

    Do something

    logger.info(time)

    { id generation }

    logger.info(time)

    Do something

Loggers are for example purpose(generally easiest way to achieve timestamps) This will help you find time required to generate one id Based on this I hope time is in millis/micros (if its in micro then you need to use timers with microsecond granularity).

If you need x milliseconds to generate 1 id then you will generate round(1000/x) id's in a second. It's a simple equation.

  1. You can add loggers at the start and end of program and limit program to execute only for fix time duration (use logical breaking using timers or manual stop of program)

if in x seconds program generates y ids then, in 1 seconds program generates y/x ids

run the program 2-3 times for enough long duration to have average and accuracy to get throughput of your program.

I hope I was helpful.

1
On

Run the code with a profiler like gprof and with it you can measure the performance of each function and draw conclusions bases on it.

Here is one of the sample output of gprof,

  %   cumulative   self              self     total           
 time   seconds   seconds    calls  ms/call  ms/call  name    
  17.7       3.72     3.72 13786208     0.00     0.00  Ns_DStringNAppend [8]
  6.1       5.00     1.28   107276     0.01     0.03  MakePath [10]
  2.9       5.60     0.60  1555972     0.00     0.00  Ns_DStringFree [35]
  2.7       6.18     0.58  1555965     0.00     0.00  Ns_DStringInit [36]
  2.3       6.67     0.49  1507858     0.00     0.00  ns_realloc [40]

In the above example , row 1 by itself has run for 3.72 seconds and had made around 13786208. So if this function alone has been run in a loop for one second, would have been called 13786208/3.72

The example is from this link