From f290746a363a971dc55355fd829eb0f3b936eb8e Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Sun, 29 Jul 2018 22:35:36 -0400 Subject: Remove Coverity worakaround StreamState Use std::ostringstream instead. Eventually I'd like to see the output stream passed into the function of interest. It will avoid problems on some mobile OSes that don't have standard inputs and outputs. --- bench1.cpp | 99 +++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 56 insertions(+), 43 deletions(-) (limited to 'bench1.cpp') diff --git a/bench1.cpp b/bench1.cpp index 11dee0be..714a8637 100644 --- a/bench1.cpp +++ b/bench1.cpp @@ -13,6 +13,10 @@ #include "smartptr.h" #include "stdcpp.h" +#include +#include +#include + #if CRYPTOPP_MSC_VERSION # pragma warning(disable: 4355) #endif @@ -32,7 +36,7 @@ const double CLOCK_TICKS_PER_SECOND = (double)CLK_TCK; const double CLOCK_TICKS_PER_SECOND = 1000000.0; #endif -const byte defaultKey[] = "0123456789" // 168 + NULL +extern const byte defaultKey[] = "0123456789" // 168 + NULL "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" "00000000000000000000000000000000000000000000000000000" "00000000000000000000000000000000000000000000000000000"; @@ -43,67 +47,70 @@ time_t g_testBegin, g_testEnd; void OutputResultBytes(const char *name, const char *provider, double length, double timeTaken) { - // Coverity finding, also see http://stackoverflow.com/a/34509163/608639. - StreamState ss(std::cout); + std::ostringstream oss; // Coverity finding if (length < 0.000001f) length = 0.000001f; if (timeTaken < 0.000001f) timeTaken = 0.000001f; double mbs = length / timeTaken / (1024*1024); - std::cout << "\n" << name << "" << provider; - std::cout << std::setiosflags(std::ios::fixed); - std::cout << "" << std::setprecision(0) << std::setiosflags(std::ios::fixed) << mbs; + oss << "\n" << name << "" << provider; + oss << std::setiosflags(std::ios::fixed); + oss << "" << std::setprecision(0) << std::setiosflags(std::ios::fixed) << mbs; if (g_hertz > 1.0f) { const double cpb = timeTaken * g_hertz / length; if (cpb < 24.0f) - std::cout << "" << std::setprecision(2) << std::setiosflags(std::ios::fixed) << cpb; + oss << "" << std::setprecision(2) << std::setiosflags(std::ios::fixed) << cpb; else - std::cout << "" << std::setprecision(1) << std::setiosflags(std::ios::fixed) << cpb; + oss << "" << std::setprecision(1) << std::setiosflags(std::ios::fixed) << cpb; } g_logTotal += log(mbs); g_logCount++; + + std::cout << oss.str(); } void OutputResultKeying(double iterations, double timeTaken) { - // Coverity finding, also see http://stackoverflow.com/a/34509163/608639. - StreamState ss(std::cout); + std::ostringstream oss; // Coverity finding if (iterations < 0.000001f) iterations = 0.000001f; if (timeTaken < 0.000001f) timeTaken = 0.000001f; - std::cout << "" << std::setprecision(3) << std::setiosflags(std::ios::fixed) << (1000*1000*timeTaken/iterations); + oss << "" << std::setprecision(3) << std::setiosflags(std::ios::fixed) << (1000*1000*timeTaken/iterations); // Coverity finding if (g_hertz > 1.0f) - std::cout << "" << std::setprecision(0) << std::setiosflags(std::ios::fixed) << timeTaken * g_hertz / iterations; + oss << "" << std::setprecision(0) << std::setiosflags(std::ios::fixed) << timeTaken * g_hertz / iterations; + + std::cout << oss.str(); } void OutputResultOperations(const char *name, const char *provider, const char *operation, bool pc, unsigned long iterations, double timeTaken) { - // Coverity finding, also see http://stackoverflow.com/a/34509163/608639. - StreamState ss(std::cout); + std::ostringstream oss; // Coverity finding if (!iterations) iterations++; if (timeTaken < 0.000001f) timeTaken = 0.000001f; - std::cout << "\n" << name << " " << operation << (pc ? " with precomputation" : ""); - std::cout << "" << provider; - std::cout << "" << std::setprecision(2) << std::setiosflags(std::ios::fixed) << (1000*timeTaken/iterations); + oss << "\n" << name << " " << operation << (pc ? " with precomputation" : ""); + oss << "" << provider; + oss << "" << std::setprecision(2) << std::setiosflags(std::ios::fixed) << (1000*timeTaken/iterations); // Coverity finding if (g_hertz > 1.0f) { const double t = timeTaken * g_hertz / iterations / 1000000; - std::cout << "" << std::setprecision(2) << std::setiosflags(std::ios::fixed) << t; + oss << "" << std::setprecision(2) << std::setiosflags(std::ios::fixed) << t; } g_logTotal += log(iterations/timeTaken); g_logCount++; + + std::cout << oss.str(); } /* @@ -276,34 +283,40 @@ void BenchMarkByNameKeyLess(const char *factoryName, const char *displayName = N void AddHtmlHeader() { + std::ostringstream oss; + // HTML5 - std::cout << ""; - std::cout << "\n"; + oss << ""; + oss << "\n"; - std::cout << "\n"; - std::cout << "\n"; - std::cout << "\nSpeed Comparison of Popular Crypto Algorithms"; - std::cout << "\n"; - std::cout << "\n"; + oss << "\n"; + oss << "\n"; + oss << "\nSpeed Comparison of Popular Crypto Algorithms"; + oss << "\n"; + oss << "\n"; - std::cout << "\n"; + oss << "\n"; - std::cout << "\n

Crypto++ " << CRYPTOPP_VERSION / 100; - std::cout << '.' << (CRYPTOPP_VERSION % 100) / 10 << '.' << CRYPTOPP_VERSION % 10 << " Benchmarks

"; + oss << "\n

Crypto++ " << CRYPTOPP_VERSION / 100; + oss << '.' << (CRYPTOPP_VERSION % 100) / 10 << '.' << CRYPTOPP_VERSION % 10 << " Benchmarks

"; - std::cout << "\n

Here are speed benchmarks for some commonly used cryptographic algorithms.

"; + oss << "\n

Here are speed benchmarks for some commonly used cryptographic algorithms.

"; if (g_hertz > 1.0f) - std::cout << "\n

CPU frequency of the test platform is " << g_hertz << " Hz.

"; + oss << "\n

CPU frequency of the test platform is " << g_hertz << " Hz.

"; else - std::cout << "\n

CPU frequency of the test platform was not provided.

" << std::endl; + oss << "\n

CPU frequency of the test platform was not provided.

" << std::endl; + + std::cout << oss.str(); } void AddHtmlFooter() { + std::ostringstream oss; std::cout << "\n"; std::cout << "\n" << std::endl; + std::cout << oss.str(); } void BenchmarkWithCommand(int argc, const char* const argv[]) @@ -327,6 +340,7 @@ void Benchmark(Test::TestClass suites, double t, double hertz) { g_allocatedTime = t; g_hertz = hertz; + std::ostringstream oss; AddHtmlHeader(); @@ -338,37 +352,36 @@ void Benchmark(Test::TestClass suites, double t, double hertz) // Unkeyed algorithms if (suites & Test::Unkeyed) { - std::cout << "\n
"; + oss << "\n
"; Benchmark1(t, hertz); } // Shared key algorithms if (suites & Test::SharedKey) { - std::cout << "\n
"; + oss << "\n
"; Benchmark2(t, hertz); } // Public key algorithms if (suites & Test::PublicKey) { - std::cout << "\n
"; + oss << "\n
"; Benchmark3(t, hertz); } g_testEnd = ::time(NULLPTR); - { - StreamState state(std::cout); - std::cout << "\n

Throughput Geometric Average: " << std::setiosflags(std::ios::fixed); - std::cout << std::exp(g_logTotal/(g_logCount > 0.0f ? g_logCount : 1.0f)) << std::endl; - } + oss << "\n

Throughput Geometric Average: " << std::setiosflags(std::ios::fixed); + oss << std::exp(g_logTotal/(g_logCount > 0.0f ? g_logCount : 1.0f)) << std::endl; - std::cout << "\n

Test started at " << TimeToString(g_testBegin); - std::cout << "\n
Test ended at " << TimeToString(g_testEnd); - std::cout << std::endl; + oss << "\n

Test started at " << TimeToString(g_testBegin); + oss << "\n
Test ended at " << TimeToString(g_testEnd); + oss << std::endl; AddHtmlFooter(); + + std::cout << oss.str(); } void Benchmark1(double t, double hertz) -- cgit v1.2.1