lok: Don't attempt to select the exact text after a failed search.
[LibreOffice.git] / libreofficekit / qa / tilebench / tilebench.cxx
blobe72c730a6dfe40092e0e1b69fa54720024e2fc46
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #include <assert.h>
11 #include <stdio.h>
12 #include <string.h>
14 #include <vector>
15 #include <osl/time.h>
17 #define LOK_USE_UNSTABLE_API
19 #include <LibreOfficeKit/LibreOfficeKitInit.h>
20 #include <LibreOfficeKit/LibreOfficeKit.hxx>
22 using namespace lok;
24 static int help()
26 fprintf( stderr, "Usage: tilebench <absolute-path-to-libreoffice-install> [path to document]\n" );
27 fprintf( stderr, "renders a selection of small tiles from the document, checksums them and times the process\n" );
28 return 1;
31 static double getTimeNow()
33 TimeValue aValue;
34 osl_getSystemTime(&aValue);
35 return (double)aValue.Seconds +
36 (double)aValue.Nanosec / (1000*1000*1000);
39 int main( int argc, char* argv[] )
41 struct TimeRecord {
42 const char *mpName;
43 double mfTime;
45 TimeRecord() : mpName(NULL), mfTime(getTimeNow()) { }
46 explicit TimeRecord(const char *pName) :
47 mpName(pName ), mfTime(getTimeNow()) { }
49 std::vector< TimeRecord > aTimes;
50 if( argc < 2 ||
51 ( argc > 1 && ( !strcmp( argv[1], "--help" ) || !strcmp( argv[1], "-h" ) ) ) )
52 return help();
54 if ( argv[1][0] != '/' )
56 fprintf(stderr, "Absolute path required to libreoffice install\n");
57 return 1;
60 aTimes.push_back(TimeRecord("initialization"));
61 // coverity[tainted_string] - build time test tool
62 Office *pOffice = lok_cpp_init(argv[1]);
63 aTimes.push_back(TimeRecord());
65 if (argv[2] != NULL)
67 aTimes.push_back(TimeRecord("load document"));
68 Document *pDocument(pOffice->documentLoad(argv[2]));
69 aTimes.push_back(TimeRecord());
71 aTimes.push_back(TimeRecord("getparts"));
72 int nParts = pDocument->getParts();
73 aTimes.push_back(TimeRecord());
75 aTimes.push_back(TimeRecord("get size of parts"));
76 for (int nPart = 0; nPart < nParts; nPart++)
78 char* pName = pDocument->getPartName(nPart);
79 pDocument->setPart(nPart);
80 long nWidth = 0, nHeight = 0;
81 pDocument->getDocumentSize(&nWidth, &nHeight);
82 fprintf (stderr, " '%s' -> %ld, %ld\n", pName, nWidth, nHeight);
83 free (pName);
85 aTimes.push_back(TimeRecord());
87 unsigned char pPixels[256*256*4];
88 for (int nPart = 0; nPart < nParts; nPart++)
91 char* pName = pDocument->getPartName(nPart);
92 fprintf (stderr, "render '%s'\n", pName);
93 free (pName);
95 pDocument->setPart(nPart);
96 long nWidth = 0, nHeight = 0;
97 pDocument->getDocumentSize(&nWidth, &nHeight);
99 { // whole document
100 aTimes.push_back(TimeRecord("render whole document"));
101 pDocument->paintTile(pPixels, 256, 256,
102 0, 0, nWidth, nHeight); // not square
103 aTimes.push_back(TimeRecord());
106 { // 1:1
107 aTimes.push_back(TimeRecord("render sub-region at 1:1"));
108 int nTiles = 0;
109 int nSplit = nWidth / 4;
110 for (int nX = 0; nX < 4; nX++)
112 for (int nY = 0; nY < nHeight / nSplit; nY++)
114 int nTilePosX = nX * nSplit;
115 int nTilePosY = nY * nSplit;
116 pDocument->paintTile(pPixels, 256, 256,
117 nTilePosX, nTilePosY, 256, 256);
118 nTiles++;
119 fprintf (stderr, " rendered tile %d at %d, %d\n",
120 nTiles, nTilePosX, nTilePosY);
123 aTimes.push_back(TimeRecord());
126 { // scaled
127 aTimes.push_back(TimeRecord("render sub-regions at scale"));
128 int nTiles = 0;
129 int nSplit = nWidth / 4;
130 for (int nX = 0; nX < 4; nX++)
132 for (int nY = 0; nY < nHeight / nSplit; nY++)
134 int nTilePosX = nX * nSplit;
135 int nTilePosY = nY * nSplit;
136 pDocument->paintTile(pPixels, 256, 256,
137 nTilePosX, nTilePosY, nSplit, nSplit);
138 nTiles++;
139 fprintf (stderr, " rendered tile %d at %d, %d\n",
140 nTiles, nTilePosX, nTilePosY);
143 aTimes.push_back(TimeRecord());
147 aTimes.push_back(TimeRecord("destroy document"));
148 delete pDocument;
149 aTimes.push_back(TimeRecord());
152 delete pOffice;
154 double nTotal = 0.0;
155 fprintf (stderr, "profile run:\n");
156 for (size_t i = 0; i < aTimes.size() - 1; i++)
158 double nDelta = aTimes[i+1].mfTime - aTimes[i].mfTime;
159 fprintf (stderr, " %s - %2.4f(ms)\n", aTimes[i].mpName, nDelta * 1000.0);
160 if (aTimes[i+1].mpName == NULL)
161 i++; // skip it.
162 nTotal += nDelta;
164 fprintf (stderr, "Total: %2.4f(s)\n", nTotal);
165 return 0;
168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */