Commit 382321d2 authored by Kirill Smelkov's avatar Kirill Smelkov
parent 25a0bb86
#include "timer-wheel.h"
#include <sys/time.h>
#include <stdlib.h>
#include <functional>
#include <string>
using std::vector;
using std::string;
typedef std::function<void()> Callback;
typedef TimerEvent<Callback> KTimer;
static TimerWheel timer_wheel;
void on_timer() {
}
double microtime() {
int err;
struct timeval tv;
err = gettimeofday(&tv, NULL);
if (err == -1) {
perror("gettimeofday");
abort();
}
return tv.tv_sec + 1E-6 * tv.tv_usec;
}
void bench_add(int N) {
vector<KTimer*> vt;
int i;
for (i=0; i<N; i++) {
auto t = new KTimer(on_timer);
vt.push_back(t);
}
auto Tstart = microtime();
for (i=0; i<N; i++) {
Tick dt = 1+i;
timer_wheel.schedule(vt[i], dt);
}
auto Tend = microtime();
printf("add %d\t%.3f μs\n", N, (Tend-Tstart)/1e-6);
}
void bench_del(int N) {
vector<KTimer*> vt;
int i;
for (i=0; i<N; i++) {
auto t = new KTimer(on_timer);
vt.push_back(t);
Tick dt = 1+i;
timer_wheel.schedule(vt[i], dt);
}
auto Tstart = microtime();
for (i=0; i<N; i++) {
vt[i]->cancel();
}
auto Tend = microtime();
printf("del %d\t%.3f μs\n", N, (Tend-Tstart)/1e-6);
}
void bench_exp(int N, Tick dt_step) {
vector<KTimer*> vt;
int i;
Tick t;
for (i=0; i<N; i++) {
auto t = new KTimer(on_timer);
vt.push_back(t);
Tick dt = 1+i;
timer_wheel.schedule(vt[i], dt);
}
auto Tstart = microtime();
for (t=0; t<(Tick)N; t += dt_step) {
timer_wheel.advance(dt_step);
}
auto Tend = microtime();
printf("exp %d\t%.3f μs\n", N, (Tend-Tstart)/1e-6);
}
int main(int argc, char *argv[]) {
string what = argv[1];
int N = atoi(argv[2]);
if (what == "add") {
bench_add(N);
}
else if (what == "del") {
bench_del(N);
}
else if (what == "exp") {
bench_exp(N, Tick(N*0.0001));
}
else {
abort();
}
}
#/bin/bash -e
rm -f ratas-{add,del,exp}.dat
for N in `seq 0 10000 1000000`; do
./kbench add $N >> ratas-add.dat
./kbench del $N >> ratas-del.dat
./kbench exp $N >> ratas-exp.dat
done
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment