2017-03-02 111 views
1

我想測試AES加密的性能。但是每當我運行代碼時,它都會給出不同的結果。爲什麼? 下面是在C++中使用加密++代碼:爲什麼Crypto ++中的AES代碼提供不同的性能結果?

int main(int argc, char* argv[]){ 
AutoSeededRandomPool prng; 

byte key[AES::DEFAULT_KEYLENGTH]; 
prng.GenerateBlock(key, sizeof(key)); 

byte iv[AES::BLOCKSIZE]; 
prng.GenerateBlock(iv, sizeof(iv)); 

CBC_Mode<AES>::Encryption e; 
e.SetKeyWithIV(key, sizeof(key), iv); 

CBC_Mode<AES>::Decryption d; 
d.SetKeyWithIV(key, sizeof(key), iv); 

時間測試是在這裏:

clock_t startTime, finishTime;  
std::string plain = "AES CBC Test"; 
std::string cipher, encoded, recovered; 
startTime = clock();  
try 
{ 

    // The StreamTransformationFilter removes 
    // padding as required. 
    StringSource s(plain, true, 
     new StreamTransformationFilter(e, 
      new StringSink(cipher) 
     ) // StreamTransformationFilter 
    ); // StringSource 

} 
catch(const CryptoPP::Exception& e) 
{ 
    cerr << e.what() << endl; 
    exit(1); 
}  
    // save current time just after finishing the encryption loop 
finishTime = clock(); 

和我的測試結果在這裏:

enter code heredouble executionTimeInSec = double(finishTime - startTime)/CLOCK_TICKS_PER_SECOND;  

std::cout << "Encryption loop execution time: " << executionTimeInSec * 1000.0 << " microseconds." << std::endl; 

std::cout << "Plain text size: " << plain.size() << " bytes." << std::endl; 

double data_rate_MiBps = ((double)plain.size()/1048576)/((double)executionTimeInSec) ; 

std::cout << "Encryption/decryption loop execution time MB/S: " << data_rate_MiBps << " MB/S." << std::endl; 
return 0;} 

時序優化調試版本。 編譯結果1:

加密循環執行時間:0.041微秒。

編譯結果2:

加密循環執行時間:0.057毫秒。

+0

這將有助於你的答案的機會,如果你的代碼實際上編譯。 –

+0

您還應該發佈是否計時優化的發佈版本或未優化的「調試」版本。如果是後者,那麼這些時間是沒有意義的。 – PaulMcKenzie

+0

另請參閱[如何在Crypto ++庫基準測試中運行?](http://stackoverflow.com/q/29264531/608639)和[AES/CBC加密與解密之間的速度差異?](http:// stackoverflow。 com/q/20164502/608639) – jww

回答

1

0.041微秒的測試時間太短。要獲得可靠的測量,您需要對測試執行多次迭代,然後將總時間除以您所做的迭代次數。

當在這麼短的時間內測量框架許多因素會弄亂你的計時:

  1. 您的系統時鐘的分辨率可能沒有足夠高的在你的措施給予相對大的跳躍。
  2. 您的計時只測量經過時間,而不是在CPU上運行的實際時間。操作系統在一次測試中將CPU分配給其他測試而不是另一次測試的影響會引發測量中的大幅波動。在進行多次迭代時,您可以在許多迭代中平滑此隨機影響,從而消除偶然性的影響。
  3. 等等
0

相關的加密+標杆,該庫在cryptest.exe提供了一個基準測試套件。你像下面那樣調用它。 b表示基準; 3表示運行測試3秒; 2.4表示CPU頻率爲2.4 GHz。

./cryptest.exe b 3 2.4 

下面的命令produces output similar to

你可以在bench1.cppbench2.cpp找到源代碼。對於AES,您需要檢查bench2.cpp。下面的代碼負責生成數字:

void BenchMark(const char *name, StreamTransformation &cipher, double timeTotal) 
{ 
    const int BUF_SIZE=RoundUpToMultipleOf(2048U, cipher.OptimalBlockSize()); 
    AlignedSecByteBlock buf(BUF_SIZE); 
    Test::GlobalRNG().GenerateBlock(buf, BUF_SIZE); 

    unsigned long i=0, blocks=1; 
    double timeTaken; 

    clock_t start = ::clock(); 
    do 
    { 
     blocks *= 2; 
     for (; i<blocks; i++) 
      cipher.ProcessString(buf, BUF_SIZE); 
     timeTaken = double(::clock() - start)/CLOCK_TICKS_PER_SECOND; 
    } 
    while (timeTaken < 2.0/3*timeTotal); 

    OutputResultBytes(name, double(blocks) * BUF_SIZE, timeTaken); 
} 

之前要調用BenchMarkStreamTransformation,工廠方法所創建的AES對象,並鍵入它進行測試。


一些基準代碼最近被更改了,但它主要是化妝品。最大的變化是增加了隨機數發生器來測試它們的表現。另請參閱用戶列表中的RNG Benchmarks and rework of RDRAND/RDSEED


當進行基準測試時,您還應該記住Turbo Boost和其他相關技術的影響。如果您的CPU運行在3.5 GHz,但在3.8 GHz時爆發,則會影響預期吞吐量。有時候,它會使基準成爲一個移動目標,而在其他時候,它可能會解釋爲什麼你超過了理論最大值。

突發只是一個內部超頻。英特爾多年來一直這樣做。他們過去稱之爲舊版P5 Pentium中的NetBurst。通過使用支持超頻的主板和處理器,硬件人員和玩家一直在努力。


enter image description here

相關問題