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) : %f us/iteration (%i iterations in %iL us)\r\n");
25 _LIT(KPerfDFormat, "\tPerformance (decryption) : %f us/iteration (%i iterations in %iL us)\r\n");
26 _LIT(KTotalFormat, "\tPerformance (total) : %f us/iteration (%i iterations in %iL us)\r\n");
27 _LIT(KPerfThroughput, "\tThroughput (total): %f MB/s\r\n");
28 _LIT(KPerfThroughputAfterSetup, "\tThroughput after setup: %f MB/s\r\n");
29 _LIT(KPerfEncryptorCreate, "\tPerformance (encryptor create) : %f us/iteration (%i iterations in %iL us)\r\n");
30 _LIT(KPerfDecryptorCreate, "\tPerformance (decryptor create) : %f us/iteration (%i iterations in %iL us)\r\n");
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::NewLC(iKey->Des());
234 encryptor = CModeCBCEncryptor::NewL(the3DESencryptor, iIV->Des());
238 TTimeIntervalSeconds diff(0);
239 const TTimeIntervalSeconds iterationTime(iIterationTime);
241 iEncryptIterations = 0;
242 startTime.UniversalTime();
243 while (diff < iterationTime)
245 iEncryptIterations++;
247 endTime.UniversalTime();
248 endTime.SecondsFrom(startTime, diff);
250 endTime.UniversalTime();
251 iEncryptorCreateTime = endTime.MicroSecondsFrom(startTime);
253 CleanupStack::Pop(1);
256 the3DESdecryptor = C3DESDecryptor::NewLC(iKey->Des());
257 decryptor = CModeCBCDecryptor::NewL(the3DESdecryptor, iIV->Des());
260 iDecryptIterations = 0;
261 startTime.UniversalTime();
262 while (diff < iterationTime)
265 iDecryptIterations++;
266 endTime.UniversalTime();
267 endTime.SecondsFrom(startTime, diff);
269 endTime.UniversalTime();
270 iDecryptorCreateTime = endTime.MicroSecondsFrom(startTime);
271 CleanupStack::Pop(1);
274 the3DESencryptor = C3DESEncryptor::NewLC(iKey->Des());
275 the3DESdecryptor = C3DESDecryptor::NewLC(iKey->Des());
277 encryptor = CModeCBCEncryptor::NewL(the3DESencryptor, iIV->Des());
278 CleanupStack::PushL(encryptor);
279 decryptor = CModeCBCDecryptor::NewL(the3DESdecryptor, iIV->Des());
280 CleanupStack::Pop(3);
287 TTimeIntervalSeconds diff(0);
288 const TTimeIntervalSeconds iterationTime(iIterationTime);
290 iEncryptIterations = 0;
291 encryptor = CAESEncryptor::NewL(iKey->Des());
292 startTime.UniversalTime();
293 while (diff < iterationTime)
295 iEncryptIterations++;
297 endTime.UniversalTime();
298 endTime.SecondsFrom(startTime, diff);
300 endTime.UniversalTime();
301 iEncryptorCreateTime = endTime.MicroSecondsFrom(startTime);
305 iDecryptIterations = 0;
306 decryptor = CAESDecryptor::NewL(iKey->Des());
307 startTime.UniversalTime();
308 while (diff < iterationTime)
311 iDecryptIterations++;
312 endTime.UniversalTime();
313 endTime.SecondsFrom(startTime, diff);
315 endTime.UniversalTime();
316 iDecryptorCreateTime = endTime.MicroSecondsFrom(startTime);
319 encryptor = CAESEncryptor::NewLC(iKey->Des());
320 decryptor = CAESDecryptor::NewL(iKey->Des());
321 CleanupStack::Pop(encryptor);
328 TTimeIntervalSeconds diff(0);
329 const TTimeIntervalSeconds iterationTime(iIterationTime);
331 iEncryptIterations = 0;
332 encryptor = CRC2Encryptor::NewL(iKey->Des(), iEffectiveKeyLen);
333 startTime.UniversalTime();
334 while (diff < iterationTime)
336 iEncryptIterations++;
338 endTime.UniversalTime();
339 endTime.SecondsFrom(startTime, diff);
341 endTime.UniversalTime();
342 iEncryptorCreateTime = endTime.MicroSecondsFrom(startTime);
346 decryptor = CRC2Decryptor::NewL(iKey->Des(), iEffectiveKeyLen);
347 iDecryptIterations = 0;
348 startTime.UniversalTime();
349 while (diff < iterationTime)
352 iDecryptIterations++;
353 endTime.UniversalTime();
354 endTime.SecondsFrom(startTime, diff);
356 endTime.UniversalTime();
357 iDecryptorCreateTime = endTime.MicroSecondsFrom(startTime);
360 encryptor = CRC2Encryptor::NewLC(iKey->Des(), iEffectiveKeyLen);
361 decryptor = CRC2Decryptor::NewL(iKey->Des(), iEffectiveKeyLen);
362 CleanupStack::Pop(encryptor);
367 CBlockTransformation* theRC2encryptor = NULL;
368 CBlockTransformation* theRC2decryptor = NULL;
370 theRC2encryptor = CRC2Encryptor::NewLC(iKey->Des(), iEffectiveKeyLen);
371 encryptor = CModeCBCEncryptor::NewL(theRC2encryptor, iIV->Des());
375 TTimeIntervalSeconds diff(0);
376 const TTimeIntervalSeconds iterationTime(iIterationTime);
378 iEncryptIterations = 0;
379 startTime.UniversalTime();
380 while (diff < iterationTime)
382 iEncryptIterations++;
384 endTime.UniversalTime();
385 endTime.SecondsFrom(startTime, diff);
387 endTime.UniversalTime();
388 iEncryptorCreateTime = endTime.MicroSecondsFrom(startTime);
389 CleanupStack::Pop(1);
393 iDecryptIterations = 0;
394 theRC2decryptor = CRC2Decryptor::NewLC(iKey->Des(), iEffectiveKeyLen);
395 decryptor = CModeCBCDecryptor::NewL(theRC2decryptor, iIV->Des());
396 startTime.UniversalTime();
397 while (diff < iterationTime)
400 iDecryptIterations++;
401 endTime.UniversalTime();
402 endTime.SecondsFrom(startTime, diff);
404 endTime.UniversalTime();
405 iDecryptorCreateTime = endTime.MicroSecondsFrom(startTime);
406 CleanupStack::Pop(1);
409 theRC2encryptor = CRC2Encryptor::NewLC(iKey->Des(), iEffectiveKeyLen);
410 theRC2decryptor = CRC2Decryptor::NewLC(iKey->Des(), iEffectiveKeyLen);
412 encryptor = CModeCBCEncryptor::NewL(theRC2encryptor, iIV->Des());
413 CleanupStack::PushL(encryptor);
414 decryptor = CModeCBCDecryptor::NewL(theRC2decryptor, iIV->Des());
415 CleanupStack::Pop(3);
420 iEncryptor = CARC4::NewL(*iKey);
423 TTimeIntervalSeconds diff(0);
424 const TTimeIntervalSeconds iterationTime(iIterationTime);
426 iEncryptIterations = 0;
427 startTime.UniversalTime();
428 while (diff < iterationTime)
430 iEncryptIterations++;
432 endTime.UniversalTime();
433 endTime.SecondsFrom(startTime, diff);
435 endTime.UniversalTime();
436 iEncryptorCreateTime = endTime.MicroSecondsFrom(startTime);
439 iDecryptIterations = 0;
440 iDecryptor = CARC4::NewL(*iKey);
441 startTime.UniversalTime();
442 while (diff < iterationTime)
445 iDecryptIterations++;
446 endTime.UniversalTime();
447 endTime.SecondsFrom(startTime, diff);
449 endTime.UniversalTime();
450 iDecryptorCreateTime = endTime.MicroSecondsFrom(startTime);
455 iEncryptor = CNullCipher::NewL();
458 TTimeIntervalSeconds diff(0);
459 const TTimeIntervalSeconds iterationTime(iIterationTime);
461 iEncryptIterations = 0;
462 startTime.UniversalTime();
463 while (diff < iterationTime)
465 iEncryptIterations++;
467 endTime.UniversalTime();
468 endTime.SecondsFrom(startTime, diff);
470 endTime.UniversalTime();
471 iEncryptorCreateTime = endTime.MicroSecondsFrom(startTime);
474 iDecryptIterations = 0;
475 iDecryptor = CNullCipher::NewL();
476 startTime.UniversalTime();
477 while (diff < iterationTime)
480 iDecryptIterations++;
481 endTime.UniversalTime();
482 endTime.SecondsFrom(startTime, diff);
484 endTime.UniversalTime();
485 iDecryptorCreateTime = endTime.MicroSecondsFrom(startTime);
492 User::Leave(KErrNotSupported);
496 if( encryptor && decryptor )
498 CleanupStack::PushL(encryptor);
499 CleanupStack::PushL(decryptor);
501 CPaddingSSLv3* ePadding = CPaddingSSLv3::NewLC(encryptor->BlockSize());
502 iEncryptor = CBufferedEncryptor::NewL(encryptor, ePadding);
503 CleanupStack::Pop(ePadding);
505 CPaddingSSLv3* dPadding = CPaddingSSLv3::NewLC(decryptor->BlockSize());
506 iDecryptor = CBufferedDecryptor::NewL(decryptor, dPadding);
507 CleanupStack::Pop(dPadding);
508 CleanupStack::Pop(decryptor);
509 CleanupStack::Pop(encryptor);
512 // clear the default input and output fields and fill with random data
513 // since for performance testing we do not care about validating
517 iInput = HBufC8::NewMaxL(iRandDataSize);
518 TPtr8 tempPtr = iInput->Des();
519 TRandom::Random(tempPtr);
521 tempbuf.Format(KDataSize, (iInput->Length()));
522 iOut.writeString(tempbuf);
525 iOutput = HBufC8::NewMaxL(iRandDataSize);
526 TPtr8 tempPtr2 = iOutput->Des();
527 TRandom::Random(tempPtr2);
529 iEResult = HBufC8::NewMaxL(iEncryptor->MaxOutputLength(iInput->Length()));
530 iDResult = HBufC8::NewMaxL(iDecryptor->MaxOutputLength(iEResult->Size()));
534 void CPerformanceTest::DoPerformActionL()
539 TReal rate = I64REAL(iEncryptorCreateTime.Int64()) / iEncryptIterations;
540 buf.Format(KPerfEncryptorCreate, rate, iEncryptIterations, iEncryptorCreateTime.Int64());
541 iOut.writeString(buf);
545 TTimeIntervalSeconds diff(0);
546 const TTimeIntervalSeconds iterationTime(iIterationTime);
548 iEncryptIterations = 0;
549 startTime.UniversalTime();
550 while (diff < iterationTime)
552 TPtr8 iEResultTemp(iEResult->Des());
555 iEncryptor->Process(*iInput, iEResultTemp);
556 iEncryptIterations++;
557 endTime.UniversalTime();
558 endTime.SecondsFrom(startTime, diff);
560 endTime.UniversalTime();
562 TTimeIntervalMicroSeconds time = endTime.MicroSecondsFrom(startTime);
563 rate = I64REAL(time.Int64()) / iEncryptIterations;
564 buf.Format(KPerfEFormat, rate, iEncryptIterations, time.Int64());
565 iOut.writeString(buf);
567 rate = (static_cast<TReal>(iEncryptIterations) * iInput->Size() * 1000 * 1000) / (I64REAL(time.Int64()) * 1024 * 1024); // Throughput in MB/s
570 buf.Format(KPerfThroughputAfterSetup, rate);
571 iOut.writeString(buf);
573 TInt64 totalEncryptTime = time.Int64();
574 totalEncryptTime+=iEncryptorCreateTime.Int64();
575 rate = I64REAL(totalEncryptTime) / iEncryptIterations;
578 buf.Format(KTotalFormat, rate, iEncryptIterations, totalEncryptTime);
579 iOut.writeString(buf);
581 rate = (static_cast<TReal>(iEncryptIterations) * iInput->Size() * 1000 * 1000) / (I64REAL(totalEncryptTime) * 1024 * 1024); // Throughput in MB/s
583 buf.Format(KPerfThroughput, rate);
584 iOut.writeString(buf);
587 rate = I64REAL(iDecryptorCreateTime.Int64()) / iDecryptIterations;
588 buf.Format(KPerfDecryptorCreate, rate, iDecryptIterations, iDecryptorCreateTime.Int64());
589 iOut.writeString(buf);
592 iDecryptIterations = 0;
593 startTime.UniversalTime();
594 while (diff < iterationTime)
596 TPtr8 iDResultTemp(iDResult->Des());
598 iDecryptor->Process(*iOutput, iDResultTemp);
599 iDecryptIterations++;
600 endTime.UniversalTime();
601 endTime.SecondsFrom(startTime, diff);
603 endTime.UniversalTime();
605 time = endTime.MicroSecondsFrom(startTime);
606 rate = I64REAL(time.Int64()) / iDecryptIterations;
609 buf.Format(KPerfDFormat, rate, iDecryptIterations, time.Int64());
610 iOut.writeString(buf);
612 rate = (static_cast<TReal>(iDecryptIterations) * iInput->Size() * 1000 * 1000) / (I64REAL(time.Int64()) * 1024 * 1024); // Throughput in MB/s
614 buf.Format(KPerfThroughputAfterSetup, rate);
615 iOut.writeString(buf);
617 TInt64 totalDecryptTime = time.Int64();
618 totalDecryptTime+=iDecryptorCreateTime.Int64();
619 rate = I64REAL(totalDecryptTime) / iDecryptIterations;
622 buf.Format(KTotalFormat, rate, iDecryptIterations, totalDecryptTime);
623 iOut.writeString(buf);
625 rate = (static_cast<TReal>(iDecryptIterations) * iInput->Size() * 1000 * 1000) / (I64REAL(totalDecryptTime) * 1024 * 1024); // Throughput in MB/s
627 buf.Format(KPerfThroughput, rate);
628 iOut.writeString(buf);