Skip to content

Instantly share code, notes, and snippets.

@devendranaga
Last active February 20, 2020 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devendranaga/daca125a573d2d2bffbd71ff66917253 to your computer and use it in GitHub Desktop.
Save devendranaga/daca125a573d2d2bffbd71ff66917253 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <memory>
#include <vector>
#include <time.h>
#include <sys/time.h>
namespace Core_Middleware {
class Perf_Item {
public:
explicit Perf_Item(const std::string item_name, int records) :
item_name_ (item_name),
disabled_ (true),
records_ (records) {
}
~Perf_Item() {
}
void Disable() {
disabled_ = true;
}
void Enable() {
disabled_ = false;
}
void Start() {
if (disabled_)
return;
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &start_tv_);
}
void Stop() {
if (disabled_)
return;
double nsec;
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &stop_tv_);
nsec = ((double)(stop_tv_.tv_sec * 1000000000ULL) + stop_tv_.tv_nsec) - ((double)(start_tv_.tv_sec * 1000000000ULL) + start_tv_.tv_nsec);
samples_.push_back(nsec);
cur_samples_ ++;
}
double Report_Avg() {
double sum = 0;
int min = records_ < samples_.size() ? records_ : samples_.size();
for (auto i = 0; i < min; i ++) {
sum += samples_[i];
}
return sum / min;
}
std::string Get_Name() {
return item_name_;
}
int Get_Sample_count() {
return cur_samples_;
}
private:
bool disabled_;
int records_;
std::string item_name_;
int cur_samples_;
struct timespec start_tv_;
struct timespec stop_tv_;
std::vector<double> samples_;
};
class Perf_Analyser {
public:
~Perf_Analyser() { }
static Perf_Analyser *Instance() {
static Perf_Analyser pa;
return &pa;
}
std::shared_ptr<Perf_Item> New_Perf_Item(std::string item_name, int records) {
std::shared_ptr<Perf_Item> pi = std::make_shared<Perf_Item>(item_name, records);
pi_list_.push_back(pi);
return pi;
}
void Dump_Analysis_Results() {
for (auto an : pi_list_) {
printf("perf_item: [%s] samples [%d] nsec: [%f]\n", an->Get_Name().c_str(), an->Get_Sample_count(), an->Report_Avg() / 1000000);
}
}
private:
std::vector<std::shared_ptr<Perf_Item>> pi_list_;
};
}
Core_Middleware::Perf_Analyser *pa = Core_Middleware::Perf_Analyser::Instance();
void f1()
{
int i;
std::shared_ptr<Core_Middleware::Perf_Item> pi2 = pa->New_Perf_Item("f1", 1);
pi2->Enable();
pi2->Start();
double a = 100.1;
for (i = 0; i < 1000000000; i ++) {
a = a * (i + 1000);
}
pi2->Stop();
}
int main()
{
int i;
std::shared_ptr<Core_Middleware::Perf_Item> pi1 = pa->New_Perf_Item("main", 1);
pi1->Enable();
pi1->Start();
double a = 100.1;
for (i = 0; i < 1000000000; i ++) {
a = a * (i + 1000);
}
pi1->Stop();
f1();
pa->Dump_Analysis_Results();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment