bryancall commented on pull request #8615:
URL: https://github.com/apache/trafficserver/pull/8615#issuecomment-1016794293
I used the same type of benchmark to benchmark sha256 and I didn't see a
difference between the old and new APIs:
```
11:14:46 zeus:(master)~/files/src/c/test_code$ g++ benchmark_openssl_sha.cc
-lbenchmark -lssl -lcrypto && ./a.out
2022-01-19T11:15:09-08:00
Running ./a.out
Run on (24 X 4672.07 MHz CPU s)
CPU Caches:
L1 Data 32 KiB (x12)
L1 Instruction 32 KiB (x12)
L2 Unified 512 KiB (x12)
L3 Unified 16384 KiB (x4)
Load Average: 0.02, 0.08, 0.14
***WARNING*** CPU scaling is enabled, the benchmark real time measurements
may be noisy and will incur extra overhead.
-----------------------------------------------------------
Benchmark Time CPU Iterations
-----------------------------------------------------------
openssl_1_sha256 513 ns 512 ns 1364991
openssl_3_sha256 513 ns 513 ns 1364948
```
Updated benchmark code:
```
#include <iostream>
#include <string>
#include <string_view>
#include <openssl/evp.h>
#include <openssl/sha.h>
#include <benchmark/benchmark.h>
//-----------------------------------------------------------------------------
class Data {
public:
Data() {
_data.reserve(_size);
for (int i = 0; i < _size; i++) {
_data.push_back((char)rand() % 256);
}
}
void reset() {
_position = 0;
}
std::string_view read() {
if (_position + 1024 > _size) {
_position = 0;
}
std::string_view tmp = _data.substr(_position, 1024);
_position += 1024;
return tmp;
}
private:
std::string _data;
static constexpr size_t _size = 1 << 20;
size_t _position = 0;
};
Data data;
//-----------------------------------------------------------------------------
class sha256_openssl1
{
private:
SHA256_CTX _ctx;
unsigned char _hash[SHA256_DIGEST_LENGTH];
public:
sha256_openssl1()
{
SHA256_Init(&_ctx);
}
bool update(void const *data, int length)
{
return 0 != SHA256_Update(&_ctx, data, length);
}
unsigned char *finalize()
{
SHA256_Final(_hash, &_ctx);
return _hash;
}
};
//-----------------------------------------------------------------------------
class sha256_openssl3
{
protected:
EVP_MD_CTX *_ctx = nullptr;
unsigned char _hash[SHA256_DIGEST_LENGTH];
public:
sha256_openssl3()
{
_ctx = EVP_MD_CTX_new();
EVP_DigestInit_ex(_ctx, EVP_sha256(), nullptr);
}
~sha256_openssl3() { EVP_MD_CTX_free(_ctx); }
bool update(void const *data, int length)
{
return EVP_DigestUpdate(_ctx, data, length);
}
unsigned char *finalize()
{
EVP_DigestFinal_ex(_ctx, _hash, nullptr);
return _hash;
}
};
//-----------------------------------------------------------------------------
static void openssl_1_sha256(benchmark::State& state) {
sha256_openssl1 sha256;
data.reset();
for (auto _ : state) {
std::string_view tmp = data.read();
sha256.update(tmp.data(), tmp.size());
}
}
//-----------------------------------------------------------------------------
static void openssl_3_sha256(benchmark::State& state) {
sha256_openssl3 sha256;
data.reset();
for (auto _ : state) {
std::string_view tmp = data.read();
sha256.update(tmp.data(), tmp.size());
}
}
//-----------------------------------------------------------------------------
BENCHMARK(openssl_1_sha256);
BENCHMARK(openssl_3_sha256);
BENCHMARK_MAIN();
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]