maskit commented on pull request #7447:
URL: https://github.com/apache/trafficserver/pull/7447#issuecomment-778035256
Seems like we should stick with the old API as long as we can for the best
performance. I'm going to make a change to use the slow new API only if the
fast old API is unavailable.
```
$ ./open
SHA1 comparison (1024 bytes * 100000)
OpenSSL SHA: 111.997 ms 60cacbf3d72e1e7834203da608037b1bf83b40e8
OpenSSL EVP: 190.121 ms 60cacbf3d72e1e7834203da608037b1bf83b40e8
Boost: 2106.630 ms 60cacbf3d72e1e7834203da608037b1bf83b40e8
APR: 1059.868 ms 60cacbf3d72e1e7834203da608037b1bf83b40e8
$ ./boring
SHA1 comparison (1024 bytes * 100000)
BoringSSL SHA: 111.878 ms 60cacbf3d72e1e7834203da608037b1bf83b40e8
BoringSSL EVP: 125.462 ms 60cacbf3d72e1e7834203da608037b1bf83b40e8
Boost: 2110.361 ms 60cacbf3d72e1e7834203da608037b1bf83b40e8
APR: 1075.716 ms 60cacbf3d72e1e7834203da608037b1bf83b40e8
```
```cpp
#include <iostream>
#include <iomanip>
#include <chrono>
#include <openssl/sha.h>
#include <openssl/evp.h>
#include <boost/uuid/detail/sha1.hpp>
#include "apr_sha1.h"
using namespace std;
void sha1_openssl_sha(const unsigned char *in, int in_len, unsigned char
*out, unsigned int *out_len) {
SHA1(in, in_len, out);
*out_len = SHA_DIGEST_LENGTH;
}
void sha1_openssl_evp(const unsigned char *in, int in_len, unsigned char
*out, unsigned int *out_len) {
EVP_Digest(in, in_len, out, out_len, EVP_sha1(), nullptr);
}
void sha1_boost(const unsigned char *in, int in_len, unsigned char *out,
unsigned int *out_len) {
boost::uuids::detail::sha1 sha1;
unsigned int inthash[5];
sha1.process_bytes(in, in_len);
sha1.get_digest(inthash);
for(int i=0;i<5;i++){
out[i*4] = (inthash[i] & 0xFF000000) >> 24;
out[i*4+1] = (inthash[i] & 0x00FF0000) >> 16;
out[i*4+2] = (inthash[i] & 0x0000FF00) >> 8;
out[i*4+3] = (inthash[i] & 0x000000FF);
}
*out_len = 20;
}
void sha1_apr(const unsigned char *in, int in_len, unsigned char *out,
unsigned int *out_len) {
apr_sha1_ctx_t ctx;
apr_sha1_init(&ctx);
apr_sha1_update_binary(&ctx, in, in_len);
apr_sha1_final(out, &ctx);
*out_len = APR_SHA1_DIGESTSIZE;
}
void measure(const char *name, void (*sha1func)(const unsigned char *, int,
unsigned char *, unsigned int *))
{
unsigned char in[1024] = {0};
unsigned char out[20];
unsigned int out_len;
auto start = chrono::high_resolution_clock::now();
for (int i = 0; i < 100000; ++i) {
sha1func(in, sizeof(in), out, &out_len);
}
auto end = chrono::high_resolution_clock::now();
chrono::duration<double, milli> elapsed = end - start;
cout << setw(15) << name << ": " << setw(8) << fixed << setprecision(3)
<< elapsed.count() << " ms ";
auto prev = cout.fill('0');
for (int i = 0; i < out_len; ++i) {
cout << setw(2) << hex << static_cast<unsigned int>(out[i]);
}
cout.fill(prev);
cout << endl;
}
#ifdef OPENSSL_IS_BORINGSSL
#define LIB_NAME "BoringSSL"
#else
#define LIB_NAME "OpenSSL"
#endif
int main(int argc, char **argv) {
cout << "SHA1 comparison (1024 bytes * 100000)" << endl;
measure(LIB_NAME " SHA", sha1_openssl_sha);
measure(LIB_NAME " EVP", sha1_openssl_evp);
measure("Boost", sha1_boost);
measure("APR", sha1_apr);
return 0;
}
```
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]