crypto/weakcrypto/test/tbigint/tprimevectorperformance.cpp
changeset 71 dd83586b62d6
equal deleted inserted replaced
66:8873e6835f7b 71:dd83586b62d6
       
     1 /*
       
     2 * Copyright (c) 2003-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 the License "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 
       
    19 #include "tprimevectorperformance.h"
       
    20 #include "t_input.h"
       
    21 #include "t_output.h"
       
    22 #include <bigint.h>
       
    23 
       
    24 _LIT(KPrimeVectorFormat, "\tPrime Vector Primality Test Time: %f us/iteration (%i iterations in %f us)\r\n");
       
    25 
       
    26 CTestAction* CPrimeVectorPerformance::NewL(RFs& aFs, CConsoleBase& aConsole, 
       
    27 	Output& aOut, const TTestActionSpec& aTestActionSpec)
       
    28 	{
       
    29 	CTestAction* self = CPrimeVectorPerformance::NewLC(aFs, aConsole,
       
    30 		aOut, aTestActionSpec);
       
    31 	CleanupStack::Pop();
       
    32 	return self;
       
    33 	}
       
    34 
       
    35 CTestAction* CPrimeVectorPerformance::NewLC(RFs& aFs, CConsoleBase& aConsole, 
       
    36 	Output& aOut, const TTestActionSpec& aTestActionSpec)
       
    37 	{
       
    38 	CPrimeVectorPerformance* self = new(ELeave) CPrimeVectorPerformance(aFs, aConsole, aOut);
       
    39 	CleanupStack::PushL(self);
       
    40 	self->ConstructL(aTestActionSpec);
       
    41 	return self;
       
    42 	}
       
    43 
       
    44 CPrimeVectorPerformance::~CPrimeVectorPerformance()
       
    45 	{
       
    46 	delete iBody;
       
    47 	}
       
    48 
       
    49 CPrimeVectorPerformance::CPrimeVectorPerformance(RFs& aFs, CConsoleBase& aConsole, 
       
    50 	Output& aOut) : CTestAction(aConsole, aOut), iFs(aFs)
       
    51 	{
       
    52 	}
       
    53 
       
    54 void CPrimeVectorPerformance::ConstructL(const TTestActionSpec& aTestActionSpec)
       
    55 	{
       
    56 	CTestAction::ConstructL(aTestActionSpec);
       
    57 	iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length());
       
    58 	iBody->Des().Copy(aTestActionSpec.iActionBody);
       
    59 
       
    60 	HBufC8* buf = Input::ParseElementHexL(*iBody, _L8("<prime>"));
       
    61 	CleanupStack::PushL(buf);
       
    62 	iPrime = RInteger::NewL(*buf);
       
    63 	CleanupStack::PopAndDestroy(buf);
       
    64 	iIterations = Input::ParseIntElement(*iBody, _L8("<iterations>"), _L8("</iterations>"));	
       
    65 	}
       
    66 
       
    67 void CPrimeVectorPerformance::DoPerformPrerequisite(TRequestStatus& aStatus)
       
    68 	{
       
    69 	TRequestStatus* status = &aStatus;
       
    70 	User::RequestComplete(status, KErrNone);
       
    71 	iActionState = CTestAction::EAction;
       
    72 	}
       
    73 
       
    74 void CPrimeVectorPerformance::DoPerformPostrequisite(TRequestStatus& aStatus)
       
    75 	{
       
    76 	TRequestStatus* status = &aStatus;
       
    77 	iFinished = ETrue;
       
    78 	User::RequestComplete(status, KErrNone);
       
    79 	}
       
    80 
       
    81 void CPrimeVectorPerformance::DoReportAction(void)
       
    82 	{
       
    83 	}
       
    84 
       
    85 void CPrimeVectorPerformance::DoCheckResult(TInt)
       
    86 	{
       
    87 	}
       
    88 
       
    89 void CPrimeVectorPerformance::PerformAction(TRequestStatus& aStatus)
       
    90 	{
       
    91 	__UHEAP_MARK;
       
    92 	TRequestStatus* status = &aStatus;
       
    93 	TTime start, end;
       
    94 	TTimeIntervalSeconds diff(0);
       
    95 	const TTimeIntervalSeconds iterationTime(iIterations);
       
    96 	TUint iterations = 0;	
       
    97 	
       
    98 	iResult = ETrue;
       
    99 
       
   100 	start.UniversalTime();
       
   101 	while (diff < iterationTime)
       
   102 		{
       
   103 		iResult = iPrime.IsPrimeL();
       
   104 		if(!iResult)
       
   105 			break;
       
   106 		iterations++;
       
   107 		end.UniversalTime();
       
   108 		end.SecondsFrom(start, diff);
       
   109 		}
       
   110 	end.UniversalTime();
       
   111 
       
   112 	TTimeIntervalMicroSeconds time = end.MicroSecondsFrom(start);
       
   113 	TReal rate = I64REAL(time.Int64()) / iterations;
       
   114 	TReal rtime = I64REAL(time.Int64());
       
   115 
       
   116 	HBufC* realbuf = HBufC::NewLC(128);
       
   117 	TPtr buf = realbuf->Des();
       
   118 	buf.Format(KPrimeVectorFormat, rate, iterations, rtime);
       
   119 	iOut.writeString(buf);
       
   120 	iConsole.Printf(_L("."));
       
   121 	CleanupStack::PopAndDestroy(realbuf);
       
   122 
       
   123 	User::RequestComplete(status, KErrNone);
       
   124 	iActionState = CTestAction::EPostrequisite;
       
   125 	__UHEAP_MARKEND;
       
   126 	}
       
   127