xref: /AOO41X/main/qadevOOo/runner/graphical/PerformanceContainer.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package graphical;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import java.io.File;
31*cdf0e10cSrcweir // import java.io.FileWriter;
32*cdf0e10cSrcweir // import java.io.OutputStream;
33*cdf0e10cSrcweir // import java.io.PrintStream;
34*cdf0e10cSrcweir import java.io.PrintStream;
35*cdf0e10cSrcweir import java.io.RandomAccessFile;
36*cdf0e10cSrcweir // import java.io.StringWriter;
37*cdf0e10cSrcweir // import java.lang.Double;
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir public class PerformanceContainer /* extends *//* implements */ {
41*cdf0e10cSrcweir     private long m_nStartTime;
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir     /*
44*cdf0e10cSrcweir       simple helper functions to start/stop a timer, to know how long a process need in milliseconds
45*cdf0e10cSrcweir      */
46*cdf0e10cSrcweir     public long getStartTime()
47*cdf0e10cSrcweir         {
48*cdf0e10cSrcweir             return System.currentTimeMillis();
49*cdf0e10cSrcweir         }
50*cdf0e10cSrcweir     public void setStartTime(long _nStartTime)
51*cdf0e10cSrcweir         {
52*cdf0e10cSrcweir             m_nStartTime = _nStartTime;
53*cdf0e10cSrcweir         }
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir     /*
56*cdf0e10cSrcweir       return the time, which is done until last startTime()
57*cdf0e10cSrcweir      */
58*cdf0e10cSrcweir     private long meanTime(long _nCurrentTimer)
59*cdf0e10cSrcweir         {
60*cdf0e10cSrcweir             if (_nCurrentTimer == 0)
61*cdf0e10cSrcweir             {
62*cdf0e10cSrcweir                 GlobalLogWriter.println("Forgotten to initialise a start timer.");
63*cdf0e10cSrcweir                 return 0;
64*cdf0e10cSrcweir             }
65*cdf0e10cSrcweir             long nMeanTime = System.currentTimeMillis();
66*cdf0e10cSrcweir             return nMeanTime - _nCurrentTimer;
67*cdf0e10cSrcweir         }
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir     /*
70*cdf0e10cSrcweir       public long stopTimer()
71*cdf0e10cSrcweir         {
72*cdf0e10cSrcweir             if (m_nStartTime == 0)
73*cdf0e10cSrcweir             {
74*cdf0e10cSrcweir                 System.out.println("Forgotten to initialise start timer.");
75*cdf0e10cSrcweir                 return 0;
76*cdf0e10cSrcweir             }
77*cdf0e10cSrcweir             long nStopTime = System.currentTimeMillis();
78*cdf0e10cSrcweir             return nStopTime - m_nStartTime;
79*cdf0e10cSrcweir         }
80*cdf0e10cSrcweir     */
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir     final static int Load = 0;
83*cdf0e10cSrcweir     final static int Store = 1;
84*cdf0e10cSrcweir     final static int Print = 2;
85*cdf0e10cSrcweir     final static int OfficeStart = 3;
86*cdf0e10cSrcweir     final static int StoreAsPDF = 4;
87*cdf0e10cSrcweir     final static int OfficeStop = 5;
88*cdf0e10cSrcweir     final static int AllTime = 6;
89*cdf0e10cSrcweir     final static int LAST_VALUE = 7; // THIS MUST BE ALWAYS THE LAST AND THE BIGGEST VALUE!
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir     private long m_nTime[];
92*cdf0e10cSrcweir     private String m_sMSOfficeVersion;
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir     public PerformanceContainer()
95*cdf0e10cSrcweir         {
96*cdf0e10cSrcweir             m_nTime = new long[LAST_VALUE];
97*cdf0e10cSrcweir             // @todo: is this need?
98*cdf0e10cSrcweir             for (int i=0;i<LAST_VALUE;i++)
99*cdf0e10cSrcweir             {
100*cdf0e10cSrcweir                 m_nTime[i] = 0;
101*cdf0e10cSrcweir             }
102*cdf0e10cSrcweir         }
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir     public void setTime(int _nIndex, long _nValue)
105*cdf0e10cSrcweir         {
106*cdf0e10cSrcweir             m_nTime[_nIndex] = _nValue;
107*cdf0e10cSrcweir         }
108*cdf0e10cSrcweir     public long getTime(int _nIndex)
109*cdf0e10cSrcweir         {
110*cdf0e10cSrcweir             return m_nTime[_nIndex];
111*cdf0e10cSrcweir         }
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir     public void startTime(int _nIndex)
114*cdf0e10cSrcweir         {
115*cdf0e10cSrcweir             m_nTime[_nIndex] = getStartTime();
116*cdf0e10cSrcweir         }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir     public void stopTime(int _nIndex)
119*cdf0e10cSrcweir         {
120*cdf0e10cSrcweir             m_nTime[_nIndex] = meanTime(m_nTime[_nIndex]);
121*cdf0e10cSrcweir         }
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir     public String getMSOfficeVersion()
124*cdf0e10cSrcweir     {
125*cdf0e10cSrcweir         return m_sMSOfficeVersion;
126*cdf0e10cSrcweir     }
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir     public void print(PrintStream out)
129*cdf0e10cSrcweir         {
130*cdf0e10cSrcweir             // String ls = System.getProperty("line.separator");
131*cdf0e10cSrcweir             // out.
132*cdf0e10cSrcweir             out.println("loadtime="        + String.valueOf(m_nTime[ Load ]));
133*cdf0e10cSrcweir             out.println("storetime="       + String.valueOf(m_nTime[ Store ]));
134*cdf0e10cSrcweir             out.println("printtime="       + String.valueOf(m_nTime[ Print ]));
135*cdf0e10cSrcweir             out.println("officestarttime=" + String.valueOf(m_nTime[ OfficeStart ]));
136*cdf0e10cSrcweir             out.println("officestoptime="  + String.valueOf(m_nTime[ OfficeStop ]));
137*cdf0e10cSrcweir             out.println("storeaspdftime="  + String.valueOf(m_nTime[ StoreAsPDF ]));
138*cdf0e10cSrcweir             out.println("alltime="         + String.valueOf(m_nTime[ AllTime ]));
139*cdf0e10cSrcweir         }
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir     public void print(IniFile _aIniFile, String _sSection)
142*cdf0e10cSrcweir         {
143*cdf0e10cSrcweir             // String ls = System.getProperty("line.separator");
144*cdf0e10cSrcweir             // out.
145*cdf0e10cSrcweir             _aIniFile.insertValue(_sSection, "loadtime"        , String.valueOf(m_nTime[ Load ]));
146*cdf0e10cSrcweir             _aIniFile.insertValue(_sSection, "storetime"       , String.valueOf(m_nTime[ Store ]));
147*cdf0e10cSrcweir             _aIniFile.insertValue(_sSection, "printtime"       , String.valueOf(m_nTime[ Print ]));
148*cdf0e10cSrcweir             _aIniFile.insertValue(_sSection, "officestarttime" , String.valueOf(m_nTime[ OfficeStart ]));
149*cdf0e10cSrcweir             _aIniFile.insertValue(_sSection, "officestoptime"  , String.valueOf(m_nTime[ OfficeStop ]));
150*cdf0e10cSrcweir             _aIniFile.insertValue(_sSection, "storeaspdftime"  , String.valueOf(m_nTime[ StoreAsPDF ]));
151*cdf0e10cSrcweir             _aIniFile.insertValue(_sSection, "alltime"         , String.valueOf(m_nTime[ AllTime ]));
152*cdf0e10cSrcweir         }
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir     public static double stringToDouble(String _sStr)
155*cdf0e10cSrcweir         {
156*cdf0e10cSrcweir             double nValue = 0;
157*cdf0e10cSrcweir             try
158*cdf0e10cSrcweir             {
159*cdf0e10cSrcweir                 nValue = Double.parseDouble( _sStr );
160*cdf0e10cSrcweir             }
161*cdf0e10cSrcweir             catch (NumberFormatException e)
162*cdf0e10cSrcweir             {
163*cdf0e10cSrcweir                 GlobalLogWriter.println("Can't convert string to double " + _sStr);
164*cdf0e10cSrcweir             }
165*cdf0e10cSrcweir             return nValue;
166*cdf0e10cSrcweir         }
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir     public static long secondsToMilliSeconds(double _nSeconds)
169*cdf0e10cSrcweir         {
170*cdf0e10cSrcweir             return (long)(_nSeconds * 1000.0);
171*cdf0e10cSrcweir         }
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir     /*
174*cdf0e10cSrcweir       Helper function, which read some values from a given file
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir       sample of wordinfofile
177*cdf0e10cSrcweir     name=c:\doc-pool\wntmsci\samples\msoffice\word\LineSpacing.doc
178*cdf0e10cSrcweir     WordVersion=11.0
179*cdf0e10cSrcweir     WordStartTime=0.340490102767944
180*cdf0e10cSrcweir     WordLoadTime=0.650935888290405
181*cdf0e10cSrcweir     WordPrintTime=0.580835103988647
182*cdf0e10cSrcweir      */
183*cdf0e10cSrcweir     public void readWordValuesFromFile(String sFilename)
184*cdf0e10cSrcweir         {
185*cdf0e10cSrcweir             File aFile = new File(sFilename);
186*cdf0e10cSrcweir             if (! aFile.exists())
187*cdf0e10cSrcweir             {
188*cdf0e10cSrcweir                 GlobalLogWriter.println("couldn't find file " + sFilename);
189*cdf0e10cSrcweir                 return;
190*cdf0e10cSrcweir             }
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir             RandomAccessFile aRandomAccessFile = null;
193*cdf0e10cSrcweir             try
194*cdf0e10cSrcweir             {
195*cdf0e10cSrcweir                 aRandomAccessFile = new RandomAccessFile(aFile,"r");
196*cdf0e10cSrcweir                 String sLine = "";
197*cdf0e10cSrcweir                 while (sLine != null)
198*cdf0e10cSrcweir                 {
199*cdf0e10cSrcweir                     sLine = aRandomAccessFile.readLine();
200*cdf0e10cSrcweir                     if ( (sLine != null) &&
201*cdf0e10cSrcweir 						 (! (sLine.length() < 2) ) &&
202*cdf0e10cSrcweir 						 (! sLine.startsWith("#")))
203*cdf0e10cSrcweir 					{
204*cdf0e10cSrcweir                         if (sLine.startsWith("WordStartTime="))
205*cdf0e10cSrcweir                         {
206*cdf0e10cSrcweir                             String sTime = sLine.substring(14);
207*cdf0e10cSrcweir                             m_nTime[OfficeStart] = secondsToMilliSeconds(stringToDouble(sTime));
208*cdf0e10cSrcweir                         }
209*cdf0e10cSrcweir                         else if (sLine.startsWith("WordLoadTime="))
210*cdf0e10cSrcweir                         {
211*cdf0e10cSrcweir                             String sTime = sLine.substring(13);
212*cdf0e10cSrcweir                             m_nTime[Load] = secondsToMilliSeconds(stringToDouble(sTime));
213*cdf0e10cSrcweir                         }
214*cdf0e10cSrcweir                         else if (sLine.startsWith("WordPrintTime="))
215*cdf0e10cSrcweir                         {
216*cdf0e10cSrcweir                             String sTime = sLine.substring(14);
217*cdf0e10cSrcweir                             m_nTime[Print] = secondsToMilliSeconds(stringToDouble(sTime));
218*cdf0e10cSrcweir                         }
219*cdf0e10cSrcweir                         else if (sLine.startsWith("WordVersion="))
220*cdf0e10cSrcweir                         {
221*cdf0e10cSrcweir                             String sMSOfficeVersion = sLine.substring(12);
222*cdf0e10cSrcweir                             m_sMSOfficeVersion = "Word:" + sMSOfficeVersion;
223*cdf0e10cSrcweir                         }
224*cdf0e10cSrcweir                         else if (sLine.startsWith("ExcelVersion="))
225*cdf0e10cSrcweir                         {
226*cdf0e10cSrcweir                             String sMSOfficeVersion = sLine.substring(13);
227*cdf0e10cSrcweir                             m_sMSOfficeVersion = "Excel:" + sMSOfficeVersion;
228*cdf0e10cSrcweir                         }
229*cdf0e10cSrcweir                         else if (sLine.startsWith("PowerPointVersion="))
230*cdf0e10cSrcweir                         {
231*cdf0e10cSrcweir                             String sMSOfficeVersion = sLine.substring(18);
232*cdf0e10cSrcweir                             m_sMSOfficeVersion = "PowerPoint:" + sMSOfficeVersion;
233*cdf0e10cSrcweir                         }
234*cdf0e10cSrcweir 					}
235*cdf0e10cSrcweir 				}
236*cdf0e10cSrcweir             }
237*cdf0e10cSrcweir             catch (java.io.FileNotFoundException fne)
238*cdf0e10cSrcweir             {
239*cdf0e10cSrcweir                 GlobalLogWriter.println("couldn't open file " + sFilename);
240*cdf0e10cSrcweir                 GlobalLogWriter.println("Message: " + fne.getMessage());
241*cdf0e10cSrcweir             }
242*cdf0e10cSrcweir             catch (java.io.IOException ie)
243*cdf0e10cSrcweir             {
244*cdf0e10cSrcweir                 GlobalLogWriter.println("Exception while reading file " + sFilename);
245*cdf0e10cSrcweir                 GlobalLogWriter.println("Message: " + ie.getMessage());
246*cdf0e10cSrcweir             }
247*cdf0e10cSrcweir             try
248*cdf0e10cSrcweir             {
249*cdf0e10cSrcweir                 aRandomAccessFile.close();
250*cdf0e10cSrcweir             }
251*cdf0e10cSrcweir             catch (java.io.IOException ie)
252*cdf0e10cSrcweir             {
253*cdf0e10cSrcweir                 GlobalLogWriter.println("Couldn't close file " + sFilename);
254*cdf0e10cSrcweir                 GlobalLogWriter.println("Message: " + ie.getMessage());
255*cdf0e10cSrcweir             }
256*cdf0e10cSrcweir         }
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir //    public static void main(String[] args)
259*cdf0e10cSrcweir //    {
260*cdf0e10cSrcweir //        BorderRemover a = new BorderRemover();
261*cdf0e10cSrcweir //        try
262*cdf0e10cSrcweir //        {
263*cdf0e10cSrcweir //            a.createNewImageWithoutBorder(args[0], args[1]);
264*cdf0e10cSrcweir //        }
265*cdf0e10cSrcweir //        catch(java.io.IOException e)
266*cdf0e10cSrcweir //        {
267*cdf0e10cSrcweir //            System.out.println("Exception caught.");
268*cdf0e10cSrcweir //        }
269*cdf0e10cSrcweir //
270*cdf0e10cSrcweir //    }
271*cdf0e10cSrcweir }
272