idlehomescreen/inc/ai3performance.h
changeset 0 f72a12da539e
equal deleted inserted replaced
-1:000000000000 0:f72a12da539e
       
     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:
       
    15 *
       
    16 */
       
    17 
       
    18 #ifndef AI3PERFORMANCE_H
       
    19 #define AI3PERFORMANCE_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,"ai3perf.log");
       
    31 _LIT(KLogFileDir,"ai3logs");
       
    32 _LIT(KLogFileRootDir,"c:\\logs\\");
       
    33 
       
    34 /**
       
    35  *  @class CAi3Perf
       
    36  *
       
    37  *  Provides API to measure performance (memory and time). Results are saved to a file.
       
    38  *
       
    39  *  @since Series 60 5.0
       
    40  */
       
    41 class CAi3Perf : public CBase
       
    42     {
       
    43     public:
       
    44        /**
       
    45         * Struct which holds data before measuring is started.
       
    46         */
       
    47         struct TPerfData
       
    48             {
       
    49            /**
       
    50             * Time when measure starts
       
    51             */
       
    52             TTime iStartTime;
       
    53             
       
    54            /**
       
    55             * Memory when measure starts
       
    56             */
       
    57             TInt iStartMemory;
       
    58             };
       
    59 
       
    60     public:  // Constructors and destructor
       
    61        /**
       
    62         * Constructor.
       
    63         */
       
    64         static CAi3Perf* NewL();
       
    65         
       
    66        /**
       
    67         * Destructor.
       
    68         */
       
    69         virtual ~CAi3Perf();
       
    70 
       
    71     public: // New functions
       
    72        /**
       
    73         * Prints current amount of memory to file.
       
    74         *
       
    75         * @param aMsg User defined message which is printed before amount of memory
       
    76         */
       
    77         inline static void PrintUserMem(const TDesC& aMsg);
       
    78         
       
    79        /**
       
    80         * Saves current amount of memory and time and returns those to user.
       
    81         *
       
    82         * @param aMsg User defined message.
       
    83         * @return TPerfData which holds current memory consumption and time.
       
    84         */
       
    85         inline static TPerfData* StartTestCase(const TDesC& aMsg = KNullDesC);
       
    86         
       
    87        /**
       
    88         * Reads current amount of memory and time and calculates performance using
       
    89         * values got from StartTestCase.
       
    90         *
       
    91         * @param aData Data which has got when calling StartTestCase.
       
    92         * @param aMsg User defined message.
       
    93         */
       
    94         inline static void StopTestCase(TPerfData* aData, const TDesC& aMsg = KNullDesC);
       
    95         
       
    96        /**
       
    97         * Create file where results are written to.
       
    98         */
       
    99         inline static void CreateLogFile();
       
   100     private:
       
   101        /**
       
   102         * Internal constructor.
       
   103         */
       
   104         CAi3Perf();
       
   105         
       
   106        /**
       
   107         * Internal constructor.
       
   108         */
       
   109         void ConstructL();
       
   110     };
       
   111 
       
   112 inline void CAi3Perf::PrintUserMem(const TDesC& aMsg)
       
   113     {
       
   114     TBuf<512> buffer;
       
   115     TInt freeRAM;
       
   116     HAL::Get(HALData::EMemoryRAMFree, freeRAM);
       
   117     RHeap heap = User::Heap();
       
   118     heap.Open();
       
   119     TInt _size = heap.Size();
       
   120     TInt largest = 0;
       
   121     TInt available = heap.Available(largest);
       
   122     heap.Close();
       
   123     _LIT( KMemoryFormat, "FreeRAM: %d kB, User: - heap %d kB, available %d kB, largest block %d kB" );
       
   124     buffer.Format(KMemoryFormat, freeRAM / 1024, _size / 1024, available / 1024, largest / 1024);
       
   125     CreateLogFile();
       
   126     //Write logs into file
       
   127     RFileLogger::WriteFormat( KLogFileDir,KLogFileName,EFileLoggingModeAppend,_L("RAM - %S - %S"), &aMsg, &buffer);
       
   128     }
       
   129 
       
   130 inline CAi3Perf::TPerfData* CAi3Perf::StartTestCase(const TDesC& aMsg)
       
   131     {
       
   132     CreateLogFile();
       
   133     //Write logs into file
       
   134     RFileLogger::WriteFormat(KLogFileDir,KLogFileName,EFileLoggingModeAppend,_L("Test case %S starts"), &aMsg);
       
   135     CAi3Perf::TPerfData* data = new (ELeave) TPerfData;
       
   136     TTime readyTime;
       
   137     data->iStartTime.HomeTime();
       
   138     User::AllocSize(data->iStartMemory);
       
   139     return data;
       
   140     }
       
   141 
       
   142 inline void CAi3Perf::StopTestCase(CAi3Perf::TPerfData* aData, const TDesC& aMsg)
       
   143     {
       
   144     if(!aData)
       
   145         {
       
   146         return;
       
   147         }
       
   148     TTime readyTime;
       
   149     readyTime.HomeTime();
       
   150     TTimeIntervalMicroSeconds delay = readyTime.MicroSecondsFrom(aData->iStartTime);
       
   151     TTime transferTime(delay.Int64());
       
   152     TBuf<64> timeString;
       
   153     transferTime.FormatL(timeString, _L("- Elapsed time: %S%C microseconds"));
       
   154     TBuf<256> tmp;
       
   155     tmp.Append(timeString);
       
   156 
       
   157     RFileLogger::Write(KLogFileDir,KLogFileName,EFileLoggingModeAppend,tmp);
       
   158     // Memory consumption
       
   159     TInt endMemory;
       
   160     User::AllocSize(endMemory); 
       
   161 
       
   162     CreateLogFile();
       
   163     //Write logs into file
       
   164     RFileLogger::WriteFormat(KLogFileDir,KLogFileName,EFileLoggingModeAppend,_L("- Allocated memory: %d kB"), (endMemory - aData->iStartMemory) / 1024);
       
   165     delete aData;
       
   166     aData = NULL;
       
   167     RFileLogger::WriteFormat(KLogFileDir,KLogFileName,EFileLoggingModeAppend,_L("Test case %S ends"), &aMsg);
       
   168     }
       
   169 inline void CAi3Perf::CreateLogFile()
       
   170     {
       
   171         RFs fsSession;
       
   172         RFile file;
       
   173         User::LeaveIfError(fsSession.Connect());
       
   174         TFileName filename;
       
   175         filename.Append(KLogFileRootDir);
       
   176         filename.Append(KLogFileDir);
       
   177         filename.Append(_L("\\"));
       
   178         //create logs directory if it doesn't exist
       
   179         fsSession.MkDir(filename);
       
   180         filename.Append(KLogFileName);
       
   181         //check if log file already exists
       
   182         TInt retStatus = file.Open(fsSession,filename,EFileRead);
       
   183         if(retStatus == KErrNotFound)
       
   184             file.Create(fsSession,filename,EFileWrite);         //create a new log file
       
   185         file.Close();
       
   186         fsSession.Close();
       
   187     }
       
   188 #endif // AI3PERFORMANCE_H
       
   189             
       
   190 // End of File