Skip to content

Commit

Permalink
Added stats based classes and intergrated them with the factory and w…
Browse files Browse the repository at this point in the history
…orkers
  • Loading branch information
Janitha Karunaratne committed Mar 6, 2014
1 parent 3510557 commit a569eeb
Show file tree
Hide file tree
Showing 10 changed files with 413 additions and 152 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
CC=g++
CFLAGS=-Wall -Wunused -Wextra -g3 -std=c++0x -pedantic
LDFLAGS=
LIBS=-lev -lrt
SOURCES=main.cc common.cc flamethrower.cc params.cc events.cc tcp_factory.cc tcp_worker.cc payload.cc http_worker.cc
LIBS=-lev -lrt -lmsgpack
SOURCES=main.cc common.cc flamethrower.cc params.cc stats.cc tcp_factory.cc tcp_worker.cc payload.cc http_worker.cc
OBJECTS=$(SOURCES:.cc=.o)
EXECUTABLE=flamethrower

Expand Down
2 changes: 1 addition & 1 deletion common.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <ev.h>

#include "params.h"
#include "events.h"
#include "stats.h"

#define DEBUG 1

Expand Down
12 changes: 0 additions & 12 deletions events.cc

This file was deleted.

26 changes: 0 additions & 26 deletions events.h

This file was deleted.

90 changes: 90 additions & 0 deletions stats.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include "stats.h"

////////////////////////////////////////////////////////////////////////////////
#define NS_PER_S 1000000000

uint64_t timestamp_ns_now() {
uint64_t nsec;
timespec tspec;
clock_gettime(CLOCK_REALTIME, &tspec);
nsec = tspec.tv_sec * NS_PER_S + tspec.tv_nsec;
return nsec;
}

////////////////////////////////////////////////////////////////////////////////
StatsList::StatsList() {
}

StatsList::~StatsList() {
}

void StatsList::push(Stats *stats) {

}

////////////////////////////////////////////////////////////////////////////////
Stats::Stats() {

}

Stats::~Stats() {

}

void Stats::print() {

}

////////////////////////////////////////////////////////////////////////////////
TcpFactoryStats::TcpFactoryStats()
: Stats() {
}

TcpFactoryStats::~TcpFactoryStats() {
}

void TcpFactoryStats::print() {

}

////////////////////////////////////////////////////////////////////////////////
TcpWorkerStats::TcpWorkerStats()
: Stats() {

}

TcpWorkerStats::~TcpWorkerStats() {

}

void TcpWorkerStats::print() {

}

////////////////////////////////////////////////////////////////////////////////
TcpServerWorkerStats::TcpServerWorkerStats()
: TcpWorkerStats() {

}

TcpServerWorkerStats::~TcpServerWorkerStats() {

}

void TcpServerWorkerStats::print() {

}

////////////////////////////////////////////////////////////////////////////////
TcpClientWorkerStats::TcpClientWorkerStats()
: TcpWorkerStats() {

}

TcpClientWorkerStats::~TcpClientWorkerStats() {

}

void TcpClientWorkerStats::print() {

}
120 changes: 120 additions & 0 deletions stats.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#ifndef STATS_H
#define STATS_H

#include <ctime>
#include <cstdint>
#include <list>

#include <msgpack.hpp>

uint64_t timestamp_ns_now();

class Stats;

////////////////////////////////////////////////////////////////////////////////
class StatsList {
private:
std::list<Stats*> stats;
public:
StatsList();
virtual ~StatsList();

void push(Stats *stats);
};

////////////////////////////////////////////////////////////////////////////////
class Stats {
public:
Stats();
virtual ~Stats();

virtual void print();
};


////////////////////////////////////////////////////////////////////////////////
class FactoryStats : public Stats {
};

class TcpFactoryStats : public FactoryStats {
public:
uint64_t bytes_in;
uint64_t bytes_out;
uint64_t cumulative_count;

TcpFactoryStats();
virtual ~TcpFactoryStats();

virtual void print();
};

class TcpServerFactoryStats : public TcpFactoryStats {
};

class TcpClientFactoryStats : public TcpFactoryStats {
};


////////////////////////////////////////////////////////////////////////////////
class TcpWorkerStats : public Stats {
public:

uint64_t readable_time;
uint64_t writable_time;
uint64_t close_time;

uint64_t bytes_in;
uint64_t bytes_out;

TcpWorkerStats();
virtual ~TcpWorkerStats();

virtual void print();
};

////////////////////////////////////////////////////////////////////////////////
class TcpServerWorkerStats : public TcpWorkerStats {
public:

uint64_t established_time;

TcpServerWorkerStats();
virtual ~TcpServerWorkerStats();

virtual void print();
};

////////////////////////////////////////////////////////////////////////////////
class TcpClientWorkerStats : public TcpWorkerStats {
public:

uint64_t connect_time;
uint64_t established_time;

TcpClientWorkerStats();
virtual ~TcpClientWorkerStats();

virtual void print();
};

////////////////////////////////////////////////////////////////////////////////
class TcpServerEchoStats : public TcpServerWorkerStats {
};

class TcpClientEchoStats : public TcpClientWorkerStats {
};

class TcpServerRawStats : public TcpServerWorkerStats {
};

class TcpClientRawStats : public TcpClientWorkerStats {
};

class TcpServerHttpStats : public TcpServerWorkerStats {
};

class TcpClientHttpStats : public TcpClientWorkerStats {
};


#endif
50 changes: 29 additions & 21 deletions tcp_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
Factory::Factory(struct ev_loop *loop,
FactoryParams &params)
: loop(loop),
params(params) {

params(params),
statslist() {
}

Factory::~Factory() {
Expand All @@ -16,10 +16,14 @@ Factory* Factory::maker(struct ev_loop *loop, FactoryParams &params) {

switch(params.type) {
case FactoryParams::FactoryType::TCP_SERVER:
return new TcpServerFactory(loop, (TcpServerFactoryParams&)params);
return new TcpServerFactory(loop,
(TcpServerFactoryParams&)params,
*new TcpServerFactoryStats());
break;
case FactoryParams::FactoryType::TCP_CLIENT:
return new TcpClientFactory(loop, (TcpClientFactoryParams&)params);
return new TcpClientFactory(loop,
(TcpClientFactoryParams&)params,
*new TcpClientFactoryStats());
break;
default:
perror("error: invalid factory type\n");
Expand All @@ -30,13 +34,11 @@ Factory* Factory::maker(struct ev_loop *loop, FactoryParams &params) {

////////////////////////////////////////////////////////////////////////////////
TcpFactory::TcpFactory(struct ev_loop *loop,
TcpFactoryParams &params)
TcpFactoryParams &params,
TcpFactoryStats &stats)
: Factory(loop, params),
params(params),
bytes_in(0),
bytes_out(0),
cumulative_count(0)
{
stats(stats) {

debug_print("ctor\n");

Expand Down Expand Up @@ -64,6 +66,7 @@ TcpFactory::~TcpFactory() {
workers.pop_front();
}

delete &stats;
}

void TcpFactory::factory_cb(struct ev_loop *loop,
Expand Down Expand Up @@ -97,28 +100,31 @@ void TcpFactory::stats_cb() {
"bytes_out=%lu "
"count=%lu "
"workers=%lu\n",
bytes_in,
bytes_out,
cumulative_count,
stats.bytes_in,
stats.bytes_out,
stats.cumulative_count,
workers.size());
}

void TcpFactory::worker_new_cb(TcpWorker &worker) {
workers.push_back(&worker);
worker.workers_list_pos = --workers.end();
cumulative_count++;
stats.cumulative_count++;
}

void TcpFactory::worker_delete_cb(TcpWorker &worker) {
workers.erase(worker.workers_list_pos);
statslist.push(&worker.stats);
ev_async_send(loop, &factory_async);
}

////////////////////////////////////////////////////////////////////////////////
TcpServerFactory::TcpServerFactory(struct ev_loop *loop,
TcpServerFactoryParams &params)
: TcpFactory(loop, params),
params(params) {
TcpServerFactoryParams &params,
TcpServerFactoryStats &stats)
: TcpFactory(loop, params, stats),
params(params),
stats(stats) {

debug_print("ctor\n");

Expand Down Expand Up @@ -187,7 +193,7 @@ void TcpServerFactory::accept_cb(struct ev_loop *loop,
void TcpServerFactory::accept_cb() {

// TODO(Janitha): Maybe this is better done in the worker_new_cb?
if(cumulative_count >= params.count) {
if(stats.cumulative_count >= params.count) {
debug_print("cumulative count reached\n");
ev_io_stop(loop, &accept_watcher);
ev_async_stop(loop, &factory_async);
Expand Down Expand Up @@ -228,9 +234,11 @@ void TcpServerFactory::factory_cb() {

////////////////////////////////////////////////////////////////////////////////
TcpClientFactory::TcpClientFactory(struct ev_loop *loop,
TcpClientFactoryParams &params)
: TcpFactory(loop, params),
params(params) {
TcpClientFactoryParams &params,
TcpClientFactoryStats &stats)
: TcpFactory(loop, params, stats),
params(params),
stats(stats) {

debug_print("ctor\n");

Expand All @@ -245,7 +253,7 @@ void TcpClientFactory::factory_cb() {
debug_print("called\n");

// TODO(Janitha): Maybe this is better done in the worker_new_cb?
if(cumulative_count >= params.count) {
if(stats.cumulative_count >= params.count) {
debug_print("cumulative count reached\n");
ev_async_stop(loop, &factory_async);
return;
Expand Down
Loading

0 comments on commit a569eeb

Please sign in to comment.