Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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 : */
9 :
10 : #include <assert.h>
11 : #include <stdio.h>
12 : #include <string.h>
13 :
14 : #include <vector>
15 : #include <osl/time.h>
16 :
17 : #define LOK_USE_UNSTABLE_API
18 :
19 : #include <LibreOfficeKit/LibreOfficeKitInit.h>
20 : #include <LibreOfficeKit/LibreOfficeKit.hxx>
21 :
22 : using namespace lok;
23 :
24 0 : static int help()
25 : {
26 0 : fprintf( stderr, "Usage: tilebench <absolute-path-to-libreoffice-install> [path to document]\n" );
27 0 : fprintf( stderr, "renders a selection of small tiles from the document, checksums them and times the process\n" );
28 0 : return 1;
29 : }
30 :
31 0 : static double getTimeNow()
32 : {
33 : TimeValue aValue;
34 0 : osl_getSystemTime(&aValue);
35 0 : return (double)aValue.Seconds +
36 0 : (double)aValue.Nanosec / (1000*1000*1000);
37 : }
38 :
39 0 : int main( int argc, char* argv[] )
40 : {
41 : struct TimeRecord {
42 : const char *mpName;
43 : double mfTime;
44 :
45 0 : TimeRecord() : mpName(NULL), mfTime(getTimeNow()) { }
46 0 : explicit TimeRecord(const char *pName) :
47 0 : mpName(pName ), mfTime(getTimeNow()) { }
48 : };
49 0 : std::vector< TimeRecord > aTimes;
50 0 : if( argc < 2 ||
51 0 : ( argc > 1 && ( !strcmp( argv[1], "--help" ) || !strcmp( argv[1], "-h" ) ) ) )
52 0 : return help();
53 :
54 0 : if ( argv[1][0] != '/' )
55 : {
56 0 : fprintf(stderr, "Absolute path required to libreoffice install\n");
57 0 : return 1;
58 : }
59 :
60 0 : aTimes.push_back(TimeRecord("initialization"));
61 0 : Office *pOffice = lok_cpp_init(argv[1]);
62 0 : aTimes.push_back(TimeRecord());
63 :
64 0 : if (argv[2] != NULL)
65 : {
66 0 : aTimes.push_back(TimeRecord("load document"));
67 0 : Document *pDocument(pOffice->documentLoad(argv[2]));
68 0 : aTimes.push_back(TimeRecord());
69 :
70 0 : aTimes.push_back(TimeRecord("getparts"));
71 0 : int nParts = pDocument->getParts();
72 0 : aTimes.push_back(TimeRecord());
73 :
74 0 : aTimes.push_back(TimeRecord("get size of parts"));
75 0 : for (int nPart = 0; nPart < nParts; nPart++)
76 : {
77 0 : char* pName = pDocument->getPartName(nPart);
78 0 : pDocument->setPart(nPart);
79 0 : long nWidth = 0, nHeight = 0;
80 0 : pDocument->getDocumentSize(&nWidth, &nHeight);
81 0 : fprintf (stderr, " '%s' -> %ld, %ld\n", pName, nWidth, nHeight);
82 0 : free (pName);
83 : }
84 0 : aTimes.push_back(TimeRecord());
85 :
86 : unsigned char pPixels[256*256*4];
87 0 : for (int nPart = 0; nPart < nParts; nPart++)
88 : {
89 : {
90 0 : char* pName = pDocument->getPartName(nPart);
91 0 : fprintf (stderr, "render '%s'\n", pName);
92 0 : free (pName);
93 : }
94 0 : pDocument->setPart(nPart);
95 0 : long nWidth = 0, nHeight = 0;
96 0 : pDocument->getDocumentSize(&nWidth, &nHeight);
97 :
98 : { // whole document
99 0 : aTimes.push_back(TimeRecord("render whole document"));
100 0 : int nRowStride = 256;
101 : pDocument->paintTile(pPixels, 256, 256, &nRowStride,
102 0 : 0, 0, nWidth, nHeight); // not square
103 0 : aTimes.push_back(TimeRecord());
104 : }
105 :
106 : { // 1:1
107 0 : aTimes.push_back(TimeRecord("render sub-region at 1:1"));
108 0 : int nTiles = 0;
109 0 : int nSplit = nWidth / 4;
110 0 : for (int nX = 0; nX < 4; nX++)
111 : {
112 0 : for (int nY = 0; nY < nHeight / nSplit; nY++)
113 : {
114 0 : int nRowStride = 256;
115 0 : int nTilePosX = nX * nSplit;
116 0 : int nTilePosY = nY * nSplit;
117 : pDocument->paintTile(pPixels, 256, 256, &nRowStride,
118 0 : nTilePosX, nTilePosY, 256, 256);
119 0 : nTiles++;
120 : fprintf (stderr, " rendered tile %d at %d, %d\n",
121 0 : nTiles, nTilePosX, nTilePosY);
122 : }
123 : }
124 0 : aTimes.push_back(TimeRecord());
125 : }
126 :
127 : { // scaled
128 0 : aTimes.push_back(TimeRecord("render sub-regions at scale"));
129 0 : int nTiles = 0;
130 0 : int nSplit = nWidth / 4;
131 0 : for (int nX = 0; nX < 4; nX++)
132 : {
133 0 : for (int nY = 0; nY < nHeight / nSplit; nY++)
134 : {
135 0 : int nRowStride = 256;
136 0 : int nTilePosX = nX * nSplit;
137 0 : int nTilePosY = nY * nSplit;
138 : pDocument->paintTile(pPixels, 256, 256, &nRowStride,
139 0 : nTilePosX, nTilePosY, nSplit, nSplit);
140 0 : nTiles++;
141 : fprintf (stderr, " rendered tile %d at %d, %d\n",
142 0 : nTiles, nTilePosX, nTilePosY);
143 : }
144 : }
145 0 : aTimes.push_back(TimeRecord());
146 : }
147 : }
148 :
149 0 : aTimes.push_back(TimeRecord("destroy document"));
150 0 : delete pDocument;
151 0 : aTimes.push_back(TimeRecord());
152 : }
153 :
154 0 : delete pOffice;
155 :
156 0 : double nTotal = 0.0;
157 0 : fprintf (stderr, "profile run:\n");
158 0 : for (size_t i = 0; i < aTimes.size() - 1; i++)
159 : {
160 0 : double nDelta = aTimes[i+1].mfTime - aTimes[i].mfTime;
161 0 : fprintf (stderr, " %s - %2.4f(ms)\n", aTimes[i].mpName, nDelta * 1000.0);
162 0 : if (aTimes[i+1].mpName == NULL)
163 0 : i++; // skip it.
164 0 : nTotal += nDelta;
165 : }
166 0 : fprintf (stderr, "Total: %2.4f(s)\n", nTotal);
167 0 : return 0;
168 : }
169 :
170 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|