Skip to content

Instantly share code, notes, and snippets.

@kumagi

kumagi/latencybench.cpp

Last active Nov 4, 2016
Embed
What would you like to do?
#include <fstream>
#include <chrono>
#include <iostream>
#include <vector>
#include <cmath>
#include <unistd.h>
int main(void) {
constexpr int size = 8192;
constexpr int tries = 1024;
std::vector<char> buff(size);
std::vector<double> latencies;
for (int j = 0; j < 10; ++j) {
FILE* fp = fopen("bench.out", "w");
latencies.reserve(tries);
for (int i = 0; i < tries; ++i) {
auto begin = std::chrono::high_resolution_clock::now();
::fwrite(buff.data(), size, 1, fp);
::fflush(fp);
::fdatasync(::fileno(fp));
auto end = std::chrono::high_resolution_clock::now() ;
auto duration = end - begin;
double count = std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count();
latencies.emplace_back(count);
}
::fclose(fp);
double sum = 0;
double sq_sum = 0;
for (const auto& l : latencies) { sum += l; sq_sum += l * l; }
const double avg = sum / tries;
const double std = std::sqrt(sq_sum - (avg * avg));
std::cout << "avg: " << avg << "ns. dev: " << std << std::endl;
latencies.clear();
}
}
avg: 953273ns. dev: 3.05352e+07
avg: 940784ns. dev: 3.01097e+07
avg: 945961ns. dev: 3.0298e+07
avg: 934906ns. dev: 2.99677e+07
avg: 952866ns. dev: 3.05593e+07
avg: 951160ns. dev: 3.04966e+07
avg: 940034ns. dev: 3.02875e+07
avg: 941095ns. dev: 3.01626e+07
avg: 940368ns. dev: 3.01288e+07
avg: 949209ns. dev: 3.04174e+07
avg: 956022ns. dev: 3.20468e+07
avg: 959712ns. dev: 3.08708e+07
avg: 997469ns. dev: 5.07343e+07
avg: 950111ns. dev: 3.04356e+07
avg: 953946ns. dev: 3.05617e+07
avg: 958470ns. dev: 3.14297e+07
avg: 955910ns. dev: 3.06715e+07
avg: 946167ns. dev: 3.03119e+07
avg: 952406ns. dev: 3.09076e+07
avg: 945744ns. dev: 3.03054e+07
avg: 958085ns. dev: 3.07143e+07
avg: 955971ns. dev: 3.06934e+07
avg: 948879ns. dev: 3.03821e+07
avg: 949375ns. dev: 3.04103e+07
avg: 943835ns. dev: 3.0236e+07
avg: 951583ns. dev: 3.23479e+07
avg: 946175ns. dev: 3.04251e+07
avg: 941098ns. dev: 3.05293e+07
avg: 928630ns. dev: 2.98142e+07
avg: 936671ns. dev: 3.01268e+07
avg: 920294ns. dev: 2.9598e+07
avg: 949545ns. dev: 3.04932e+07
avg: 988096ns. dev: 5.11055e+07
avg: 948385ns. dev: 3.06888e+07
avg: 958450ns. dev: 3.07974e+07
avg: 942127ns. dev: 3.02137e+07
avg: 940157ns. dev: 3.01003e+07
avg: 949178ns. dev: 3.05932e+07
avg: 923719ns. dev: 2.97063e+07
avg: 948093ns. dev: 3.26688e+07
avg: 942238ns. dev: 3.05801e+07
avg: 947432ns. dev: 3.09687e+07
avg: 934662ns. dev: 2.99745e+07
avg: 941679ns. dev: 3.02114e+07
avg: 942009ns. dev: 3.04072e+07
avg: 923070ns. dev: 2.95864e+07
avg: 962219ns. dev: 3.45216e+07
avg: 948902ns. dev: 3.05189e+07
avg: 952594ns. dev: 3.08987e+07
avg: 934129ns. dev: 2.99253e+07
avg: 950099ns. dev: 3.05136e+07
avg: 945333ns. dev: 3.02968e+07
avg: 941487ns. dev: 3.02141e+07
avg: 978732ns. dev: 4.99992e+07
avg: 940293ns. dev: 3.01167e+07
avg: 946577ns. dev: 3.03535e+07
avg: 954189ns. dev: 3.19195e+07
avg: 937416ns. dev: 3.00865e+07
avg: 936434ns. dev: 3.00109e+07
avg: 927990ns. dev: 2.99053e+07
avg: 941437ns. dev: 3.02442e+07
avg: 950080ns. dev: 3.18836e+07
avg: 930354ns. dev: 2.98365e+07
avg: 937610ns. dev: 3.00606e+07
avg: 936024ns. dev: 3.00461e+07
avg: 938782ns. dev: 3.00976e+07
avg: 943509ns. dev: 3.02945e+07
avg: 939501ns. dev: 3.00763e+07
avg: 942032ns. dev: 3.02674e+07
avg: 936470ns. dev: 3.00097e+07
avg: 937818ns. dev: 3.01417e+07
avg: 943710ns. dev: 3.02007e+07
avg: 943027ns. dev: 3.02348e+07
avg: 972431ns. dev: 4.88096e+07
avg: 941551ns. dev: 3.02088e+07
avg: 946214ns. dev: 3.03364e+07
avg: 941895ns. dev: 3.02066e+07
avg: 969335ns. dev: 3.29657e+07
avg: 941391ns. dev: 3.02619e+07
avg: 937675ns. dev: 3.0059e+07
avg: 937608ns. dev: 3.05943e+07
avg: 932056ns. dev: 2.9997e+07
avg: 952817ns. dev: 3.09146e+07
avg: 936093ns. dev: 3.00383e+07
avg: 947457ns. dev: 3.05016e+07
avg: 950231ns. dev: 3.0736e+07
avg: 940172ns. dev: 3.01318e+07
avg: 975989ns. dev: 3.28324e+07
avg: 950361ns. dev: 3.05431e+07
avg: 925972ns. dev: 2.98431e+07
avg: 931173ns. dev: 2.99196e+07
avg: 940723ns. dev: 3.01639e+07
avg: 946919ns. dev: 3.15724e+07
avg: 944181ns. dev: 3.02864e+07
avg: 987640ns. dev: 4.9819e+07
avg: 941523ns. dev: 3.02261e+07
avg: 935087ns. dev: 3.00019e+07
avg: 943243ns. dev: 3.0417e+07
avg: 943049ns. dev: 3.01816e+07
avg: 933645ns. dev: 3.00078e+07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment