1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include "dbtest_base.cxx"
12 #include <boost/scoped_ptr.hpp>
13 #include <osl/file.hxx>
14 #include <osl/process.h>
16 #include <rtl/ustrbuf.hxx>
17 #include <tools/stream.hxx>
18 #include <unotools/tempfile.hxx>
20 #include <com/sun/star/beans/XPropertySet.hpp>
21 #include <com/sun/star/frame/XStorable.hpp>
22 #include <com/sun/star/lang/XComponent.hpp>
23 #include <com/sun/star/sdb/XOfficeDatabaseDocument.hpp>
24 #include <com/sun/star/sdbc/XColumnLocate.hpp>
25 #include <com/sun/star/sdbc/XConnection.hpp>
26 #include <com/sun/star/sdbc/XParameters.hpp>
27 #include <com/sun/star/sdbc/XPreparedStatement.hpp>
28 #include <com/sun/star/sdbc/XResultSet.hpp>
29 #include <com/sun/star/sdbc/XRow.hpp>
30 #include <com/sun/star/sdbc/XStatement.hpp>
31 #include <com/sun/star/util/XCloseable.hpp>
33 using namespace ::com::sun::star
;
34 using namespace ::com::sun::star::beans
;
35 using namespace ::com::sun::star::frame
;
36 using namespace ::com::sun::star::lang
;
37 using namespace ::com::sun::star::sdb
;
38 using namespace ::com::sun::star::sdbc
;
39 using namespace ::com::sun::star::uno
;
41 void normaliseTimeValue(TimeValue
* pVal
)
43 pVal
->Seconds
+= pVal
->Nanosec
/ 1000000000;
44 pVal
->Nanosec
%= 1000000000;
47 void getTimeDifference(const TimeValue
* pTimeStart
,
48 const TimeValue
* pTimeEnd
,
49 TimeValue
* pTimeDifference
)
51 // We add 1 second to the nanoseconds to ensure that we get a positive number
52 // We have to normalise anyway so this doesn't cause any harm.
53 // (Seconds/Nanosec are both unsigned)
54 pTimeDifference
->Seconds
= pTimeEnd
->Seconds
- pTimeStart
->Seconds
- 1;
55 pTimeDifference
->Nanosec
= 1000000000 + pTimeEnd
->Nanosec
- pTimeStart
->Nanosec
;
56 normaliseTimeValue(pTimeDifference
);
59 OUString
getPrintableTimeValue(const TimeValue
* pTimeValue
)
61 return OUString::number(
62 (sal_uInt64(pTimeValue
->Seconds
) * SAL_CONST_UINT64(1000000000)
63 + sal_uInt64(pTimeValue
->Nanosec
))/ 1000000
68 * The recommended way to run this test is:
69 * 'SAL_LOG="" DBA_PERFTEST=YES make CppunitTest_dbaccess_embeddeddb_performancetest'
70 * This blocks the unnecessary exception output and show only the performance data.
72 * You also need to create the file dbacess/qa/unit/data/wordlist, this list cannot
73 * contain any unescaped apostrophes (since the words are used directly to assemble
74 * sql statement), apostrophes are escaped using a double apostrophe, i.e. ''.
75 * one easy way of generating a list is using:
76 * 'for WORD in $(aspell dump master); do echo ${WORD//\'/\'\'}; done > dbaccess/qa/unit/data/wordlist'
78 * Note that wordlist cannot have more than 220580 lines, this is due to a hard
79 * limit in our hsqldb version.
81 * Also note that this unit test "fails" when doing performance testing, this is
82 * since by default unit test output is hidden, and thus there is no way of
83 * reading the results.
85 class EmbeddedDBPerformanceTest
89 static const char our_sEnableTestEnvVar
[];
91 // We store the results and print them at the end due to the amount of warning
92 // noise present which otherwise obscures the results.
93 OUStringBuffer m_aOutputBuffer
;
95 void printTimes(const TimeValue
* pTime1
, const TimeValue
* pTime2
, const TimeValue
* pTime3
);
97 void doPerformanceTestOnODB(const OUString
& rDriverURL
,
98 const OUString
& rDBName
,
99 const bool bUsePreparedStatement
);
101 void setupTestTable(uno::Reference
< XConnection
>& xConnection
);
103 SvFileStream
*getWordListStream();
106 void performPreparedStatementInsertTest(
107 uno::Reference
< XConnection
>& xConnection
,
108 const OUString
& rDBName
);
109 void performStatementInsertTest(
110 uno::Reference
< XConnection
>& xConnection
,
111 const OUString
& rDBName
);
112 void performReadTest(
113 uno::Reference
< XConnection
>& xConnection
,
114 const OUString
& rDBName
);
116 // Perform all tests on a given DB.
121 void testPerformance();
123 CPPUNIT_TEST_SUITE(EmbeddedDBPerformanceTest
);
124 CPPUNIT_TEST(testPerformance
);
125 CPPUNIT_TEST_SUITE_END();
128 SvFileStream
* EmbeddedDBPerformanceTest::getWordListStream()
131 createFileURL("wordlist", wlPath
);
132 return new SvFileStream(wlPath
, StreamMode::READ
);
135 void EmbeddedDBPerformanceTest::printTimes(
136 const TimeValue
* pTime1
,
137 const TimeValue
* pTime2
,
138 const TimeValue
* pTime3
)
140 m_aOutputBuffer
.append(
141 getPrintableTimeValue(pTime1
) + "\t" +
142 getPrintableTimeValue(pTime2
) + "\t" +
143 getPrintableTimeValue(pTime3
) + "\t"
148 const char EmbeddedDBPerformanceTest::our_sEnableTestEnvVar
[] = "DBA_PERFTEST";
150 // TODO: we probably should create a document from scratch instead?
152 void EmbeddedDBPerformanceTest::testPerformance()
155 osl_getEnvironment(OUString(our_sEnableTestEnvVar
).pData
, &sEnabled
.pData
);
157 if (sEnabled
.isEmpty())
160 m_aOutputBuffer
.append("---------------------\n");
162 m_aOutputBuffer
.append("---------------------\n");
164 m_aOutputBuffer
.append("---------------------\n");
166 fprintf(stdout
, "Performance Test Results:\n");
167 fprintf(stdout
, "%s",
168 OUStringToOString(m_aOutputBuffer
.makeStringAndClear(),
169 RTL_TEXTENCODING_UTF8
)
173 // We want the results printed, but unit test output is only printed on failure
174 // Hence we deliberately fail the test.
175 CPPUNIT_ASSERT(false);
178 void EmbeddedDBPerformanceTest::testFirebird()
181 m_aOutputBuffer
.append("Standard Insert\n");
182 doPerformanceTestOnODB("sdbc:embedded:firebird", "Firebird", false);
183 m_aOutputBuffer
.append("PreparedStatement Insert\n");
184 doPerformanceTestOnODB("sdbc:embedded:firebird", "Firebird", true);
187 void EmbeddedDBPerformanceTest::testHSQLDB()
189 m_aOutputBuffer
.append("Standard Insert\n");
190 doPerformanceTestOnODB("sdbc:embedded:hsqldb", "HSQLDB", false);
191 m_aOutputBuffer
.append("PreparedStatement Insert\n");
192 doPerformanceTestOnODB("sdbc:embedded:hsqldb", "HSQLDB", true);
196 * Use an existing .odb to do performance tests on. The database cannot have
197 * a table of the name PFTESTTABLE.
199 void EmbeddedDBPerformanceTest::doPerformanceTestOnODB(
200 const OUString
& rDriverURL
,
201 const OUString
& rDBName
,
202 const bool bUsePreparedStatement
)
204 ::utl::TempFile aFile
;
205 aFile
.EnableKillingFile();
208 uno::Reference
< XOfficeDatabaseDocument
> xDocument(
209 m_xSFactory
->createInstance("com.sun.star.sdb.OfficeDatabaseDocument"),
211 uno::Reference
< XStorable
> xStorable(xDocument
, UNO_QUERY_THROW
);
213 uno::Reference
< XDataSource
> xDataSource
= xDocument
->getDataSource();
214 uno::Reference
< XPropertySet
> xPropertySet(xDataSource
, UNO_QUERY_THROW
);
215 xPropertySet
->setPropertyValue("URL", Any(rDriverURL
));
217 xStorable
->storeAsURL(aFile
.GetURL(), uno::Sequence
< beans::PropertyValue
>());
220 uno::Reference
< XOfficeDatabaseDocument
> xDocument(
221 loadFromDesktop(aFile
.GetURL()), UNO_QUERY_THROW
);
223 uno::Reference
< XConnection
> xConnection
=
224 getConnectionForDocument(xDocument
);
226 setupTestTable(xConnection
);
228 if (bUsePreparedStatement
)
229 performPreparedStatementInsertTest(xConnection
, rDBName
);
231 performStatementInsertTest(xConnection
, rDBName
);
233 performReadTest(xConnection
, rDBName
);
236 void EmbeddedDBPerformanceTest::setupTestTable(
237 uno::Reference
< XConnection
>& xConnection
)
239 uno::Reference
< XStatement
> xStatement
= xConnection
->createStatement();
241 // Although not strictly necessary we use quoted identifiers to reflect
242 // the fact that Base always uses quoted identifiers.
244 "CREATE TABLE \"PFTESTTABLE\" ( \"ID\" INTEGER NOT NULL PRIMARY KEY "
245 ", \"STRINGCOLUMNA\" VARCHAR (50) "
248 xConnection
->commit();
251 void EmbeddedDBPerformanceTest::performPreparedStatementInsertTest(
252 uno::Reference
< XConnection
>& xConnection
,
253 const OUString
& rDBName
)
255 uno::Reference
< XPreparedStatement
> xPreparedStatement
=
256 xConnection
->prepareStatement(
257 "INSERT INTO \"PFTESTTABLE\" ( \"ID\", "
262 uno::Reference
< XParameters
> xParameters(xPreparedStatement
, UNO_QUERY_THROW
);
264 ::boost::scoped_ptr
< SvFileStream
> pFile(getWordListStream());
269 TimeValue aStart
, aMiddle
, aEnd
;
270 osl_getSystemTime(&aStart
);
272 while (pFile
->ReadByteStringLine(aWord
, RTL_TEXTENCODING_UTF8
))
274 xParameters
->setInt(1, aID
++);
275 xParameters
->setString(2, aWord
);
276 xPreparedStatement
->execute();
278 osl_getSystemTime(&aMiddle
);
279 xConnection
->commit();
280 osl_getSystemTime(&aEnd
);
283 TimeValue aTimeInsert
, aTimeCommit
, aTimeTotal
;
284 getTimeDifference(&aStart
, &aMiddle
, &aTimeInsert
);
285 getTimeDifference(&aMiddle
, &aEnd
, &aTimeCommit
);
286 getTimeDifference(&aStart
, &aEnd
, &aTimeTotal
);
287 m_aOutputBuffer
.append("Insert: " + rDBName
+ "\n");
288 printTimes(&aTimeInsert
, &aTimeCommit
, &aTimeTotal
);
293 void EmbeddedDBPerformanceTest::performStatementInsertTest(
294 uno::Reference
< XConnection
>& xConnection
,
295 const OUString
& rDBName
)
297 uno::Reference
< XStatement
> xStatement
=
298 xConnection
->createStatement();
300 ::boost::scoped_ptr
< SvFileStream
> pFile(getWordListStream());
305 TimeValue aStart
, aMiddle
, aEnd
;
306 osl_getSystemTime(&aStart
);
308 while (pFile
->ReadByteStringLine(aWord
, RTL_TEXTENCODING_UTF8
))
311 "INSERT INTO \"PFTESTTABLE\" ( \"ID\", "
314 + OUString::number(aID
++) + ", '" + aWord
+ "' )"
317 osl_getSystemTime(&aMiddle
);
318 xConnection
->commit();
319 osl_getSystemTime(&aEnd
);
321 TimeValue aTimeInsert
, aTimeCommit
, aTimeTotal
;
322 getTimeDifference(&aStart
, &aMiddle
, &aTimeInsert
);
323 getTimeDifference(&aMiddle
, &aEnd
, &aTimeCommit
);
324 getTimeDifference(&aStart
, &aEnd
, &aTimeTotal
);
325 m_aOutputBuffer
.append("Insert: " + rDBName
+ "\n");
326 printTimes(&aTimeInsert
, &aTimeCommit
, &aTimeTotal
);
331 void EmbeddedDBPerformanceTest::performReadTest(
332 uno::Reference
< XConnection
>& xConnection
,
333 const OUString
& rDBName
)
335 uno::Reference
< XStatement
> xStatement
= xConnection
->createStatement();
337 TimeValue aStart
, aMiddle
, aEnd
;
338 osl_getSystemTime(&aStart
);
340 uno::Reference
< XResultSet
> xResults
= xStatement
->executeQuery("SELECT * FROM PFTESTTABLE");
342 osl_getSystemTime(&aMiddle
);
344 uno::Reference
< XRow
> xRow(xResults
, UNO_QUERY_THROW
);
346 while (xResults
->next())
350 osl_getSystemTime(&aEnd
);
352 TimeValue aTimeSelect
, aTimeIterate
, aTimeTotal
;
353 getTimeDifference(&aStart
, &aMiddle
, &aTimeSelect
);
354 getTimeDifference(&aMiddle
, &aEnd
, &aTimeIterate
);
355 getTimeDifference(&aStart
, &aEnd
, &aTimeTotal
);
356 m_aOutputBuffer
.append("Read from: " + rDBName
+ "\n");
357 printTimes(&aTimeSelect
, &aTimeIterate
, &aTimeTotal
);
360 CPPUNIT_TEST_SUITE_REGISTRATION(EmbeddedDBPerformanceTest
);
362 CPPUNIT_PLUGIN_IMPLEMENT();
364 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */