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"
13 #include <osl/process.h>
15 #include <rtl/ustrbuf.hxx>
16 #include <tools/stream.hxx>
17 #include <unotools/tempfile.hxx>
19 #include <com/sun/star/beans/XPropertySet.hpp>
20 #include <com/sun/star/frame/XStorable.hpp>
21 #include <com/sun/star/sdb/XOfficeDatabaseDocument.hpp>
22 #include <com/sun/star/sdbc/XConnection.hpp>
23 #include <com/sun/star/sdbc/XParameters.hpp>
24 #include <com/sun/star/sdbc/XPreparedStatement.hpp>
25 #include <com/sun/star/sdbc/XResultSet.hpp>
26 #include <com/sun/star/sdbc/XRow.hpp>
27 #include <com/sun/star/sdbc/XStatement.hpp>
29 using namespace ::com::sun::star
;
30 using namespace ::com::sun::star::beans
;
31 using namespace ::com::sun::star::frame
;
32 using namespace ::com::sun::star::lang
;
33 using namespace ::com::sun::star::sdb
;
34 using namespace ::com::sun::star::sdbc
;
35 using namespace ::com::sun::star::uno
;
37 static void normaliseTimeValue(TimeValue
* pVal
)
39 pVal
->Seconds
+= pVal
->Nanosec
/ 1000000000;
40 pVal
->Nanosec
%= 1000000000;
43 static void getTimeDifference(const TimeValue
* pTimeStart
,
44 const TimeValue
* pTimeEnd
,
45 TimeValue
* pTimeDifference
)
47 // We add 1 second to the nanoseconds to ensure that we get a positive number
48 // We have to normalise anyway so this doesn't cause any harm.
49 // (Seconds/Nanosec are both unsigned)
50 pTimeDifference
->Seconds
= pTimeEnd
->Seconds
- pTimeStart
->Seconds
- 1;
51 pTimeDifference
->Nanosec
= 1000000000 + pTimeEnd
->Nanosec
- pTimeStart
->Nanosec
;
52 normaliseTimeValue(pTimeDifference
);
55 static OUString
getPrintableTimeValue(const TimeValue
* pTimeValue
)
57 return OUString::number(
58 (sal_uInt64(pTimeValue
->Seconds
) * SAL_CONST_UINT64(1000000000)
59 + sal_uInt64(pTimeValue
->Nanosec
))/ 1000000
64 * The recommended way to run this test is:
65 * 'SAL_LOG="" DBA_PERFTEST=YES make CppunitTest_dbaccess_embeddeddb_performancetest'
66 * This blocks the unnecessary exception output and show only the performance data.
68 * You also need to create the file dbaccess/qa/unit/data/wordlist, this list cannot
69 * contain any unescaped apostrophes (since the words are used directly to assemble
70 * sql statement), apostrophes are escaped using a double apostrophe, i.e. ''.
71 * one easy way of generating a list is using:
72 * 'for WORD in $(aspell dump master); do echo ${WORD//\'/\'\'}; done > dbaccess/qa/unit/data/wordlist'
74 * Note that wordlist cannot have more than 220580 lines, this is due to a hard
75 * limit in our hsqldb version.
77 * Also note that this unit test "fails" when doing performance testing, this is
78 * since by default unit test output is hidden, and thus there is no way of
79 * reading the results.
81 class EmbeddedDBPerformanceTest
85 static const char our_sEnableTestEnvVar
[];
87 // We store the results and print them at the end due to the amount of warning
88 // noise present which otherwise obscures the results.
89 OUStringBuffer m_aOutputBuffer
;
91 void printTimes(const TimeValue
* pTime1
, const TimeValue
* pTime2
, const TimeValue
* pTime3
);
93 void doPerformanceTestOnODB(const OUString
& rDriverURL
,
94 const OUString
& rDBName
,
95 const bool bUsePreparedStatement
);
97 void setupTestTable(uno::Reference
< XConnection
> const & xConnection
);
99 SvFileStream
*getWordListStream();
102 void performPreparedStatementInsertTest(
103 uno::Reference
< XConnection
> const & xConnection
,
104 const OUString
& rDBName
);
105 void performStatementInsertTest(
106 uno::Reference
< XConnection
> const & xConnection
,
107 const OUString
& rDBName
);
108 void performReadTest(
109 uno::Reference
< XConnection
> const & xConnection
,
110 const OUString
& rDBName
);
112 // Perform all tests on a given DB.
117 void testPerformance();
119 CPPUNIT_TEST_SUITE(EmbeddedDBPerformanceTest
);
120 CPPUNIT_TEST(testPerformance
);
121 CPPUNIT_TEST_SUITE_END();
124 SvFileStream
* EmbeddedDBPerformanceTest::getWordListStream()
127 createFileURL("wordlist", wlPath
);
128 return new SvFileStream(wlPath
, StreamMode::READ
);
131 void EmbeddedDBPerformanceTest::printTimes(
132 const TimeValue
* pTime1
,
133 const TimeValue
* pTime2
,
134 const TimeValue
* pTime3
)
137 .append(getPrintableTimeValue(pTime1
)).append("\t")
138 .append(getPrintableTimeValue(pTime2
)).append("\t")
139 .append(getPrintableTimeValue(pTime3
)).append("\t")
143 const char EmbeddedDBPerformanceTest::our_sEnableTestEnvVar
[] = "DBA_PERFTEST";
145 // TODO: we probably should create a document from scratch instead?
147 void EmbeddedDBPerformanceTest::testPerformance()
150 osl_getEnvironment(OUString(our_sEnableTestEnvVar
).pData
, &sEnabled
.pData
);
152 if (sEnabled
.isEmpty())
155 m_aOutputBuffer
.append("---------------------\n");
157 m_aOutputBuffer
.append("---------------------\n");
159 m_aOutputBuffer
.append("---------------------\n");
161 fprintf(stdout
, "Performance Test Results:\n");
162 fprintf(stdout
, "%s",
163 OUStringToOString(m_aOutputBuffer
.makeStringAndClear(),
164 RTL_TEXTENCODING_UTF8
)
168 // We want the results printed, but unit test output is only printed on failure
169 // Hence we deliberately fail the test.
170 CPPUNIT_ASSERT(false);
173 void EmbeddedDBPerformanceTest::testFirebird()
176 m_aOutputBuffer
.append("Standard Insert\n");
177 doPerformanceTestOnODB("sdbc:embedded:firebird", "Firebird", false);
178 m_aOutputBuffer
.append("PreparedStatement Insert\n");
179 doPerformanceTestOnODB("sdbc:embedded:firebird", "Firebird", true);
182 void EmbeddedDBPerformanceTest::testHSQLDB()
184 m_aOutputBuffer
.append("Standard Insert\n");
185 doPerformanceTestOnODB("sdbc:embedded:hsqldb", "HSQLDB", false);
186 m_aOutputBuffer
.append("PreparedStatement Insert\n");
187 doPerformanceTestOnODB("sdbc:embedded:hsqldb", "HSQLDB", true);
191 * Use an existing .odb to do performance tests on. The database cannot have
192 * a table of the name PFTESTTABLE.
194 void EmbeddedDBPerformanceTest::doPerformanceTestOnODB(
195 const OUString
& rDriverURL
,
196 const OUString
& rDBName
,
197 const bool bUsePreparedStatement
)
199 ::utl::TempFile aFile
;
200 aFile
.EnableKillingFile();
203 uno::Reference
< XOfficeDatabaseDocument
> xDocument(
204 m_xSFactory
->createInstance("com.sun.star.sdb.OfficeDatabaseDocument"),
206 uno::Reference
< XStorable
> xStorable(xDocument
, UNO_QUERY_THROW
);
208 uno::Reference
< XDataSource
> xDataSource
= xDocument
->getDataSource();
209 uno::Reference
< XPropertySet
> xPropertySet(xDataSource
, UNO_QUERY_THROW
);
210 xPropertySet
->setPropertyValue("URL", Any(rDriverURL
));
212 xStorable
->storeAsURL(aFile
.GetURL(), uno::Sequence
< beans::PropertyValue
>());
215 uno::Reference
< XOfficeDatabaseDocument
> xDocument(
216 loadFromDesktop(aFile
.GetURL()), UNO_QUERY_THROW
);
218 uno::Reference
< XConnection
> xConnection
=
219 getConnectionForDocument(xDocument
);
221 setupTestTable(xConnection
);
223 if (bUsePreparedStatement
)
224 performPreparedStatementInsertTest(xConnection
, rDBName
);
226 performStatementInsertTest(xConnection
, rDBName
);
228 performReadTest(xConnection
, rDBName
);
231 void EmbeddedDBPerformanceTest::setupTestTable(
232 uno::Reference
< XConnection
> const & xConnection
)
234 uno::Reference
< XStatement
> xStatement
= xConnection
->createStatement();
236 // Although not strictly necessary we use quoted identifiers to reflect
237 // the fact that Base always uses quoted identifiers.
239 "CREATE TABLE \"PFTESTTABLE\" ( \"ID\" INTEGER NOT NULL PRIMARY KEY "
240 ", \"STRINGCOLUMNA\" VARCHAR (50) "
243 xConnection
->commit();
246 void EmbeddedDBPerformanceTest::performPreparedStatementInsertTest(
247 uno::Reference
< XConnection
> const & xConnection
,
248 const OUString
& rDBName
)
250 uno::Reference
< XPreparedStatement
> xPreparedStatement
=
251 xConnection
->prepareStatement(
252 "INSERT INTO \"PFTESTTABLE\" ( \"ID\", "
257 uno::Reference
< XParameters
> xParameters(xPreparedStatement
, UNO_QUERY_THROW
);
259 std::unique_ptr
< SvFileStream
> pFile(getWordListStream());
264 TimeValue aStart
, aMiddle
, aEnd
;
265 osl_getSystemTime(&aStart
);
267 while (pFile
->ReadByteStringLine(aWord
, RTL_TEXTENCODING_UTF8
))
269 xParameters
->setInt(1, aID
++);
270 xParameters
->setString(2, aWord
);
271 xPreparedStatement
->execute();
273 osl_getSystemTime(&aMiddle
);
274 xConnection
->commit();
275 osl_getSystemTime(&aEnd
);
278 TimeValue aTimeInsert
, aTimeCommit
, aTimeTotal
;
279 getTimeDifference(&aStart
, &aMiddle
, &aTimeInsert
);
280 getTimeDifference(&aMiddle
, &aEnd
, &aTimeCommit
);
281 getTimeDifference(&aStart
, &aEnd
, &aTimeTotal
);
282 m_aOutputBuffer
.append("Insert: ").append(rDBName
).append("\n");
283 printTimes(&aTimeInsert
, &aTimeCommit
, &aTimeTotal
);
288 void EmbeddedDBPerformanceTest::performStatementInsertTest(
289 uno::Reference
< XConnection
> const & xConnection
,
290 const OUString
& rDBName
)
292 uno::Reference
< XStatement
> xStatement
=
293 xConnection
->createStatement();
295 std::unique_ptr
< SvFileStream
> pFile(getWordListStream());
300 TimeValue aStart
, aMiddle
, aEnd
;
301 osl_getSystemTime(&aStart
);
303 while (pFile
->ReadByteStringLine(aWord
, RTL_TEXTENCODING_UTF8
))
306 "INSERT INTO \"PFTESTTABLE\" ( \"ID\", "
309 + OUString::number(aID
++) + ", '" + aWord
+ "' )"
312 osl_getSystemTime(&aMiddle
);
313 xConnection
->commit();
314 osl_getSystemTime(&aEnd
);
316 TimeValue aTimeInsert
, aTimeCommit
, aTimeTotal
;
317 getTimeDifference(&aStart
, &aMiddle
, &aTimeInsert
);
318 getTimeDifference(&aMiddle
, &aEnd
, &aTimeCommit
);
319 getTimeDifference(&aStart
, &aEnd
, &aTimeTotal
);
320 m_aOutputBuffer
.append("Insert: ").append(rDBName
).append("\n");
321 printTimes(&aTimeInsert
, &aTimeCommit
, &aTimeTotal
);
326 void EmbeddedDBPerformanceTest::performReadTest(
327 uno::Reference
< XConnection
> const & xConnection
,
328 const OUString
& rDBName
)
330 uno::Reference
< XStatement
> xStatement
= xConnection
->createStatement();
332 TimeValue aStart
, aMiddle
, aEnd
;
333 osl_getSystemTime(&aStart
);
335 uno::Reference
< XResultSet
> xResults
= xStatement
->executeQuery("SELECT * FROM PFTESTTABLE");
337 osl_getSystemTime(&aMiddle
);
339 uno::Reference
< XRow
> xRow(xResults
, UNO_QUERY_THROW
);
341 while (xResults
->next())
345 osl_getSystemTime(&aEnd
);
347 TimeValue aTimeSelect
, aTimeIterate
, aTimeTotal
;
348 getTimeDifference(&aStart
, &aMiddle
, &aTimeSelect
);
349 getTimeDifference(&aMiddle
, &aEnd
, &aTimeIterate
);
350 getTimeDifference(&aStart
, &aEnd
, &aTimeTotal
);
351 m_aOutputBuffer
.append("Read from: ").append(rDBName
).append("\n");
352 printTimes(&aTimeSelect
, &aTimeIterate
, &aTimeTotal
);
355 CPPUNIT_TEST_SUITE_REGISTRATION(EmbeddedDBPerformanceTest
);
357 CPPUNIT_PLUGIN_IMPLEMENT();
359 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */