testsuite/src/c++/TestRam.cpp

Go to the documentation of this file.
00001 /*--------------------------------------------------------------------------
00002 Name:      TestRam.cpp
00003 Project:   xmlBlaster.org
00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
00005 Comment:   Load test for xmlBlaster
00006 Version:   $Id: TestRam.cpp 14955 2006-03-20 12:40:31Z goetzger $
00007 ---------------------------------------------------------------------------*/
00008 #include <util/XmlBCfg.h>
00009 #include "TestSuite.h"
00010 #include <util/StopWatch.h>
00011 #include <iostream>
00012 
00013 using namespace std;
00014 using namespace org::xmlBlaster::client;
00015 using namespace org::xmlBlaster::util;
00016 using namespace org::xmlBlaster::util::qos;
00017 using namespace org::xmlBlaster::authentication;
00018 using namespace org::xmlBlaster::client::key;
00019 using namespace org::xmlBlaster::client::qos;
00020 
00021 
00033 namespace org { namespace xmlBlaster { namespace test {
00034 
00042 class TestRam : public TestSuite
00043 {
00044 
00045 private:
00046 
00047    static const string::size_type NUM_PUBLISH = 1000;
00048    StopWatch stopWatch_;
00049    string    publishOid_;
00050    string    senderName_;
00051    string    senderContent_;
00052    string    contentMime_;
00053    string    contentMimeExtended_;
00054 
00055 public:
00056    TestRam(int args, char *argc[], const string &loginName) 
00057       :  TestSuite(args, argc, "TestRam"), stopWatch_()
00058    {
00059       senderName_   = loginName;
00060       publishOid_   = "";
00061       contentMime_  = "text/plain";
00062       contentMimeExtended_ = "1.0";
00063    }
00064 
00065    ~TestRam() {
00066    }
00067 
00068 
00074    void setUp() 
00075    {
00076       TestSuite::setUp();
00077       try {
00078          string passwd = "secret";
00079          SecurityQos secQos(global_, senderName_, passwd);
00080          ConnectQos connQos(global_);
00081          connQos.setSecurityQos(secQos);
00082          connection_.connect(connQos, 0);
00083           // Connect to xmlBlaster without Callback
00084       }
00085       catch (XmlBlasterException &e) {
00086           log_.error(ME, e.toXml());
00087           usage();
00088       }
00089    }
00090 
00091 
00097    void tearDown() 
00098    {
00099       log_.info(ME, "tearDown() ...");
00100 
00101       for (string::size_type i=0; i < NUM_PUBLISH; i++) {
00102          EraseKey key(global_);
00103          key.setOid(string("TestRam-") + lexical_cast<string>(i+1));
00104          EraseQos qos(global_);
00105          vector<EraseReturnQos> strArr;
00106          try {
00107             strArr = connection_.erase(key, qos);
00108             if (strArr.size() != 1) {
00109                log_.error(ME, "num erased messages is wrong");
00110                assert(0);
00111             }
00112          }
00113          catch(XmlBlasterException &e) {
00114             log_.error(ME, string("XmlBlasterException: ") + e.toXml());
00115          }
00116       }
00117       log_.info(ME, "Erased " + lexical_cast<string>(NUM_PUBLISH) + " topics");
00118 
00119       connection_.disconnect(DisconnectQos(global_));
00120    }
00121 
00122 
00128    void testPublish() 
00129    {
00130       if (log_.trace()) log_.trace(ME, "Publishing new topics ...");
00131 
00132       vector<util::MessageUnit> msgVec;
00133       msgVec.reserve(NUM_PUBLISH);
00134 
00135       for (string::size_type i=0; i < NUM_PUBLISH; i++) {
00136          PublishKey key(global_);
00137          key.setOid(string("TestRam-") + lexical_cast<string>(i+1));
00138          senderContent_ = lexical_cast<string>(i+1);
00139          PublishQos qos(global_);
00140          util::MessageUnit msgUnit(key, senderContent_, qos);
00141          msgVec.push_back(msgUnit);
00142       }
00143 
00144       try {
00145          // 1. Query the current memory allocated in xmlBlaster
00146          GetKey key(global_);
00147          key.setOid("__cmd:?usedMem");
00148          GetQos qos(global_);
00149          vector<util::MessageUnit> msgRetVec = connection_.get(key, qos);
00150          if (msgRetVec.size() != 1) {
00151             log_.error(ME, "msgRetVec.length!=1");
00152             assert(0);
00153          }
00154          if (msgRetVec[0].getContentLen() == 0) {
00155             log_.error(ME, "returned msgRetVec[0].msgUnit.content.length == 0");
00156             assert(0);
00157          }
00158          string usedMemBefore = msgRetVec[0].getContentStr();
00159          long usedBefore = lexical_cast<long>(usedMemBefore);
00160          log_.info(ME, string("xmlBlaster used allocated memory before ") +
00161                    "publishing = " + usedMemBefore);
00162 
00163          log_.info(ME, "Publishing " + lexical_cast<string>(NUM_PUBLISH) + " new topics ...");
00164          stopWatch_.restart();
00165          // 2. publish all the messages
00166          vector<PublishReturnQos> publishOidArr = connection_.publishArr(msgVec);
00167          double elapsed = 0.001 * stopWatch_.elapsed();
00168 
00169          for (unsigned int i=0; i < NUM_PUBLISH; i++) {
00170             cout << msgVec[i].getKey().toXml() << endl;
00171             //cout << msgVec[i].getContentStr() << endl;
00172          }
00173 
00174          long avg = (long)((double)NUM_PUBLISH / elapsed);
00175          log_.info(ME, "Success: Publishing done, " + lexical_cast<string>(NUM_PUBLISH) + " messages sent, average new topics/second = " + lexical_cast<string>(avg));
00176 
00177          if (publishOidArr.size() != NUM_PUBLISH) {
00178             log_.error(ME, "numPublished=" + lexical_cast<string>(publishOidArr.size()) + " is wrong");
00179             assert(0);
00180          }
00181 
00182          // 3. Query the memory allocated in xmlBlaster after publishing all
00183          // the messages
00184          msgRetVec = connection_.get(key, qos);
00185          string usedMemAfter = msgRetVec[0].getContentStr();
00186          long usedAfter = lexical_cast<long>(usedMemAfter);
00187          log_.info(ME, string("xmlBlaster used allocated memory after ") +
00188                    "publishing = " + usedMemAfter);
00189          log_.info(ME, lexical_cast<string>((usedAfter-usedBefore)/NUM_PUBLISH) + " bytes/topic");
00190       }
00191       catch(XmlBlasterException &e) {
00192          log_.warn(ME, string("Exception: ") + e.toXml());
00193          assert(0);
00194       }
00195    }
00196 
00197 
00201    void testManyPublish() 
00202    {
00203       testPublish();
00204    }
00205 
00206    void usage() const
00207    {
00208                 TestSuite::usage();
00209       log_.plain(ME, "----------------------------------------------------------");
00210       log_.plain(ME, "Testing C++ access to xmlBlaster");
00211       log_.plain(ME, "Usage:");
00212       XmlBlasterAccess::usage();
00213       log_.usage();
00214       log_.plain(ME, "Example:");
00215       log_.plain(ME, "   TestRam -bootstrapHostname myHostName");
00216       log_.plain(ME, "----------------------------------------------------------");
00217    }
00218 };
00219 
00220 }}} // namespace
00221 
00222 using namespace org::xmlBlaster::test;
00223 
00224 int main(int args, char *argc[]) {
00225    org::xmlBlaster::util::Object_Lifetime_Manager::init();
00226    try {
00227       TestRam testObj(args, argc, "Tim");
00228       testObj.setUp();
00229       testObj.testManyPublish();
00230       testObj.tearDown();
00231    }
00232    catch (...) {
00233       std::cout << "ERROR: Caught exception!!!!" << endl;
00234    }
00235    org::xmlBlaster::util::Object_Lifetime_Manager::fini();
00236    return 0;
00237 }
00238 
00239