Skip to content

Instantly share code, notes, and snippets.

@kfur
Created September 15, 2021 12:36
Show Gist options
  • Save kfur/c62fdf764968bb7e016d67f345cf1d4a to your computer and use it in GitHub Desktop.
Save kfur/c62fdf764968bb7e016d67f345cf1d4a to your computer and use it in GitHub Desktop.
Tdlib aes ige benchmark
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/utils/crypto.h"
#include "td/utils/Slice.h"
#include <fstream>
#include <iostream>
#include <unistd.h>
#include <chrono>
std::string data;
std::string key;
std::string iv;
std::string file_to_str(const std::string &file_name) {
std::ifstream ifile(file_name, std::ios::binary | std::ios::ate);
const std::streamsize size = ifile.tellg();
std::vector<char> file_bytes(size);
ifile.seekg(0, std::ios::beg);
ifile.read(file_bytes.data(), size);
return {file_bytes.begin(), file_bytes.end()};
}
void init_data() {
chdir("/Users/kfur/workspace/tdlib/example/cpp");
data = file_to_str("data.in");
key = file_to_str("key.in");
iv = file_to_str("iv.in");
}
using namespace std::chrono;
void bench(const uint count) {
std::cout << "Doing " << count << " iteration(s) with " <<
data.size() / 1024 << " KB of data for tdlib" << std::endl;
std::string crypt(data.size(), '\0');
milliseconds start = duration_cast<milliseconds>(
system_clock::now().time_since_epoch()
);
for (uid_t i = 0; i < count; i++) {
td::aes_ige_encrypt(td::as_slice(key), td::as_mutable_slice(iv), td::as_slice(data), td::as_mutable_slice(crypt));
}
milliseconds enc_end = duration_cast<milliseconds>(
system_clock::now().time_since_epoch()
) - start;
start = duration_cast<milliseconds>(
system_clock::now().time_since_epoch()
);
for (uid_t i = 0; i < count; i++) {
td::aes_ige_decrypt(td::as_slice(key), td::as_mutable_slice(iv), td::as_mutable_slice(crypt), td::as_slice(data));
}
milliseconds dec_end = duration_cast<milliseconds>(
system_clock::now().time_since_epoch()
) - start;
std::cout << "Encrypt took " << enc_end.count() << "ms" << " (avg " << static_cast<double>(enc_end.count()) / static_cast<double>(count) << " ms)" << std::endl;
std::cout << "Decrypt took " << dec_end.count() << "ms" << " (avg " << static_cast<double>(dec_end.count()) / static_cast<double>(count) << " ms)";
}
int main() {
init_data();
bench(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment