OSDN Git Service

queue_testを追加
authornakai <nakai@1ed66053-1c2d-0410-8867-f7571e6e31d3>
Tue, 9 Jun 2009 04:25:22 +0000 (04:25 +0000)
committernakai <nakai@1ed66053-1c2d-0410-8867-f7571e6e31d3>
Tue, 9 Jun 2009 04:25:22 +0000 (04:25 +0000)
git-svn-id: http://10.144.169.20/repos/um/branches/l7vsd-3.x-shamshel-refine@7893 1ed66053-1c2d-0410-8867-f7571e6e31d3

unit_tests/bentch/Makefile
unit_tests/bentch/queue_test.cpp [new file with mode: 0644]

index 5a2a0ef..249d6a2 100644 (file)
@@ -1,6 +1,7 @@
 TARGET         = atomic_bench
+TARGET2                = queue_bench
 CPP            = g++
-CPPFLAGS       = -Wall -O2 -g -Werror -pthread 
+CPPFLAGS       = -O2 -g -Werror -pthread 
 INCLUDES       =       -I../../include
 LIBS           =
 
@@ -8,6 +9,7 @@ LDFLAGS         = -lrt -ldl
 
 
 SRCS           = atomic_test.cpp
+SRCS2          = queue_test.cpp
 
 
 .SUFFIXES:     .o .cpp
@@ -15,14 +17,18 @@ SRCS                = atomic_test.cpp
                $(CPP) $(CPPFLAGS) $(INCLUDES) -c $< -o $@
 
 OBJS           = $(SRCS:.cpp=.o)
+OBJS2          = $(SRCS2:.cpp=.o)
 
-all:   $(TARGET)
+all:   $(TARGET) $(TARGET2)
 
 $(TARGET):     $(OBJS)
        $(CPP) $(INCLUDES) -o $@ $(LDFLAGS) $(OBJS) $(LIBS)
 
+$(TARGET2):    $(OBJS2)
+       $(CPP) $(INCLUDES) -o $@ $(LDFLAGS) $(OBJS) $(LIBS)
+
 
 clean:
-       rm -f $(TARGET) $(OBJS)
+       rm -f $(TARGET) $(OBJS) $(TARGET2) $(OBJS2)
 
 
diff --git a/unit_tests/bentch/queue_test.cpp b/unit_tests/bentch/queue_test.cpp
new file mode 100644 (file)
index 0000000..643c807
--- /dev/null
@@ -0,0 +1,88 @@
+#include <pthread.h>
+#include <iostream>
+#include <boost/lexical_cast.hpp>
+#include <vector>
+#include <queue>
+#include "lockfree_queue.h"
+#include "rdtsc64.h"
+
+
+l7vs::lockfree_queue< long long >      lockfree_queue_long;
+std::queue<long long>                  stl_queue_long;
+pthread_mutex_t                                mutex;
+
+
+void*  thread_func_atomic( void* param ){
+       unsigned long long starttime, endtime;
+       RDTSC64( starttime );
+       for( long long i = 0 ; i < 100; ++i ){
+               lockfree_queue_long.push( i );  
+       }
+       for( long long i = 0 ; i < 100; ++i ){
+               long long value;
+               lockfree_queue_long.pop( value );
+       }
+       RDTSC64( endtime );
+       std::cout << endtime - starttime << " , " << std::endl;
+       return 0;
+}
+
+void*  thread_func_mutex( void* param ){
+       unsigned long long starttime, endtime;
+       RDTSC64( starttime );
+       for( long long i = 0 ; i < 100; ++i ){
+               pthread_mutex_lock( &mutex );
+               stl_queue_long.push( i );
+               pthread_mutex_unlock( &mutex );
+       }
+       for( long long i = 0 ; i < 100; ++i ){
+               pthread_mutex_lock( &mutex );
+               long long v = stl_queue_long.front();
+               stl_queue_long.pop();
+       }
+       RDTSC64( endtime );
+       std::cout << endtime - starttime << " ," << std::endl;
+       return 0;
+}
+
+int main( int argc, char* argv[] ){
+
+       pthread_mutex_init( &mutex, NULL );
+       if( argc < 2 ){
+               std::cout << "usage : queue_bench [thread_num] " << std::endl;
+               return 0;
+       }
+
+       int count = boost::lexical_cast<int>( argv[1] );
+       std::vector<pthread_t>  thread_vec;
+       pthread_t       thd;
+
+       //atomic version
+       std::cout << "atomic func time start" << std::endl;
+       for( int i = 0; i < count; ++i ){
+               pthread_create( &thd, NULL, thread_func_atomic, NULL );
+               thread_vec.push_back( thd );
+       }
+       for( std::vector<pthread_t>::iterator itr = thread_vec.begin();
+            itr != thread_vec.end();
+            ++itr ){
+               pthread_join( *itr, NULL );
+       }
+       thread_vec.clear();
+
+       //mutex version
+       std::cout << "mutex func time start" << std::endl;
+       for( int i = 0; i < count; ++i ){
+               pthread_create( &thd, NULL, thread_func_mutex, NULL );
+               thread_vec.push_back( thd );
+       }
+       for( std::vector<pthread_t>::iterator itr = thread_vec.begin();
+            itr != thread_vec.end();
+            ++itr ){
+               pthread_join( *itr, NULL );
+       }
+
+       pthread_mutex_destroy( &mutex );
+
+       return 0;
+}