1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Perf log header. |
|
15 * |
|
16 */ |
|
17 |
|
18 #ifndef CMULPERF_H |
|
19 #define CMULPERF_H |
|
20 |
|
21 // INCLUDES |
|
22 #include <e32debug.h> |
|
23 #include <e32base.h> |
|
24 #include <e32std.h> |
|
25 #include <hal.h> |
|
26 #include <e32cmn.h> |
|
27 #include <flogger.h> |
|
28 #include <f32file.h> |
|
29 |
|
30 _LIT(KLogFileName,"mulperf.log"); |
|
31 _LIT(KLogFileDir,"mullogs"); |
|
32 _LIT(KLogFileRootDir,"c:\\logs\\"); |
|
33 |
|
34 /** |
|
35 * @class CMulPerf |
|
36 * |
|
37 * Provides API to measure performance (memory and time). Results are saved to a file. |
|
38 * |
|
39 * @lib duiengine.lib |
|
40 * @since Series 60 5.0 |
|
41 */ |
|
42 class CMulPerf : public CBase |
|
43 { |
|
44 public: |
|
45 /** |
|
46 * Struct which holds data before measuring is started. |
|
47 */ |
|
48 struct TPerfData |
|
49 { |
|
50 /** |
|
51 * Time when measure starts |
|
52 */ |
|
53 TTime iStartTime; |
|
54 |
|
55 /** |
|
56 * Memory when measure starts |
|
57 */ |
|
58 TInt iStartMemory; |
|
59 }; |
|
60 |
|
61 public: // Constructors and destructor |
|
62 /** |
|
63 * Constructor. |
|
64 */ |
|
65 static CMulPerf* NewL(); |
|
66 |
|
67 /** |
|
68 * Destructor. |
|
69 */ |
|
70 virtual ~CMulPerf(); |
|
71 |
|
72 public: // New functions |
|
73 /** |
|
74 * Prints current amount of memory to file. |
|
75 * |
|
76 * @param aMsg User defined message which is printed before amount of memory |
|
77 */ |
|
78 inline static void PrintUserMem(const TDesC& aMsg); |
|
79 |
|
80 /** |
|
81 * Saves current amount of memory and time and returns those to user. |
|
82 * |
|
83 * @param aMsg User defined message. |
|
84 * @return TPerfData which holds current memory consumption and time. |
|
85 */ |
|
86 inline static TPerfData* StartTestCase(const TDesC& aMsg = KNullDesC); |
|
87 |
|
88 /** |
|
89 * Reads current amount of memory and time and calculates performance using |
|
90 * values got from StartTestCase. |
|
91 * |
|
92 * @param aData Data which has got when calling StartTestCase. |
|
93 * @param aMsg User defined message. |
|
94 */ |
|
95 inline static void StopTestCase(TPerfData* aData, const TDesC& aMsg = KNullDesC); |
|
96 |
|
97 /** |
|
98 * Create file where results are written to. |
|
99 */ |
|
100 inline static void CreateLogFile(); |
|
101 private: |
|
102 /** |
|
103 * Internal constructor. |
|
104 */ |
|
105 CMulPerf(); |
|
106 |
|
107 /** |
|
108 * Internal constructor. |
|
109 */ |
|
110 void ConstructL(); |
|
111 }; |
|
112 |
|
113 inline void CMulPerf::PrintUserMem(const TDesC& aMsg) |
|
114 { |
|
115 TBuf<512> buffer; |
|
116 TInt freeRAM; |
|
117 HAL::Get(HALData::EMemoryRAMFree, freeRAM); |
|
118 RHeap heap = User::Heap(); |
|
119 heap.Open(); |
|
120 TInt _size = heap.Size(); |
|
121 TInt largest = 0; |
|
122 TInt available = heap.Available(largest); |
|
123 heap.Close(); |
|
124 _LIT( KMemoryFormat, "FreeRAM: %d kB, User: - heap %d kB, available %d kB, largest block %d kB" ); |
|
125 buffer.Format(KMemoryFormat, freeRAM / 1024, _size / 1024, available / 1024, largest / 1024); |
|
126 CreateLogFile(); |
|
127 //Write logs into file |
|
128 RFileLogger::WriteFormat( KLogFileDir,KLogFileName,EFileLoggingModeAppend,_L("RAM - %S - %S"), &aMsg, &buffer); |
|
129 } |
|
130 |
|
131 inline CMulPerf::TPerfData* CMulPerf::StartTestCase(const TDesC& aMsg) |
|
132 { |
|
133 CreateLogFile(); |
|
134 //Write logs into file |
|
135 RFileLogger::WriteFormat(KLogFileDir,KLogFileName,EFileLoggingModeAppend,_L("Test case %S starts"), &aMsg); |
|
136 CMulPerf::TPerfData* data = new (ELeave) TPerfData; |
|
137 TTime readyTime; |
|
138 data->iStartTime.HomeTime(); |
|
139 User::AllocSize(data->iStartMemory); |
|
140 return data; |
|
141 } |
|
142 |
|
143 inline void CMulPerf::StopTestCase(CMulPerf::TPerfData* aData, const TDesC& aMsg) |
|
144 { |
|
145 if (!aData) |
|
146 { |
|
147 return; |
|
148 } |
|
149 TTime readyTime; |
|
150 readyTime.HomeTime(); |
|
151 TTimeIntervalMicroSeconds delay = readyTime.MicroSecondsFrom(aData->iStartTime); |
|
152 TTime transferTime(delay.Int64()); |
|
153 TBuf<64> timeString; |
|
154 transferTime.FormatL(timeString, _L("- Elapsed time: %S%C microseconds")); |
|
155 TBuf<256> tmp; |
|
156 tmp.Append(timeString); |
|
157 |
|
158 RFileLogger::Write(KLogFileDir,KLogFileName,EFileLoggingModeAppend,tmp); |
|
159 // Memory consumption |
|
160 TInt endMemory; |
|
161 User::AllocSize(endMemory); |
|
162 |
|
163 CreateLogFile(); |
|
164 //Write logs into file |
|
165 RFileLogger::WriteFormat(KLogFileDir,KLogFileName,EFileLoggingModeAppend,_L("- Allocated memory: %d kB"), (endMemory - aData->iStartMemory) / 1024); |
|
166 delete aData; |
|
167 aData = NULL; |
|
168 RFileLogger::WriteFormat(KLogFileDir,KLogFileName,EFileLoggingModeAppend,_L("Test case %S ends"), &aMsg); |
|
169 } |
|
170 inline void CMulPerf::CreateLogFile() |
|
171 { |
|
172 RFs fsSession; |
|
173 RFile file; |
|
174 User::LeaveIfError(fsSession.Connect()); |
|
175 TFileName filename; |
|
176 filename.Append(KLogFileRootDir); |
|
177 filename.Append(KLogFileDir); |
|
178 filename.Append(_L("\\")); |
|
179 //create logs directory if it doesn't exist |
|
180 fsSession.MkDir(filename); |
|
181 filename.Append(KLogFileName); |
|
182 //check if log file already exists |
|
183 TInt retStatus = file.Open(fsSession,filename,EFileRead); |
|
184 if (retStatus == KErrNotFound) |
|
185 file.Create(fsSession,filename,EFileWrite); //create a new log file |
|
186 file.Close(); |
|
187 fsSession.Close(); |
|
188 } |
|
189 #endif // CMULPERF_H |
|
190 |
|
191 // End of File |
|