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);
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.
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.
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.