Back to index

nordugrid-arc-nox  1.1.0~rc6
perftest_deleg_bydelegclient.cpp
Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include <config.h>
00003 #endif
00004 
00005 // perftest_deleg_bydelegclient.cpp
00006 
00007 #include <iostream>
00008 #include <fstream>
00009 #include <string>
00010 #include <stdlib.h>
00011 #include <glibmm/thread.h>
00012 #include <glibmm/timer.h>
00013 
00014 #include <arc/GUID.h>
00015 #include <arc/ArcConfig.h>
00016 #include <arc/Logger.h>
00017 #include <arc/URL.h>
00018 #include <arc/message/PayloadSOAP.h>
00019 #include <arc/message/MCC.h>
00020 #include <arc/client/ClientInterface.h>
00021 #include <arc/client/ClientX509Delegation.h>
00022 
00023 // Some global shared variables...
00024 Glib::Mutex* mutex;
00025 bool run;
00026 int finishedThreads;
00027 unsigned long completedRequests;
00028 unsigned long failedRequests;
00029 unsigned long totalRequests;
00030 Glib::TimeVal completedTime;
00031 Glib::TimeVal failedTime;
00032 Glib::TimeVal totalTime;
00033 std::string url_str;
00034 
00035 // Round off a double to an integer.
00036 int Round(double x){
00037   return int(x+0.5);
00038 }
00039 
00040 // Send requests and collect statistics.
00041 void sendRequests(){
00042   // Some variables...
00043   unsigned long completedRequests = 0;
00044   unsigned long failedRequests = 0;
00045   Glib::TimeVal completedTime(0,0);
00046   Glib::TimeVal failedTime(0,0);
00047   Glib::TimeVal tBefore;
00048   Glib::TimeVal tAfter;
00049   bool connected;
00050 
00051   //std::string url_str("https://127.0.0.1:60000/echo");
00052   Arc::URL url(url_str);
00053 
00054   Arc::MCCConfig mcc_cfg;
00055   mcc_cfg.AddPrivateKey("../echo/userkey-nopass.pem");
00056   mcc_cfg.AddCertificate("../echo/usercert.pem");
00057   mcc_cfg.AddCAFile("../echo/testcacert.pem");
00058   mcc_cfg.AddCADir("../echo/certificates");
00059   
00060   while(run){
00061     
00062     // Create a Client.
00063     Arc::ClientX509Delegation *client = NULL;
00064     client = new Arc::ClientX509Delegation(mcc_cfg,url);
00065 
00066     connected=true;
00067     while(run and connected){
00068       // Send the delegation request and time it.
00069       tBefore.assign_current_time();
00070       std::string arc_delegation_id;
00071       bool res;
00072       if(client) {
00073         if(!(res = client->createDelegation(Arc::DELEG_ARC, arc_delegation_id))) {
00074           std::cerr<<"Delegation to ARC delegation service failed"<<std::endl;
00075         }
00076       }
00077       tAfter.assign_current_time();
00078 
00079       if(!res) {
00080         // The delegation has not succeeded.
00081         failedRequests++;
00082         failedTime+=tAfter-tBefore;
00083         connected=false;
00084       }
00085       else {
00086         // Everything worked just fine.
00087         std::cout<<"Delegation ID: "<<arc_delegation_id<<std::endl;
00088         completedRequests++;
00089         completedTime+=tAfter-tBefore;
00090       }
00091     }
00092     if(client) delete client;
00093   }
00094 
00095   // Update global variables.
00096   Glib::Mutex::Lock lock(*mutex);
00097   ::completedRequests+=completedRequests;
00098   ::failedRequests+=failedRequests;
00099   ::completedTime+=completedTime;
00100   ::failedTime+=failedTime;
00101   finishedThreads++;
00102   std::cout << "Number of finished threads: " << finishedThreads << std::endl;
00103 }
00104 
00105 int main(int argc, char* argv[]){
00106   // Some variables...
00107   int numberOfThreads;
00108   int duration;
00109   int i;
00110   Glib::Thread** threads;
00111   const char* config_file = NULL;
00112   int debug_level = -1;
00113   Arc::LogStream logcerr(std::cerr);
00114 
00115   // Process options - quick hack, must use Glib options later
00116   while(argc >= 3) {
00117     if(strcmp(argv[1],"-c") == 0) {
00118       config_file = argv[2];
00119       argv[2]=argv[0]; argv+=2; argc-=2;
00120     } else if(strcmp(argv[1],"-d") == 0) {
00121       debug_level=Arc::string_to_level(argv[2]);
00122       argv[2]=argv[0]; argv+=2; argc-=2;
00123     } else {
00124       break;
00125     };
00126   } 
00127   if(debug_level >= 0) {
00128     Arc::Logger::getRootLogger().setThreshold((Arc::LogLevel)debug_level);
00129     Arc::Logger::getRootLogger().addDestination(logcerr);
00130   }
00131   // Extract command line arguments.
00132   if (argc!=4){
00133     std::cerr << "Wrong number of arguments!" << std::endl
00134              << std::endl
00135              << "Usage:" << std::endl
00136              << "perftest_deleg_bydelegclient [-c config] [-d debug] url threads duration" << std::endl
00137              << std::endl
00138              << "Arguments:" << std::endl
00139              << "url     The url of the delegation service." << std::endl
00140              << "threads  The number of concurrent requests." << std::endl
00141              << "duration The duration of the test in seconds." << std::endl
00142              << "config   The file containing client chain XML configuration with " << std::endl
00143               << "         'soap' entry point and HOSTNAME, PORTNUMBER and PATH " << std::endl
00144               << "         keyword for hostname, port and HTTP path of 'echo' service." << std::endl
00145              << "debug    The textual representation of desired debug level. Available " << std::endl
00146               << "         levels: DEBUG, VERBOSE, INFO, WARNING, ERROR, FATAL." << std::endl;
00147     exit(EXIT_FAILURE);
00148   }
00149   url_str = std::string(argv[1]);
00150   numberOfThreads = atoi(argv[2]);
00151   duration = atoi(argv[3]);
00152 
00153   // Start threads.
00154   run=true;
00155   finishedThreads=0;
00156   //Glib::thread_init();
00157   mutex=new Glib::Mutex;
00158   threads = new Glib::Thread*[numberOfThreads];
00159   for (i=0; i<numberOfThreads; i++)
00160     threads[i]=Glib::Thread::create(sigc::ptr_fun(sendRequests),true);
00161 
00162   // Sleep while the threads are working.
00163   Glib::usleep(duration*1000000);
00164 
00165   // Stop the threads
00166   run=false;
00167   while(finishedThreads<numberOfThreads)
00168     Glib::usleep(100000);
00169 
00170   // Print the result of the test.
00171   Glib::Mutex::Lock lock(*mutex);
00172   totalRequests = completedRequests+failedRequests;
00173   totalTime = completedTime+failedTime;
00174   std::cout << "========================================" << std::endl;
00175   std::cout << "URL: "
00176            << url_str << std::endl;
00177   std::cout << "Number of threads: "
00178            << numberOfThreads << std::endl;
00179   std::cout << "Duration: "
00180            << duration << " s" << std::endl;
00181   std::cout << "Number of requests: "
00182            << totalRequests << std::endl;
00183   std::cout << "Completed requests: "
00184            << completedRequests << " ("
00185            << Round(completedRequests*100.0/totalRequests)
00186            << "%)" << std::endl;
00187   std::cout << "Failed requests: "
00188            << failedRequests << " ("
00189            << Round(failedRequests*100.0/totalRequests)
00190            << "%)" << std::endl;
00191   std::cout << "Average response time for all requests: "
00192            << Round(1000*totalTime.as_double()/totalRequests)
00193            << " ms" << std::endl;
00194   if (completedRequests!=0)
00195     std::cout << "Average response time for completed requests: "
00196              << Round(1000*completedTime.as_double()/completedRequests)
00197              << " ms" << std::endl;
00198   if (failedRequests!=0)
00199     std::cout << "Average response time for failed requests: "
00200              << Round(1000*failedTime.as_double()/failedRequests)
00201              << " ms" << std::endl;
00202   std::cout << "========================================" << std::endl;
00203 
00204   return 0;
00205 }