throughput_counter.cpp

Go to the documentation of this file.
00001 #ifndef THROUGHPUT_COUNTER_IMPLEMENTATION_FILE
00002 #define THROUGHPUT_COUNTER_IMPLEMENTATION_FILE
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : throughput_counter                                                *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 2000-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #include "throughput_counter.h"
00019 #include "time_stamp.h"
00020 
00021 #include <basis/function.h>
00022 
00023 throughput_counter::throughput_counter()
00024 : _running(false),
00025   _start(new time_stamp),
00026   _end(new time_stamp),
00027   _time_overall(0),
00028   _byte_count(0),
00029   _send_count(0)
00030 {}
00031 
00032 throughput_counter::throughput_counter(const throughput_counter &to_copy)
00033 : _start(new time_stamp),
00034   _end(new time_stamp)
00035 {
00036   *this = to_copy;
00037 }
00038 
00039 throughput_counter::~throughput_counter()
00040 {
00041   _running = false;
00042   WHACK(_start);
00043   WHACK(_end);
00044 }
00045 
00046 throughput_counter &throughput_counter::operator =
00047     (const throughput_counter &to_copy)
00048 {
00049   if (this == &to_copy) return *this;  // bail on copying to self.
00050   _running = to_copy._running;
00051   *_start = *to_copy._start;
00052   *_end = *to_copy._end;
00053   _time_overall = to_copy._time_overall;
00054   _byte_count = to_copy._byte_count;
00055   _send_count = to_copy._send_count;
00056   return *this;
00057 }
00058 
00059 void throughput_counter::combine(const throughput_counter &to_blend)
00060 {
00061   if (this == &to_blend) return;  // no, we don't like that.
00062   _time_overall += to_blend._time_overall;
00063   _byte_count += to_blend._byte_count;
00064   _send_count += to_blend._send_count;
00065 }
00066 
00067 void throughput_counter::start()
00068 {
00069   if (running()) return;  // can't start if already started.
00070   *_start = time_stamp();
00071   *_end = time_stamp();  // just to clear.
00072   _running = true;
00073 }
00074 
00075 void throughput_counter::stop()
00076 {
00077   if (!running()) return;  // better have been started before stopping.
00078   *_end = time_stamp();
00079   _time_overall += _end->value() - _start->value();
00080   _running = false;
00081 }
00082 
00083 void throughput_counter::reset()
00084 {
00085   _running = false;
00086   _start->reset();
00087   _end->reset();
00088   _time_overall = 0;
00089   _byte_count = 0;
00090   _send_count = 0;
00091 }
00092 
00093 void throughput_counter::send(double size_of_send)
00094 {
00095   if (!running()) return;  // can't add if we're not in a run.
00096   _send_count++;
00097   _byte_count += size_of_send;
00098 }
00099 
00100 void throughput_counter::add_run(double size_of_send, double time_of_send,
00101     double number_of_runs)
00102 {
00103   _send_count += number_of_runs;
00104   _byte_count += size_of_send;
00105   _time_overall += time_of_send;
00106 }
00107 
00108 time_stamp throughput_counter::start_time() const { return *_start; }
00109 
00110 time_stamp throughput_counter::stop_time() const { return *_end; }
00111 
00112 double throughput_counter::total_time() const
00113 {
00114   double extra_time = running()? time_stamp().value() - _start->value() : 0;
00115   return _time_overall + extra_time;
00116 }
00117 
00118 double throughput_counter::bytes_per_second() const
00119 {
00120   double total = total_time() / SECOND_ms;
00121   return double(bytes_sent()) / total;
00122 }
00123 
00124 double throughput_counter::kilobytes_per_second() const
00125 { return bytes_per_second() / double(KILOBYTE); }
00126 
00127 double throughput_counter::megabytes_per_second() const
00128 { return kilobytes_per_second() / double(KILOBYTE); }
00129 
00130 
00131 #endif //THROUGHPUT_COUNTER_IMPLEMENTATION_FILE
00132 

Generated on Fri Nov 28 04:29:16 2008 for HOOPLE Libraries by  doxygen 1.5.1