Merge branch 'dev' of feistymeow.org:feisty_meow into dev
[feisty_meow.git] / octopi / library / sockets / throughput_counter.cpp
1
2
3
4 /*****************************************************************************\
5 *                                                                             *
6 *  Name   : throughput_counter                                                *
7 *  Author : Chris Koeritz                                                     *
8 *                                                                             *
9 *******************************************************************************
10 * Copyright (c) 2000-$now By Author.  This program is free software; you can  *
11 * redistribute it and/or modify it under the terms of the GNU General Public  *
12 * License as published by the Free Software Foundation; either version 2 of   *
13 * the License or (at your option) any later version.  This is online at:      *
14 *     http://www.fsf.org/copyleft/gpl.html                                    *
15 * Please send any updates to: fred@gruntose.com                               *
16 \*****************************************************************************/
17
18 #include "throughput_counter.h"
19
20 #include <basis/functions.h>
21
22 using namespace basis;
23 using namespace timely;
24
25 namespace sockets {
26
27 throughput_counter::throughput_counter()
28 : _running(false),
29   _start(new time_stamp),
30   _end(new time_stamp),
31   _time_overall(0),
32   _byte_count(0),
33   _send_count(0)
34 {}
35
36 throughput_counter::throughput_counter(const throughput_counter &to_copy)
37 : _start(new time_stamp),
38   _end(new time_stamp)
39 {
40   *this = to_copy;
41 }
42
43 throughput_counter::~throughput_counter()
44 {
45   _running = false;
46   WHACK(_start);
47   WHACK(_end);
48 }
49
50 throughput_counter &throughput_counter::operator =
51     (const throughput_counter &to_copy)
52 {
53   if (this == &to_copy) return *this;  // bail on copying to self.
54   _running = to_copy._running;
55   *_start = *to_copy._start;
56   *_end = *to_copy._end;
57   _time_overall = to_copy._time_overall;
58   _byte_count = to_copy._byte_count;
59   _send_count = to_copy._send_count;
60   return *this;
61 }
62
63 void throughput_counter::combine(const throughput_counter &to_blend)
64 {
65   if (this == &to_blend) return;  // no, we don't like that.
66   _time_overall += to_blend._time_overall;
67   _byte_count += to_blend._byte_count;
68   _send_count += to_blend._send_count;
69 }
70
71 void throughput_counter::start()
72 {
73   if (running()) return;  // can't start if already started.
74   *_start = time_stamp();
75   *_end = time_stamp();  // just to clear.
76   _running = true;
77 }
78
79 void throughput_counter::stop()
80 {
81   if (!running()) return;  // better have been started before stopping.
82   *_end = time_stamp();
83   _time_overall += _end->value() - _start->value();
84   _running = false;
85 }
86
87 void throughput_counter::reset()
88 {
89   _running = false;
90   _start->reset();
91   _end->reset();
92   _time_overall = 0;
93   _byte_count = 0;
94   _send_count = 0;
95 }
96
97 void throughput_counter::send(double size_of_send)
98 {
99   if (!running()) return;  // can't add if we're not in a run.
100   _send_count++;
101   _byte_count += size_of_send;
102 }
103
104 void throughput_counter::add_run(double size_of_send, double time_of_send,
105     double number_of_runs)
106 {
107   _send_count += number_of_runs;
108   _byte_count += size_of_send;
109   _time_overall += time_of_send;
110 }
111
112 time_stamp throughput_counter::start_time() const { return *_start; }
113
114 time_stamp throughput_counter::stop_time() const { return *_end; }
115
116 double throughput_counter::total_time() const
117 {
118   double extra_time = running()? time_stamp().value() - _start->value() : 0;
119   return _time_overall + extra_time;
120 }
121
122 double throughput_counter::bytes_per_second() const
123 {
124   double total = total_time() / SECOND_ms;
125   return double(bytes_sent()) / total;
126 }
127
128 double throughput_counter::kilobytes_per_second() const
129 { return bytes_per_second() / double(KILOBYTE); }
130
131 double throughput_counter::megabytes_per_second() const
132 { return kilobytes_per_second() / double(KILOBYTE); }
133
134 } //namespace.
135