You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2007/07/08 14:58:12 UTC

svn commit: r554365 - in /activemq/activemq-cpp/trunk/src: main/activemq/util/Queue.h test-benchmarks/Makefile.am test-benchmarks/activemq/util/QueueBenchmark.cpp test-benchmarks/activemq/util/QueueBenchmark.h test-benchmarks/testRegistry.cpp

Author: tabish
Date: Sun Jul  8 05:58:11 2007
New Revision: 554365

URL: http://svn.apache.org/viewvc?view=rev&rev=554365
Log:
http://issues.apache.org/activemq/browse/AMQCPP-128

Queue class cleanup, no real perf increase

Added:
    activemq/activemq-cpp/trunk/src/test-benchmarks/activemq/util/QueueBenchmark.cpp
    activemq/activemq-cpp/trunk/src/test-benchmarks/activemq/util/QueueBenchmark.h
Modified:
    activemq/activemq-cpp/trunk/src/main/activemq/util/Queue.h
    activemq/activemq-cpp/trunk/src/test-benchmarks/Makefile.am
    activemq/activemq-cpp/trunk/src/test-benchmarks/testRegistry.cpp

Modified: activemq/activemq-cpp/trunk/src/main/activemq/util/Queue.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/util/Queue.h?view=diff&rev=554365&r1=554364&r2=554365
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/util/Queue.h (original)
+++ activemq/activemq-cpp/trunk/src/main/activemq/util/Queue.h Sun Jul  8 05:58:11 2007
@@ -26,11 +26,11 @@
 namespace util{
 
     /**
-     * The Queue class accepts messages with an psuh(m) command 
-     * where m is the message to be queued.  It destructively 
+     * The Queue class accepts messages with an psuh(m) command
+     * where m is the message to be queued.  It destructively
      * returns the message with pop().  pop() returns messages in
-     * the order they were enqueued.                                               
-     *                                                              
+     * the order they were enqueued.
+     *
      * Queue is implemented with an instance of the STL queue object.
      * The interface is essentially the same as that of the STL queue
      * except that the pop method actually reaturns a reference to the
@@ -49,105 +49,154 @@
      * for multiple threads to be reading from and writing to the same
      * Queue.
      *
-     * Clients should consider that in a multiple threaded app it is 
+     * Clients should consider that in a multiple threaded app it is
      * possible that items could be placed on the queue faster than
      * you are taking them off, so protection should be placed in your
      * polling loop to ensure that you don't get stuck there.
      */
 
-    template <typename T> class Queue : public concurrent::Synchronizable
-    {
+    template <typename T> class Queue : public concurrent::Synchronizable {
     public:
-   
-        Queue(void);
-        virtual ~Queue(void);
+
+        Queue() {}
+        virtual ~Queue() {}
 
         /**
          * Empties this queue.
          */
-        void clear();
-        
+        void clear() {
+            queue.clear();
+        }
+
         /**
          * Returns a Reference to the element at the head of the queue
          * @return reference to a queue type object or (safe)
          */
-        T& front(void);
+        T& front() {
+            if( queue.empty() ) {
+                return safe;
+            }
+
+            return queue.front();
+        }
 
         /**
          * Returns a Reference to the element at the head of the queue
          * @return reference to a queue type object or (safe)
          */
-        const T& front(void) const;
+        const T& front() const {
+            if( queue.empty() ) {
+                return safe;
+            }
+
+            return queue.front();
+        }
 
         /**
          * Returns a Reference to the element at the tail of the queue
          * @return reference to a queue type object or (safe)
          */
-        T& back(void);
+        T& back() {
+            if( queue.empty() ) {
+                return safe;
+            }
+
+            return queue.back();
+        }
 
         /**
          * Returns a Reference to the element at the tail of the queue
          * @return reference to a queue type object or (safe)
          */
-        const T& back(void) const;
+        const T& back() const {
+            if( queue.empty() ) {
+                return safe;
+            }
+
+            return queue.back();
+        }
 
         /**
          * Places a new Object at the Tail of the queue
          * @param t - Queue Object Type reference.
          */
-        void push( const T &t );
-        
+        void push( const T &t ) {
+            queue.push_back( t );
+        }
+
         /**
          * Places a new Object at the front of the queue
          * @param t - Queue Object Type reference.
          */
-        void enqueueFront( const T &t );
+        void enqueueFront( const T &t ) {
+            queue.push_front( t );
+        }
 
         /**
          * Removes and returns the element that is at the Head of the queue
          * @return reference to a queue type object or (safe)
          */
-        T pop(void);
+        T pop() {
+            if( queue.empty() ) {
+                return safe;
+            }
+
+            // Pop the element into a temp, since we need to remain locked.
+            // this means getting front and then popping.
+            T temp = queue.front();
+            queue.pop_front();
+
+            return temp;
+        }
 
         /**
          * Gets the Number of elements currently in the Queue
          * @return Queue Size
          */
-        size_t size(void) const;
+        size_t size() const{
+            return queue.size();
+        }
 
         /**
          * Checks if this Queue is currently empty
          * @return boolean indicating queue emptiness
          */
-        bool empty(void) const;
-        
+        bool empty() const {
+            return queue.empty();
+        }
+
         /**
          * @return the all values in this queue as a std::vector.
          */
-        virtual std::vector<T> toArray() const;
-        
+        virtual std::vector<T> toArray() const {
+            std::vector<T> valueArray( queue.begin(), queue.end() );
+            return valueArray;
+        }
+
         /**
          * Reverses the order of the contents of this queue and stores them
          * in the target queue.
          * @param target - The target queue that will receive the contents of
          * this queue in reverse order.
          */
-        void reverse( Queue<T>& target ) const;
-   
+        void reverse( Queue<T>& target ) const {
+            target.queue.insert( target.queue.end(), queue.rbegin(), queue.rend() );
+        }
+
         /**
          * Locks the object.
          */
         virtual void lock() throw( exceptions::ActiveMQException ){
             mutex.lock();
         }
-   
+
         /**
          * Unlocks the object.
          */
-        virtual void unlock() throw( exceptions::ActiveMQException ){   
+        virtual void unlock() throw( exceptions::ActiveMQException ){
             mutex.unlock();
         }
-       
+
         /**
          * Waits on a signal from this object, which is generated
          * by a call to Notify.  Must have this object locked before
@@ -156,7 +205,7 @@
         virtual void wait() throw( exceptions::ActiveMQException ){
             mutex.wait();
         }
-    
+
         /**
          * Waits on a signal from this object, which is generated
          * by a call to Notify.  Must have this object locked before
@@ -165,9 +214,9 @@
          * @param millisecs time to wait, or WAIT_INIFINITE
          * @throws ActiveMQException
          */
-        virtual void wait( unsigned long millisecs ) 
+        virtual void wait( unsigned long millisecs )
             throw( exceptions::ActiveMQException ) {
-         
+
             mutex.wait(millisecs);
         }
 
@@ -179,7 +228,7 @@
         virtual void notify() throw( exceptions::ActiveMQException ){
             mutex.notify();
         }
-        
+
         /**
          * Signals the waiters on this object that it can now wake
          * up and continue.  Must have this object locked before
@@ -188,7 +237,7 @@
         virtual void notifyAll() throw( exceptions::ActiveMQException ){
             mutex.notifyAll();
         }
-   
+
     public:   // Statics
 
         /**
@@ -209,153 +258,11 @@
         // Safe value used when pop, front or back are
         // called and the queue is empty.
         static T safe;
-      
+
     };
-   
+
     //-----{ Static Init }----------------------------------------------------//
-    template <typename T>
-    T Queue<T>::safe;
-    
-    //-----{ Retrieve current length of Queue }-------------------------------//
-   
-    template <typename T> inline size_t Queue<T>::size() const
-    {
-        return queue.size();
-    }
-   
-    //-----{ Retrieve whether Queue is empty or not }-------------------------//
-   
-    template <typename T> inline bool Queue<T>::empty(void) const
-    {
-        return queue.empty();
-    }
-   
-    //-----{ Defulat Constructor }--------------------------------------------//
-   
-    template <typename T> 
-    Queue<T>::Queue()
-    {
-    }
-
-    //-----{ Default Destructor }---------------------------------------------//
-   
-    template <typename T> Queue<T>::~Queue()
-    {
-    }
-
-    template <typename T> 
-    void Queue<T>::clear()
-    {
-        queue.clear();
-    }
-    
-    //-----{ Add Elements to Back of Queue }----------------------------------//
-
-    template <typename T> 
-    void Queue<T>::push( const T &t )
-    {
-        queue.push_back( t );
-    }
-    
-    template <typename T> 
-    void Queue<T>::enqueueFront( const T &t )
-    {
-        queue.push_front( t );
-    }
-
-    //-----{ Remove Elements from Front of Queue }----------------------------//
-
-    template <typename T> 
-    T Queue<T>::pop(void)
-    {
-        if(queue.empty())
-        {   
-            return safe;
-        }
-
-        // Pop the element into a temp, since we need to remain locked.
-        // this means getting front and then popping.
-        T temp = queue.front();
-        queue.pop_front();
-   
-        return temp;
-    }
-
-    //-----{ Returnreference to element at front of Queue }-------------------//
-
-    template <typename T> 
-    T& Queue<T>::front(void)
-    {
-        if( queue.empty() )
-        {
-            return safe;
-        }
-         
-        return queue.front();
-    }
-   
-    //-----{ Returnreference to element at front of Queue }-------------------//
-
-    template <typename T> 
-    const T& Queue<T>::front(void) const
-    {
-        if( queue.empty() )
-        {   
-            return safe;
-        }
-
-        return queue.front();
-    }
-   
-    //-----{ Returnreference to element at back of Queue }--------------------//
-
-    template <typename T> 
-    T& Queue<T>::back(void)
-    {
-        if( queue.empty() )
-        {
-            return safe;
-        }
-         
-        return queue.back();
-    }
-   
-    //-----{ Returnreference to element at back of Queue }--------------------//
-
-    template <typename T> 
-    const T& Queue<T>::back(void) const
-    {
-        if( queue.empty() )
-        {
-            return safe;
-        }
-   
-        return queue.back();
-    }
-    
-    template <typename T> 
-    void Queue<T>::reverse( Queue<T>& target ) const 
-    {
-        typename std::list<T>::const_reverse_iterator iter;
-        iter = queue.rbegin();
-        for( ; iter != queue.rend(); ++iter ) {
-            target.push( *iter );
-        }
-    }
-    
-    ////////////////////////////////////////////////////////////////////////////
-    template <typename T>
-    std::vector<T> Queue<T>::toArray() const{
-        std::vector<T> valueArray(queue.size());
-        
-        typename std::list<T>::const_iterator iter;
-        iter=queue.begin();
-        for( int ix=0; iter != queue.end(); ++iter, ++ix ){
-            valueArray[ix] = *iter;
-        }
-        
-        return valueArray;
-    }
+    template <typename T> T Queue<T>::safe;
 
 }}
 

Modified: activemq/activemq-cpp/trunk/src/test-benchmarks/Makefile.am
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test-benchmarks/Makefile.am?view=diff&rev=554365&r1=554364&r2=554365
==============================================================================
--- activemq/activemq-cpp/trunk/src/test-benchmarks/Makefile.am (original)
+++ activemq/activemq-cpp/trunk/src/test-benchmarks/Makefile.am Sun Jul  8 05:58:11 2007
@@ -18,6 +18,7 @@
 cc_sources = \
   activemq/util/PrimitiveMapBenchmark.cpp \
   activemq/util/PropertiesBenchmark.cpp \
+  activemq/util/QueueBenchmark.cpp \
   benchmark/PerformanceTimer.cpp \
   testRegistry.cpp \
   main.cpp
@@ -25,6 +26,7 @@
 hh_sources = \
   activemq/util/PrimitiveMapBenchmark.h \
   activemq/util/PropertiesBenchmark.h \
+  activemq/util/QueueBenchmark.h \
   benchmark/BenchmarkBase.h \
   benchmark/PerformanceTimer.h
 

Added: activemq/activemq-cpp/trunk/src/test-benchmarks/activemq/util/QueueBenchmark.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test-benchmarks/activemq/util/QueueBenchmark.cpp?view=auto&rev=554365
==============================================================================
--- activemq/activemq-cpp/trunk/src/test-benchmarks/activemq/util/QueueBenchmark.cpp (added)
+++ activemq/activemq-cpp/trunk/src/test-benchmarks/activemq/util/QueueBenchmark.cpp Sun Jul  8 05:58:11 2007
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "QueueBenchmark.h"
+
+#include <activemq/util/Integer.h>
+
+using namespace std;
+using namespace activemq;
+using namespace activemq::util;
+
+////////////////////////////////////////////////////////////////////////////////
+QueueBenchmark::QueueBenchmark(){
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void QueueBenchmark::run(){
+
+    int numRuns = 250;
+    std::string test = "test";
+    std::string resultStr = "";
+    int resultInt = 0;
+    Queue<std::string> stringQCopy;
+    Queue<int> intQCopy;
+
+    for( int i = 0; i < numRuns; ++i ) {
+        stringQ.push( test );
+        intQ.push( 65536 );
+    }
+
+    for( int i = 0; i < numRuns; ++i ) {
+        stringQ.pop();
+        intQ.pop();
+    }
+
+    for( int i = 0; i < numRuns; ++i ) {
+        stringQ.enqueueFront( test );
+        intQ.enqueueFront( 1024 );
+    }
+
+    for( int i = 0; i < numRuns; ++i ) {
+        stringQ.reverse( stringQCopy );
+        intQ.reverse( intQCopy );
+    }
+
+    std::vector<std::string> stringVec;
+    std::vector<int> intVec;
+
+    for( int i = 0; i < numRuns; ++i ) {
+        stringVec = stringQ.toArray();
+        intVec = intQ.toArray();
+    }
+
+    for( int i = 0; i < numRuns; ++i ) {
+        resultStr = stringQ.front();
+        resultStr = stringQ.back();
+        resultInt = intQ.front();
+        resultInt = intQ.back();
+        stringQ.pop();
+        intQ.pop();
+    }
+
+}

Added: activemq/activemq-cpp/trunk/src/test-benchmarks/activemq/util/QueueBenchmark.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test-benchmarks/activemq/util/QueueBenchmark.h?view=auto&rev=554365
==============================================================================
--- activemq/activemq-cpp/trunk/src/test-benchmarks/activemq/util/QueueBenchmark.h (added)
+++ activemq/activemq-cpp/trunk/src/test-benchmarks/activemq/util/QueueBenchmark.h Sun Jul  8 05:58:11 2007
@@ -0,0 +1,29 @@
+#ifndef _ACTIVEMQ_QUEUE_QUEUEBENCHMARK_H_
+#define _ACTIVEMQ_QUEUE_QUEUEBENCHMARK_H_
+
+#include <benchmark/BenchmarkBase.h>
+#include <activemq/util/Queue.h>
+
+namespace activemq{
+namespace util{
+
+    class QueueBenchmark :
+        public benchmark::BenchmarkBase<
+            activemq::util::QueueBenchmark, Queue<int> >
+    {
+    private:
+
+        Queue<std::string> stringQ;
+        Queue<int> intQ;
+
+    public:
+
+        QueueBenchmark();
+        virtual ~QueueBenchmark() {}
+
+        virtual void run();
+    };
+
+}}
+
+#endif /*_ACTIVEMQ_QUEUE_QUEUEBENCHMARK_H_*/

Modified: activemq/activemq-cpp/trunk/src/test-benchmarks/testRegistry.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test-benchmarks/testRegistry.cpp?view=diff&rev=554365&r1=554364&r2=554365
==============================================================================
--- activemq/activemq-cpp/trunk/src/test-benchmarks/testRegistry.cpp (original)
+++ activemq/activemq-cpp/trunk/src/test-benchmarks/testRegistry.cpp Sun Jul  8 05:58:11 2007
@@ -17,6 +17,8 @@
 
 #include <activemq/util/PrimitiveMapBenchmark.h>
 #include <activemq/util/PropertiesBenchmark.h>
+#include <activemq/util/QueueBenchmark.h>
 
 CPPUNIT_TEST_SUITE_REGISTRATION( activemq::util::PrimitiveMapBenchmark );
 CPPUNIT_TEST_SUITE_REGISTRATION( activemq::util::PropertiesBenchmark );
+CPPUNIT_TEST_SUITE_REGISTRATION( activemq::util::QueueBenchmark );