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