Added tests for ClassLevelLockable policy for SmallObject allocator.

git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@773 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
rich_sposato 2006-11-03 00:38:25 +00:00
parent 1743fd1da3
commit 337f50ba9d
2 changed files with 91 additions and 30 deletions

View file

@ -14,11 +14,12 @@
// $Id$ // $Id$
//#define LOKI_CLASS_LEVEL_THREADING // ----------------------------------------------------------------------------
//#define LOKI_OBJECT_LEVEL_THREADING
#define LOKI_SMALL_OBJECT_USE_NEW_ARRAY #define LOKI_SMALL_OBJECT_USE_NEW_ARRAY
#define LOKI_CLASS_LEVEL_THREADING
#include <loki/SmallObj.h> #include <loki/SmallObj.h>
#include "timer.h" #include "timer.h"
@ -33,6 +34,8 @@
using namespace std; using namespace std;
// ----------------------------------------------------------------------------
template<unsigned int N> template<unsigned int N>
class ThisIsASmallObject class ThisIsASmallObject
{ {
@ -48,6 +51,8 @@ struct Base<N, void> : public ThisIsASmallObject<N>
{}; {};
// ----------------------------------------------------------------------------
#ifdef COMPARE_BOOST_POOL #ifdef COMPARE_BOOST_POOL
template<unsigned int N> template<unsigned int N>
@ -110,6 +115,8 @@ boost::object_pool< BoostPoolNew<N> > BoostPoolNew<N>::BoostPool;
#endif #endif
// ----------------------------------------------------------------------------
int array_test_nr = 0; int array_test_nr = 0;
double t100_new = 0; double t100_new = 0;
@ -139,6 +146,8 @@ int FUNC(void**, const int N, int loop, Timer& t, const char* s) \
#define LOKI_BOOST_TEST_NR -1 #define LOKI_BOOST_TEST_NR -1
#endif #endif
// ----------------------------------------------------------------------------
#define LOKI_SMALLOBJ_BENCH_ARRAY(FUNC, CODE_DECL, CODE_NEW, CODE_DELETE) \ #define LOKI_SMALLOBJ_BENCH_ARRAY(FUNC, CODE_DECL, CODE_NEW, CODE_DELETE) \
template<class T, int TN> \ template<class T, int TN> \
int FUNC(void** arrv, const int N, int loop, Timer& t, const char* s) \ int FUNC(void** arrv, const int N, int loop, Timer& t, const char* s) \
@ -194,6 +203,8 @@ int FUNC(void** arrv, const int N, int loop, Timer& t, const char* s) \
return t.t(); \ return t.t(); \
} }
// ----------------------------------------------------------------------------
LOKI_SMALLOBJ_BENCH(delete_new ,delete new T;) LOKI_SMALLOBJ_BENCH(delete_new ,delete new T;)
LOKI_SMALLOBJ_BENCH(delete_new_mal ,std::free(std::malloc(sizeof(T)));) LOKI_SMALLOBJ_BENCH(delete_new_mal ,std::free(std::malloc(sizeof(T)));)
@ -220,9 +231,11 @@ LOKI_SMALLOBJ_BENCH_ARRAY(new_del_a_on_a_all,std::allocator<T[TN]> st ,
st.deallocate(reinterpret_cast<T(*)[TN]>(arr[i]), 1);) st.deallocate(reinterpret_cast<T(*)[TN]>(arr[i]), 1);)
// ----------------------------------------------------------------------------
#ifndef COMPARE_BOOST_POOL #ifndef COMPARE_BOOST_POOL
#define LOKI_SMALLOBJBECH_ABCD(FUNC,N,LOOP,TIMER,MESSAGE) \ #define LOKI_SMALL_OBJECT_BENCH_ABCD(FUNC,N,LOOP,TIMER,MESSAGE) \
array_test_nr = 0; \ array_test_nr = 0; \
cout << MESSAGE << endl; \ cout << MESSAGE << endl; \
FUNC<A,N>(a,N,LOOP,TIMER,"new :"); \ FUNC<A,N>(a,N,LOOP,TIMER,"new :"); \
@ -232,7 +245,7 @@ LOKI_SMALLOBJ_BENCH_ARRAY(new_del_a_on_a_all,std::allocator<T[TN]> st ,
FUNC##_mal<A,N>(a,N,LOOP,TIMER,"malloc :"); \ FUNC##_mal<A,N>(a,N,LOOP,TIMER,"malloc :"); \
cout << endl << endl; cout << endl << endl;
#else #else
#define LOKI_SMALLOBJBECH_ABCD(FUNC,N,LOOP,TIMER,MESSAGE) \ #define LOKI_SMALL_OBJECT_BENCH_ABCD(FUNC,N,LOOP,TIMER,MESSAGE) \
array_test_nr = 0; \ array_test_nr = 0; \
cout << MESSAGE << endl; \ cout << MESSAGE << endl; \
FUNC<A,N>(a,N,LOOP,TIMER,"new :"); \ FUNC<A,N>(a,N,LOOP,TIMER,"new :"); \
@ -244,19 +257,36 @@ LOKI_SMALLOBJ_BENCH_ARRAY(new_del_a_on_a_all,std::allocator<T[TN]> st ,
cout << endl << endl; cout << endl << endl;
#endif #endif
#define LOKI_ALLOCATOR_PARAMETERS Loki::SingleThreaded, 4096, 128, 4, Loki::NoDestroy // ----------------------------------------------------------------------------
template<unsigned int Size, int loop> template<
unsigned int Size,
int loop,
template <class, class> class ThreadingModel,
std::size_t chunkSize,
std::size_t maxSmallObjectSize,
std::size_t objectAlignSize,
template <class> class LifetimePolicy,
class MutexPolicy
>
void testSize() void testSize()
{ {
//#define LOKI_ALLOCATOR_PARAMETERS ::Loki::SingleThreaded, 4096, 128, 4, Loki::NoDestroy
typedef Base<Size, void> A; typedef Base<Size, void> A;
typedef Base<Size, Loki::SmallObject< LOKI_ALLOCATOR_PARAMETERS > > B; typedef Base<Size, Loki::SmallObject< ThreadingModel, chunkSize,
typedef Base<Size, Loki::SmallValueObject< LOKI_ALLOCATOR_PARAMETERS > > C; maxSmallObjectSize, objectAlignSize, LifetimePolicy, MutexPolicy > > B;
typedef Base<Size, Loki::SmallValueObject< ThreadingModel, chunkSize,
maxSmallObjectSize, objectAlignSize, LifetimePolicy, MutexPolicy > > C;
typedef Loki::AllocatorSingleton< ThreadingModel, chunkSize,
maxSmallObjectSize, objectAlignSize, LifetimePolicy, MutexPolicy > AllocatorSingleton;
#ifdef COMPARE_BOOST_POOL #ifdef COMPARE_BOOST_POOL
typedef BoostPoolNew<Size> D; typedef BoostPoolNew<Size> D;
#endif #endif
assert( (!Loki::AllocatorSingleton< LOKI_ALLOCATOR_PARAMETERS >::IsCorrupted()) ); assert( (!AllocatorSingleton::IsCorrupted()) );
cout << endl << endl; cout << endl << endl;
cout << "Allocator Benchmark Tests with " << Size << " bytes big objects " << endl; cout << "Allocator Benchmark Tests with " << Size << " bytes big objects " << endl;
cout << endl; cout << endl;
@ -278,49 +308,80 @@ void testSize()
void** a= new void*[Narr]; void** a= new void*[Narr];
cout << loop << " times "; cout << loop << " times ";
LOKI_SMALLOBJBECH_ABCD(delete_new ,0,loop,t,"'delete new T'"); LOKI_SMALL_OBJECT_BENCH_ABCD(delete_new ,0,loop,t,"'delete new T'");
assert( (!Loki::AllocatorSingleton< LOKI_ALLOCATOR_PARAMETERS >::IsCorrupted()) ); assert( (!AllocatorSingleton::IsCorrupted()) );
cout << "N=" << N <<" : " << loop << " times "; cout << "N=" << N <<" : " << loop << " times ";
LOKI_SMALLOBJBECH_ABCD(delete_new_array ,N,loop,t,"'delete[] new T[N]'"); LOKI_SMALL_OBJECT_BENCH_ABCD(delete_new_array ,N,loop,t,"'delete[] new T[N]'");
assert( (!Loki::AllocatorSingleton< LOKI_ALLOCATOR_PARAMETERS >::IsCorrupted()) ); assert( (!AllocatorSingleton::IsCorrupted()) );
cout << "i=0..." << Narr << " : "; cout << "i=0..." << Narr << " : ";
LOKI_SMALLOBJBECH_ABCD(new_del_on_arr ,0,Narr,t,"1. 'arr[i] = new T' 2. 'delete arr[i]'"); LOKI_SMALL_OBJECT_BENCH_ABCD(new_del_on_arr ,0,Narr,t,"1. 'arr[i] = new T' 2. 'delete arr[i]'");
assert( (!Loki::AllocatorSingleton< LOKI_ALLOCATOR_PARAMETERS >::IsCorrupted()) ); assert( (!AllocatorSingleton::IsCorrupted()) );
cout << "i=0..." << Narr << ", N=" << N <<" : "; cout << "i=0..." << Narr << ", N=" << N <<" : ";
LOKI_SMALLOBJBECH_ABCD(new_del_a_on_a ,N,Narr,t,"1. 'arr[i] = new T[N]' 2. 'delete[] arr[i]'"); LOKI_SMALL_OBJECT_BENCH_ABCD(new_del_a_on_a ,N,Narr,t,"1. 'arr[i] = new T[N]' 2. 'delete[] arr[i]'");
assert( (!Loki::AllocatorSingleton< LOKI_ALLOCATOR_PARAMETERS >::IsCorrupted()) ); assert( (!AllocatorSingleton::IsCorrupted()) );
delete [] a; delete [] a;
cout << "_________________________________________________________________" << endl; cout << "_________________________________________________________________" << endl;
assert( (!Loki::AllocatorSingleton< LOKI_ALLOCATOR_PARAMETERS >::IsCorrupted()) ); assert( (!AllocatorSingleton::IsCorrupted()) );
Loki::AllocatorSingleton< LOKI_ALLOCATOR_PARAMETERS >::ClearExtraMemory(); AllocatorSingleton::ClearExtraMemory();
} }
// ----------------------------------------------------------------------------
int main() void DoSingleThreadTest (void)
{ {
const int loop = 1000*1000; const int loop = 1000*1000;
cout << endl; cout << endl;
testSize< 2, loop, ::Loki::SingleThreaded, 4096, 128, 4, ::Loki::NoDestroy, ::Loki::Mutex >();
testSize< 2,loop>(); testSize< 3, loop, ::Loki::SingleThreaded, 4096, 128, 4, ::Loki::NoDestroy, ::Loki::Mutex >();
testSize< 3,loop>(); testSize< 8, loop, ::Loki::SingleThreaded, 4096, 128, 4, ::Loki::NoDestroy, ::Loki::Mutex >();
testSize< 8,loop>(); testSize< 9, loop, ::Loki::SingleThreaded, 4096, 128, 4, ::Loki::NoDestroy, ::Loki::Mutex >();
testSize< 9,loop>(); testSize< 16, loop, ::Loki::SingleThreaded, 4096, 128, 4, ::Loki::NoDestroy, ::Loki::Mutex >();
testSize<16,loop>(); testSize< 17, loop, ::Loki::SingleThreaded, 4096, 128, 4, ::Loki::NoDestroy, ::Loki::Mutex >();
testSize<17,loop>();
#if defined(__BORLANDC__) || defined(_MSC_VER) #if defined(__BORLANDC__) || defined(_MSC_VER)
system("PAUSE"); system("PAUSE");
#endif #endif
}
// ----------------------------------------------------------------------------
#if defined(LOKI_CLASS_LEVEL_THREADING)
void DoClassLockTest (void)
{
const int loop = 1000*1000;
cout << endl;
testSize< 2, loop, ::Loki::ClassLevelLockable, 4096, 128, 4, ::Loki::NoDestroy, ::Loki::Mutex >();
testSize< 3, loop, ::Loki::ClassLevelLockable, 4096, 128, 4, ::Loki::NoDestroy, ::Loki::Mutex >();
testSize< 8, loop, ::Loki::ClassLevelLockable, 4096, 128, 4, ::Loki::NoDestroy, ::Loki::Mutex >();
testSize< 9, loop, ::Loki::ClassLevelLockable, 4096, 128, 4, ::Loki::NoDestroy, ::Loki::Mutex >();
testSize< 16, loop, ::Loki::ClassLevelLockable, 4096, 128, 4, ::Loki::NoDestroy, ::Loki::Mutex >();
testSize< 17, loop, ::Loki::ClassLevelLockable, 4096, 128, 4, ::Loki::NoDestroy, ::Loki::Mutex >();
#if defined(__BORLANDC__) || defined(_MSC_VER)
system("PAUSE");
#endif
}
#endif
// ----------------------------------------------------------------------------
int main()
{
DoSingleThreadTest();
#if defined(LOKI_CLASS_LEVEL_THREADING)
DoClassLockTest();
#endif
return 0; return 0;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="Windows-1252"?> <?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject <VisualStudioProject
ProjectType="Visual C++" ProjectType="Visual C++"
Version="8,00" Version="8.00"
Name="SmallObjCompare" Name="SmallObjCompare"
ProjectGUID="{0A98B714-818C-4DD3-A07C-BDD16399F362}" ProjectGUID="{0A98B714-818C-4DD3-A07C-BDD16399F362}"
Keyword="Win32Proj" Keyword="Win32Proj"