xref: /AOO41X/main/bridges/test/performance/testperformance.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
30*cdf0e10cSrcweir #include "precompiled_bridges.hxx"
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include <stdio.h>
33*cdf0e10cSrcweir #include <math.h>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include <osl/interlck.h>
36*cdf0e10cSrcweir #include <osl/mutex.hxx>
37*cdf0e10cSrcweir #include <osl/semaphor.h>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #include <rtl/string.hxx>
40*cdf0e10cSrcweir #include <rtl/byteseq.hxx>
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir #ifdef SAL_W32
45*cdf0e10cSrcweir #include <windows.h>
46*cdf0e10cSrcweir #else
47*cdf0e10cSrcweir #include <sys/times.h>
48*cdf0e10cSrcweir #endif
49*cdf0e10cSrcweir #ifndef ULONG_MAX
50*cdf0e10cSrcweir #define ULONG_MAX 0xffffffff
51*cdf0e10cSrcweir #endif
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir using namespace ::rtl;
54*cdf0e10cSrcweir using namespace ::osl;
55*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir static inline sal_uInt32 getSystemTicks()
58*cdf0e10cSrcweir {
59*cdf0e10cSrcweir #ifdef SAL_W32
60*cdf0e10cSrcweir 	return (sal_uInt32)GetTickCount();
61*cdf0e10cSrcweir #else // only UNX supported for now
62*cdf0e10cSrcweir 	static sal_uInt32	nImplTicksPerSecond = 0;
63*cdf0e10cSrcweir 	static double		dImplTicksPerSecond;
64*cdf0e10cSrcweir 	static double		dImplTicksULONGMAX;
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir 	struct tms			aTms;
67*cdf0e10cSrcweir 	sal_uInt32 nTicks = (sal_uInt32)times( &aTms );
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir 	if ( !nImplTicksPerSecond )
70*cdf0e10cSrcweir 	{
71*cdf0e10cSrcweir 		nImplTicksPerSecond = CLK_TCK;
72*cdf0e10cSrcweir 		dImplTicksPerSecond = nImplTicksPerSecond;
73*cdf0e10cSrcweir 		dImplTicksULONGMAX	= (double)(sal_uInt32)ULONG_MAX;
74*cdf0e10cSrcweir 	}
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir 	double fTicks = nTicks;
77*cdf0e10cSrcweir 	fTicks *= 1000;
78*cdf0e10cSrcweir 	fTicks /= dImplTicksPerSecond;
79*cdf0e10cSrcweir 	fTicks = fmod (fTicks, dImplTicksULONGMAX);
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir 	return (sal_uInt32)fTicks;
82*cdf0e10cSrcweir #endif
83*cdf0e10cSrcweir }
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir class MyTimer
86*cdf0e10cSrcweir {
87*cdf0e10cSrcweir public:
88*cdf0e10cSrcweir 	MyTimer(  const OString &descString ) :
89*cdf0e10cSrcweir 		nStart( getSystemTicks() ),
90*cdf0e10cSrcweir 		m_descString( descString )
91*cdf0e10cSrcweir 		{
92*cdf0e10cSrcweir 		}
93*cdf0e10cSrcweir 	~MyTimer( )
94*cdf0e10cSrcweir 		{
95*cdf0e10cSrcweir 			printf( "%f s : %s\n", (getSystemTicks() -nStart) / 1000., m_descString.getStr() );
96*cdf0e10cSrcweir 		}
97*cdf0e10cSrcweir private:
98*cdf0e10cSrcweir 	sal_uInt32 nStart;
99*cdf0e10cSrcweir 	OString m_descString;
100*cdf0e10cSrcweir };
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir void main()
103*cdf0e10cSrcweir {
104*cdf0e10cSrcweir 	// interlocked count
105*cdf0e10cSrcweir 	{
106*cdf0e10cSrcweir 		MyTimer timer( "performance - 1000*10000 interlocked count" );
107*cdf0e10cSrcweir 		oslInterlockedCount count;
108*cdf0e10cSrcweir 		for( int i = 0 ; i < 1000*10000 ; i ++  )
109*cdf0e10cSrcweir 		{
110*cdf0e10cSrcweir 			osl_incrementInterlockedCount( &count );
111*cdf0e10cSrcweir 		}
112*cdf0e10cSrcweir 	}
113*cdf0e10cSrcweir 	{
114*cdf0e10cSrcweir 		OString myDummyString( "blubber" );
115*cdf0e10cSrcweir 		MyTimer timer( "performance - 1000*10000 acquiring/releasing a refcounted string(without destruction)" );
116*cdf0e10cSrcweir 		for( int i = 0 ; i < 1000*10000 ; i ++  )
117*cdf0e10cSrcweir 		{
118*cdf0e10cSrcweir 			OString myNextDummyString = myDummyString ;
119*cdf0e10cSrcweir 		}
120*cdf0e10cSrcweir 	}
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir 	printf( "--------------------\n" );
123*cdf0e10cSrcweir 	{
124*cdf0e10cSrcweir 		Mutex mutex;
125*cdf0e10cSrcweir 		MyTimer timer( "performance - 1000*10000 acquiring/releasing an osl::Mutex" );
126*cdf0e10cSrcweir 		for( int i = 0 ; i < 1000*10000 ; i ++  )
127*cdf0e10cSrcweir 		{
128*cdf0e10cSrcweir 			MutexGuard guard( mutex );
129*cdf0e10cSrcweir 		}
130*cdf0e10cSrcweir 	}
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir 	{
133*cdf0e10cSrcweir 		oslSemaphore sema = osl_createSemaphore(1);
134*cdf0e10cSrcweir 		MyTimer timer( "performance - 1000*10000 acquiring/releasing an osl::Semaphore" );
135*cdf0e10cSrcweir 		for( int i = 0 ; i < 1000*10000 ; i ++  )
136*cdf0e10cSrcweir 		{
137*cdf0e10cSrcweir 			osl_acquireSemaphore( sema );
138*cdf0e10cSrcweir 			osl_releaseSemaphore( sema );
139*cdf0e10cSrcweir 		}
140*cdf0e10cSrcweir 	}
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir 	printf( "--------------------\n" );
143*cdf0e10cSrcweir 	{
144*cdf0e10cSrcweir 		MyTimer timer( "performance - 1000*10000 rtl::ByteSequence(500)" );
145*cdf0e10cSrcweir 		for( int i = 0 ; i < 1000*1000 ; i ++  )
146*cdf0e10cSrcweir 		{
147*cdf0e10cSrcweir 			ByteSequence seq(500);
148*cdf0e10cSrcweir 		}
149*cdf0e10cSrcweir 	}
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir 	{
152*cdf0e10cSrcweir 		MyTimer timer( "performance - 1000*1000 rtl::ByteSequence(500,BYTESEQ_NODEFAULT)" );
153*cdf0e10cSrcweir 		for( int i = 0 ; i < 1000*1000 ; i ++  )
154*cdf0e10cSrcweir 		{
155*cdf0e10cSrcweir 			ByteSequence seq(500, BYTESEQ_NODEFAULT);
156*cdf0e10cSrcweir 		}
157*cdf0e10cSrcweir 	}
158*cdf0e10cSrcweir 	{
159*cdf0e10cSrcweir 		MyTimer timer( "performance - 1000*1000 com::sun::star::uno::Sequence< sal_Int8 > (500)" );
160*cdf0e10cSrcweir 		for( int i = 0 ; i < 1000*1000 ; i ++  )
161*cdf0e10cSrcweir 		{
162*cdf0e10cSrcweir 			Sequence< sal_Int8> seq(500);
163*cdf0e10cSrcweir 		}
164*cdf0e10cSrcweir 	}
165*cdf0e10cSrcweir 	{
166*cdf0e10cSrcweir 		MyTimer timer( "performance - 1000*1000 rtl_freeMemory( rtl_allocateMemory( 512 ) )" );
167*cdf0e10cSrcweir 		for( int i = 0 ; i < 1000*1000 ; i ++  )
168*cdf0e10cSrcweir 		{
169*cdf0e10cSrcweir 			rtl_freeMemory( rtl_allocateMemory( 512 ) );
170*cdf0e10cSrcweir 		}
171*cdf0e10cSrcweir 	}
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir 	printf( "--------------------\n" );
174*cdf0e10cSrcweir 	{
175*cdf0e10cSrcweir 		MyTimer timer( "performance - 1000*1000 byte  string construction/destruction" );
176*cdf0e10cSrcweir 		for( int i = 0 ; i < 1000*1000 ; i ++  )
177*cdf0e10cSrcweir 		{
178*cdf0e10cSrcweir 			OString textEnc( "this is a test string" );
179*cdf0e10cSrcweir 		}
180*cdf0e10cSrcweir 	}
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir 	{
183*cdf0e10cSrcweir 		MyTimer timer( "performance - 1000*1000 unicode string construction/destruction" );
184*cdf0e10cSrcweir 		for( int i = 0 ; i < 1000*1000 ; i ++  )
185*cdf0e10cSrcweir 		{
186*cdf0e10cSrcweir 			OUString textEnc( RTL_CONSTASCII_USTRINGPARAM( "this is a test string" ) );
187*cdf0e10cSrcweir 		}
188*cdf0e10cSrcweir 	}
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir }
191