Back to index

nordugrid-arc-nox  1.1.0~rc6
Functions | Variables
perftest_cmd_times.cpp File Reference
#include <unistd.h>
#include <errno.h>
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <glibmm/thread.h>
#include <glibmm/timer.h>
#include <vector>
#include <sys/wait.h>

Go to the source code of this file.

Functions

int Round (double x)
void execCommand ()
int main (int argc, char *argv[])

Variables

Glib::Mutex * mutex
int finishedProcesses
unsigned long completedCommands
unsigned long failedCommands
unsigned long totalCommands
Glib::TimeVal completedTime
Glib::TimeVal failedTime
Glib::TimeVal totalTime
int numberOfProcesses
std::string cmd_str
std::vector< std::string > arglist

Function Documentation

void execCommand ( )

Definition at line 43 of file perftest_cmd_times.cpp.

                   {
  // Some variables...
  Glib::TimeVal tBefore;
  Glib::TimeVal tAfter;
  char **list;
  int pid;

  list = (char **)malloc (sizeof(char *) * (arglist.size() +1));
  for (int i = 0;i < arglist.size();i++)
    list[i] = (char *)arglist[i].c_str();
  list[arglist.size()] = NULL;

  for (int i=0; i<numberOfProcesses; i++) {

    tBefore.assign_current_time();

#ifdef WIN32
    STARTUPINFO si = {0};
    si.cb = sizeof(si);
    PROCESS_INFORMATION pi;
    if(CreateProcess(cmd_str.c_str(),NULL,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi
)) {
      std::cout << "Create process failed: "<<GetLastError()<<std::endl;
      exit(1);
    };

    int status;
    status = WaitForSingleObject(pi.hProcess, INFINITE);
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);

    tAfter.assign_current_time();

    if (status != WAIT_OBJECT_0 && status != WAIT_ABANDONED) {
      std::cout << "ERROR: " << cmd_str << " returns code " << status
 << std::endl;
      Glib::Mutex::Lock lock(*mutex);
      ::failedCommands++;
      ::failedTime+=tAfter-tBefore;
      finishedProcesses++;
    }
    else {
      Glib::Mutex::Lock lock(*mutex);
      ::completedCommands++;
      ::completedTime+=tAfter-tBefore;
      finishedProcesses++;
    }
#else
    pid = fork();
    if(pid == 0) {
      int e = execvp(cmd_str.c_str(), list);
      //If execvp returns, it must have failed.
      std::cout << "[child] error " << e << " errno: " << errno << std::endl;
      exit(1);
    }
    else if(pid == -1) {
      std::cout << "Fork failed. Exiting." << std::endl;
      exit(1);
    }
    else {
      int child_status, child_pid;
      child_pid = wait(&child_status);

      tAfter.assign_current_time();
      if(child_status != 0) {
        std::cout << "ERROR: " << cmd_str << " returns code " << child_status << std::endl;
        Glib::Mutex::Lock lock(*mutex);
        ::failedCommands++;
        ::failedTime+=tAfter-tBefore;
        finishedProcesses++;
      }
      else {
        Glib::Mutex::Lock lock(*mutex);
        ::completedCommands++;
        ::completedTime+=tAfter-tBefore;
        finishedProcesses++;
      }
    }
#endif
  } 

  std::cout << "Number of finished processes: " << finishedProcesses << std::endl;

  free(list);

}
int main ( int  argc,
char *  argv[] 
)

Definition at line 130 of file perftest_cmd_times.cpp.

                                {
  // Some variables...
  //int numberOfProcesses;
  int i;
 
  // Extract command line arguments.
  if (argc<3){
    std::cerr << "Wrong number of arguments!" << std::endl
             << std::endl
             << "Usage:" << std::endl
             << "perftest_cmd_times processes " << std::endl
             << std::endl
             << "Arguments:" << std::endl
             << "processes  The number of concurrent commands." << std::endl;
    exit(EXIT_FAILURE);
  }

  numberOfProcesses = atoi(argv[1]);

  cmd_str = std::string(argv[2]);
  std::cout<<"cmd_str "<<cmd_str<<std::endl;

  //Parse the arguments of command line
  for (int i = 0;i < (argc -2);i++) {
    arglist.push_back((char *)argv[i+2]);
    std::cout<<"argv: "<<arglist[i]<<std::endl;
  }

  // Start processes.
  finishedProcesses=0;
  mutex=new Glib::Mutex;

  execCommand();

  // Print the result of the test.
  Glib::Mutex::Lock lock(*mutex);
  totalCommands = completedCommands+failedCommands;
  totalTime = completedTime+failedTime;
  std::cout << "========================================" << std::endl;
  std::cout << "Number of processes: "
           << numberOfProcesses << std::endl;
  std::cout << "Number of commands: "
           << totalCommands << std::endl;
  std::cout << "Completed commands: "
           << completedCommands << " ("
           << Round(completedCommands*100.0/totalCommands)
           << "%)" << std::endl;
  std::cout << "Failed commands: "
           << failedCommands << " ("
           << Round(failedCommands*100.0/totalCommands)
           << "%)" << std::endl;
  std::cout << "Average response time for all commands: "
           << Round(1000*totalTime.as_double()/totalCommands)
           << " ms" << std::endl;
  if (completedCommands!=0)
    std::cout << "Average response time for completed commands: "
             << Round(1000*completedTime.as_double()/completedCommands)
             << " ms" << std::endl;
  if (failedCommands!=0)
    std::cout << "Average response time for failed commands: "
             << Round(1000*failedTime.as_double()/failedCommands)
             << " ms" << std::endl;
  std::cout << "========================================" << std::endl;

  return 0;
}

Here is the call graph for this function:

int Round ( double  x)

Definition at line 38 of file perftest_cmd_times.cpp.

                   {
  return int(x+0.5);
}

Variable Documentation

std::vector<std::string> arglist

Definition at line 35 of file perftest_cmd_times.cpp.

std::string cmd_str

Definition at line 34 of file perftest_cmd_times.cpp.

unsigned long completedCommands

Definition at line 26 of file perftest_cmd_times.cpp.

Glib::TimeVal completedTime

Definition at line 29 of file perftest_cmd_times.cpp.

unsigned long failedCommands

Definition at line 27 of file perftest_cmd_times.cpp.

Glib::TimeVal failedTime

Definition at line 30 of file perftest_cmd_times.cpp.

Definition at line 25 of file perftest_cmd_times.cpp.

Glib::Mutex* mutex

Definition at line 24 of file perftest_cmd_times.cpp.

Definition at line 33 of file perftest_cmd_times.cpp.

unsigned long totalCommands

Definition at line 28 of file perftest_cmd_times.cpp.

Glib::TimeVal totalTime

Definition at line 31 of file perftest_cmd_times.cpp.