Update contrib.
2 * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
19 #include "tperformancetest.h"
20 #include "symmetric.h"
24 _LIT(KPerfEFormat, "\tPerformance(encryption)\t%f");
25 _LIT(KPerfDFormat, "\tPerformance(decryption)\t%f");
26 _LIT(KTotalFormat, "\tPerformance(total)\t%f");
27 _LIT(KPerfThroughput, "\tThroughput (total)\t%f");
28 _LIT(KPerfThroughputAfterSetup, "\tThroughput after setup\t%f");
29 _LIT(KPerfEncryptorCreate, "\tPerformance(encryptor create)\t%f");
30 _LIT(KPerfDecryptorCreate, "\tPerformance(decryptor create)\t%f");
31 //_LIT(KDataSize,"\tData input size : %i B\r\n");
34 CTestAction* CPerformanceTest::NewL(RFs& aFs,
35 CConsoleBase& aConsole,
37 const TTestActionSpec& aTestActionSpec)
39 CTestAction* self = CPerformanceTest::NewLC(aFs, aConsole,
40 aOut, aTestActionSpec);
45 CTestAction* CPerformanceTest::NewLC(RFs& aFs,
46 CConsoleBase& aConsole,
48 const TTestActionSpec& aTestActionSpec)
50 CPerformanceTest* self = new(ELeave) CPerformanceTest(aFs, aConsole, aOut);
51 CleanupStack::PushL(self);
52 self->ConstructL(aTestActionSpec);
56 CPerformanceTest::~CPerformanceTest()
62 CPerformanceTest::CPerformanceTest(RFs& aFs,
63 CConsoleBase& aConsole,
66 : CCryptoTestAction(aFs, aConsole, aOut)
69 void CPerformanceTest::DoPerformPrerequisiteL()
74 TPtrC8 vector = Input::ParseElement(*iBody, KVectorStart, KVectorEnd, pos, err);
76 DoInputParseL(vector);
78 CBlockTransformation* encryptor = 0;
79 CBlockTransformation* decryptor = 0;
84 {// Time the length of time it takes to set up the key schedule,
85 // save it and add to encrypt time, to be consistent with old API
86 // for comparison purposes (old API does both set up and encrypt/decrypt
87 // in a single operation).
90 TTimeIntervalSeconds diff(0);
91 const TTimeIntervalSeconds iterationTime(iIterationTime);
93 encryptor = CDESEncryptor::NewL(iKey->Des());
94 iEncryptIterations = 0;
95 startTime.UniversalTime();
96 while (diff < iterationTime)
100 endTime.UniversalTime();
101 endTime.SecondsFrom(startTime, diff);
103 endTime.UniversalTime();
104 iEncryptorCreateTime = endTime.MicroSecondsFrom(startTime);
108 decryptor = CDESDecryptor::NewL(iKey->Des());
111 iDecryptIterations = 0;
112 startTime.UniversalTime();
113 while (diff < iterationTime)
116 iDecryptIterations++;
117 endTime.UniversalTime();
118 endTime.SecondsFrom(startTime, diff);
120 endTime.UniversalTime();
121 iDecryptorCreateTime = endTime.MicroSecondsFrom(startTime);
125 encryptor = CDESEncryptor::NewLC(iKey->Des());
126 decryptor = CDESDecryptor::NewL(iKey->Des());
127 CleanupStack::Pop(encryptor);
132 CBlockTransformation* desEncryptor = NULL;
133 CBlockTransformation* desDecryptor = NULL;
135 desEncryptor = CDESEncryptor::NewLC(iKey->Des());
136 encryptor = CModeCBCEncryptor::NewL(desEncryptor, iIV->Des());
140 TTimeIntervalSeconds diff(0);
141 const TTimeIntervalSeconds iterationTime(iIterationTime);
143 iEncryptIterations = 0;
144 startTime.UniversalTime();
145 while (diff < iterationTime)
147 iEncryptIterations++;
149 endTime.UniversalTime();
150 endTime.SecondsFrom(startTime, diff);
152 endTime.UniversalTime();
153 iEncryptorCreateTime = endTime.MicroSecondsFrom(startTime);
155 CleanupStack::Pop(1);
158 desDecryptor = CDESDecryptor::NewLC(iKey->Des());
159 decryptor = CModeCBCDecryptor::NewL(desDecryptor, iIV->Des());
162 iDecryptIterations = 0;
163 startTime.UniversalTime();
164 while (diff < iterationTime)
167 iDecryptIterations++;
168 endTime.UniversalTime();
169 endTime.SecondsFrom(startTime, diff);
171 endTime.UniversalTime();
172 iDecryptorCreateTime = endTime.MicroSecondsFrom(startTime);
174 CleanupStack::Pop(1);
177 desEncryptor = CDESEncryptor::NewLC(iKey->Des());
178 desDecryptor = CDESDecryptor::NewLC(iKey->Des());
180 encryptor = CModeCBCEncryptor::NewL(desEncryptor, iIV->Des());
181 CleanupStack::PushL(encryptor);
182 decryptor = CModeCBCDecryptor::NewL(desDecryptor, iIV->Des());
183 CleanupStack::Pop(3);
188 encryptor = C3DESEncryptor::NewL(iKey->Des());
191 TTimeIntervalSeconds diff(0);
192 const TTimeIntervalSeconds iterationTime(iIterationTime);
194 iEncryptIterations = 0;
195 startTime.UniversalTime();
196 while (diff < iterationTime)
198 iEncryptIterations++;
200 endTime.UniversalTime();
201 endTime.SecondsFrom(startTime, diff);
203 endTime.UniversalTime();
204 iEncryptorCreateTime = endTime.MicroSecondsFrom(startTime);
209 iDecryptIterations = 0;
210 decryptor = C3DESDecryptor::NewL(iKey->Des());
211 startTime.UniversalTime();
212 while (diff < iterationTime)
215 iDecryptIterations++;
216 endTime.UniversalTime();
217 endTime.SecondsFrom(startTime, diff);
219 endTime.UniversalTime();
220 iDecryptorCreateTime = endTime.MicroSecondsFrom(startTime);
223 encryptor = C3DESEncryptor::NewLC(iKey->Des());
224 decryptor = C3DESDecryptor::NewL(iKey->Des());
225 CleanupStack::Pop(encryptor);
230 CBlockTransformation* the3DESencryptor = NULL;
231 CBlockTransformation* the3DESdecryptor = NULL;
233 the3DESencryptor = C3DESEncryptor::NewL(iKey->Des());
234 CleanupStack::PushL(the3DESencryptor);
235 encryptor = CModeCBCEncryptor::NewL(the3DESencryptor, iIV->Des());
239 TTimeIntervalSeconds diff(0);
240 const TTimeIntervalSeconds iterationTime(iIterationTime);
242 iEncryptIterations = 0;
243 startTime.UniversalTime();
244 while (diff < iterationTime)
246 iEncryptIterations++;
248 endTime.UniversalTime();
249 endTime.SecondsFrom(startTime, diff);
251 endTime.UniversalTime();
252 iEncryptorCreateTime = endTime.MicroSecondsFrom(startTime);
254 CleanupStack::Pop(1);
257 the3DESdecryptor = C3DESDecryptor::NewLC(iKey->Des());
258 decryptor = CModeCBCDecryptor::NewL(the3DESdecryptor, iIV->Des());
261 iDecryptIterations = 0;
262 startTime.UniversalTime();
263 while (diff < iterationTime)
266 iDecryptIterations++;
267 endTime.UniversalTime();
268 endTime.SecondsFrom(startTime, diff);
270 endTime.UniversalTime();
271 iDecryptorCreateTime = endTime.MicroSecondsFrom(startTime);
272 CleanupStack::Pop(1);
275 the3DESencryptor = C3DESEncryptor::NewLC(iKey->Des());
276 the3DESdecryptor = C3DESDecryptor::NewLC(iKey->Des());
278 encryptor = CModeCBCEncryptor::NewL(the3DESencryptor, iIV->Des());
279 CleanupStack::PushL(encryptor);
280 decryptor = CModeCBCDecryptor::NewL(the3DESdecryptor, iIV->Des());
281 CleanupStack::Pop(3);
288 TTimeIntervalSeconds diff(0);
289 const TTimeIntervalSeconds iterationTime(iIterationTime);
291 iEncryptIterations = 0;
292 encryptor = CAESEncryptor::NewL(iKey->Des());
293 startTime.UniversalTime();
294 while (diff < iterationTime)
296 iEncryptIterations++;
298 endTime.UniversalTime();
299 endTime.SecondsFrom(startTime, diff);
301 endTime.UniversalTime();
302 iEncryptorCreateTime = endTime.MicroSecondsFrom(startTime);
306 iDecryptIterations = 0;
307 decryptor = CAESDecryptor::NewL(iKey->Des());
308 startTime.UniversalTime();
309 while (diff < iterationTime)
312 iDecryptIterations++;
313 endTime.UniversalTime();
314 endTime.SecondsFrom(startTime, diff);
316 endTime.UniversalTime();
317 iDecryptorCreateTime = endTime.MicroSecondsFrom(startTime);
320 encryptor = CAESEncryptor::NewLC(iKey->Des());
321 decryptor = CAESDecryptor::NewL(iKey->Des());
322 CleanupStack::Pop(encryptor);
329 TTimeIntervalSeconds diff(0);
330 const TTimeIntervalSeconds iterationTime(iIterationTime);
332 iEncryptIterations = 0;
333 encryptor = CRC2Encryptor::NewL(iKey->Des(), iEffectiveKeyLen);
334 startTime.UniversalTime();
335 while (diff < iterationTime)
337 iEncryptIterations++;
339 endTime.UniversalTime();
340 endTime.SecondsFrom(startTime, diff);
342 endTime.UniversalTime();
343 iEncryptorCreateTime = endTime.MicroSecondsFrom(startTime);
347 decryptor = CRC2Decryptor::NewL(iKey->Des(), iEffectiveKeyLen);
348 iDecryptIterations = 0;
349 startTime.UniversalTime();
350 while (diff < iterationTime)
353 iDecryptIterations++;
354 endTime.UniversalTime();
355 endTime.SecondsFrom(startTime, diff);
357 endTime.UniversalTime();
358 iDecryptorCreateTime = endTime.MicroSecondsFrom(startTime);
361 encryptor = CRC2Encryptor::NewLC(iKey->Des(), iEffectiveKeyLen);
362 decryptor = CRC2Decryptor::NewL(iKey->Des(), iEffectiveKeyLen);
363 CleanupStack::Pop(encryptor);
368 CBlockTransformation* theRC2encryptor = NULL;
369 CBlockTransformation* theRC2decryptor = NULL;
371 theRC2encryptor = CRC2Encryptor::NewLC(iKey->Des(), iEffectiveKeyLen);
372 encryptor = CModeCBCEncryptor::NewL(theRC2encryptor, iIV->Des());
376 TTimeIntervalSeconds diff(0);
377 const TTimeIntervalSeconds iterationTime(iIterationTime);
379 iEncryptIterations = 0;
380 startTime.UniversalTime();
381 while (diff < iterationTime)
383 iEncryptIterations++;
385 endTime.UniversalTime();
386 endTime.SecondsFrom(startTime, diff);
388 endTime.UniversalTime();
389 iEncryptorCreateTime = endTime.MicroSecondsFrom(startTime);
390 CleanupStack::Pop(1);
394 iDecryptIterations = 0;
395 theRC2decryptor = CRC2Decryptor::NewLC(iKey->Des(), iEffectiveKeyLen);
396 decryptor = CModeCBCDecryptor::NewL(theRC2decryptor, iIV->Des());
397 startTime.UniversalTime();
398 while (diff < iterationTime)
401 iDecryptIterations++;
402 endTime.UniversalTime();
403 endTime.SecondsFrom(startTime, diff);
405 endTime.UniversalTime();
406 iDecryptorCreateTime = endTime.MicroSecondsFrom(startTime);
407 CleanupStack::Pop(1);
410 theRC2encryptor = CRC2Encryptor::NewLC(iKey->Des(), iEffectiveKeyLen);
411 theRC2decryptor = CRC2Decryptor::NewLC(iKey->Des(), iEffectiveKeyLen);
413 encryptor = CModeCBCEncryptor::NewL(theRC2encryptor, iIV->Des());
414 CleanupStack::PushL(encryptor);
415 decryptor = CModeCBCDecryptor::NewL(theRC2decryptor, iIV->Des());
416 CleanupStack::Pop(3);
421 iEncryptor = CARC4::NewL(*iKey);
424 TTimeIntervalSeconds diff(0);
425 const TTimeIntervalSeconds iterationTime(iIterationTime);
427 iEncryptIterations = 0;
428 startTime.UniversalTime();
429 while (diff < iterationTime)
431 iEncryptIterations++;
433 endTime.UniversalTime();
434 endTime.SecondsFrom(startTime, diff);
436 endTime.UniversalTime();
437 iEncryptorCreateTime = endTime.MicroSecondsFrom(startTime);
440 iDecryptIterations = 0;
441 iDecryptor = CARC4::NewL(*iKey);
442 startTime.UniversalTime();
443 while (diff < iterationTime)
446 iDecryptIterations++;
447 endTime.UniversalTime();
448 endTime.SecondsFrom(startTime, diff);
450 endTime.UniversalTime();
451 iDecryptorCreateTime = endTime.MicroSecondsFrom(startTime);
456 iEncryptor = CNullCipher::NewL();
459 TTimeIntervalSeconds diff(0);
460 const TTimeIntervalSeconds iterationTime(iIterationTime);
462 iEncryptIterations = 0;
463 startTime.UniversalTime();
464 while (diff < iterationTime)
466 iEncryptIterations++;
468 endTime.UniversalTime();
469 endTime.SecondsFrom(startTime, diff);
471 endTime.UniversalTime();
472 iEncryptorCreateTime = endTime.MicroSecondsFrom(startTime);
475 iDecryptIterations = 0;
476 iDecryptor = CNullCipher::NewL();
477 startTime.UniversalTime();
478 while (diff < iterationTime)
481 iDecryptIterations++;
482 endTime.UniversalTime();
483 endTime.SecondsFrom(startTime, diff);
485 endTime.UniversalTime();
486 iDecryptorCreateTime = endTime.MicroSecondsFrom(startTime);
493 User::Leave(KErrNotSupported);
497 if( encryptor && decryptor )
499 CleanupStack::PushL(encryptor);
500 CleanupStack::PushL(decryptor);
502 CPaddingSSLv3* ePadding = CPaddingSSLv3::NewLC(encryptor->BlockSize());
503 iEncryptor = CBufferedEncryptor::NewL(encryptor, ePadding);
504 CleanupStack::Pop(ePadding);
506 CPaddingSSLv3* dPadding = CPaddingSSLv3::NewLC(decryptor->BlockSize());
507 iDecryptor = CBufferedDecryptor::NewL(decryptor, dPadding);
508 CleanupStack::Pop(dPadding);
509 CleanupStack::Pop(decryptor);
510 CleanupStack::Pop(encryptor);
513 // clear the default input and output fields and fill with random data
514 // since for performance testing we do not care about validating
518 iInput = HBufC8::NewMaxL(iRandDataSize);
519 TPtr8 tempPtr = iInput->Des();
520 TRandom::Random(tempPtr);
522 //tempbuf.Format(KDataSize, (iInput->Length()));
523 iOut.writeString(tempbuf);
526 iOutput = HBufC8::NewMaxL(iRandDataSize);
527 TPtr8 tempPtr2 = iOutput->Des();
528 TRandom::Random(tempPtr2);
530 iEResult = HBufC8::NewMaxL(iEncryptor->MaxOutputLength(iInput->Length()));
531 iDResult = HBufC8::NewMaxL(iDecryptor->MaxOutputLength(iEResult->Size()));
535 void CPerformanceTest::DoPerformActionL()
540 TReal rate = I64REAL(iEncryptorCreateTime.Int64()) / iEncryptIterations;
541 buf.Format(KPerfEncryptorCreate, rate, iEncryptIterations, iEncryptorCreateTime.Int64());
542 iOut.writeString(buf);
546 TTimeIntervalSeconds diff(0);
547 const TTimeIntervalSeconds iterationTime(iIterationTime);
549 iEncryptIterations = 0;
550 startTime.UniversalTime();
551 while (diff < iterationTime)
553 TPtr8 iEResultTemp(iEResult->Des());
556 iEncryptor->Process(*iInput, iEResultTemp);
557 iEncryptIterations++;
558 endTime.UniversalTime();
559 endTime.SecondsFrom(startTime, diff);
561 endTime.UniversalTime();
563 TTimeIntervalMicroSeconds time = endTime.MicroSecondsFrom(startTime);
564 rate = I64REAL(time.Int64()) / iEncryptIterations;
565 buf.Format(KPerfEFormat, rate, iEncryptIterations, time.Int64());
566 iOut.writeString(buf);
568 rate = (static_cast<TReal>(iEncryptIterations) * iInput->Size() * 1000 * 1000) / (I64REAL(time.Int64()) * 1024 * 1024); // Throughput in MB/s
571 buf.Format(KPerfThroughputAfterSetup, rate);
572 iOut.writeString(buf);
574 TInt64 totalEncryptTime = time.Int64();
575 totalEncryptTime+=iEncryptorCreateTime.Int64();
576 rate = I64REAL(totalEncryptTime) / iEncryptIterations;
579 buf.Format(KTotalFormat, rate, iEncryptIterations, totalEncryptTime);
580 iOut.writeString(buf);
581 rate = (static_cast<TReal>(iEncryptIterations) * iInput->Size() * 1000 * 1000) / (I64REAL(totalEncryptTime) * 1024 * 1024); // Throughput in MB/s
584 buf.Format(KPerfThroughput, rate);
585 iOut.writeString(buf);
588 rate = I64REAL(iDecryptorCreateTime.Int64()) / iDecryptIterations;
589 buf.Format(KPerfDecryptorCreate, rate, iDecryptIterations, iDecryptorCreateTime.Int64());
590 iOut.writeString(buf);
593 iDecryptIterations = 0;
594 startTime.UniversalTime();
595 while (diff < iterationTime)
597 TPtr8 iDResultTemp(iDResult->Des());
599 iDecryptor->Process(*iOutput, iDResultTemp);
600 iDecryptIterations++;
601 endTime.UniversalTime();
602 endTime.SecondsFrom(startTime, diff);
604 endTime.UniversalTime();
606 time = endTime.MicroSecondsFrom(startTime);
607 rate = I64REAL(time.Int64()) / iDecryptIterations;
610 buf.Format(KPerfDFormat, rate, iDecryptIterations, time.Int64());
611 iOut.writeString(buf);
613 rate = (static_cast<TReal>(iDecryptIterations) * iInput->Size() * 1000 * 1000) / (I64REAL(time.Int64()) * 1024 * 1024); // Throughput in MB/s
616 buf.Format(KPerfThroughputAfterSetup, rate);
617 iOut.writeString(buf);
619 TInt64 totalDecryptTime = time.Int64();
620 totalDecryptTime+=iDecryptorCreateTime.Int64();
621 rate = I64REAL(totalDecryptTime) / iDecryptIterations;
624 buf.Format(KTotalFormat, rate, iDecryptIterations, totalDecryptTime);
625 iOut.writeString(buf);
627 rate = (static_cast<TReal>(iDecryptIterations) * iInput->Size() * 1000 * 1000) / (I64REAL(totalDecryptTime) * 1024 * 1024); // Throughput in MB/s
630 buf.Format(KPerfThroughput, rate);
631 iOut.writeString(buf);