4 /*****************************************************************************\
6 * Name : throughput_counter *
7 * Author : Chris Koeritz *
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 \*****************************************************************************/
18 #include "throughput_counter.h"
20 #include <basis/functions.h>
22 using namespace basis;
23 using namespace timely;
27 throughput_counter::throughput_counter()
29 _start(new time_stamp),
36 throughput_counter::throughput_counter(const throughput_counter &to_copy)
37 : _start(new time_stamp),
43 throughput_counter::~throughput_counter()
50 throughput_counter &throughput_counter::operator =
51 (const throughput_counter &to_copy)
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;
63 void throughput_counter::combine(const throughput_counter &to_blend)
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;
71 void throughput_counter::start()
73 if (running()) return; // can't start if already started.
74 *_start = time_stamp();
75 *_end = time_stamp(); // just to clear.
79 void throughput_counter::stop()
81 if (!running()) return; // better have been started before stopping.
83 _time_overall += _end->value() - _start->value();
87 void throughput_counter::reset()
97 void throughput_counter::send(double size_of_send)
99 if (!running()) return; // can't add if we're not in a run.
101 _byte_count += size_of_send;
104 void throughput_counter::add_run(double size_of_send, double time_of_send,
105 double number_of_runs)
107 _send_count += number_of_runs;
108 _byte_count += size_of_send;
109 _time_overall += time_of_send;
112 time_stamp throughput_counter::start_time() const { return *_start; }
114 time_stamp throughput_counter::stop_time() const { return *_end; }
116 double throughput_counter::total_time() const
118 double extra_time = running()? time_stamp().value() - _start->value() : 0;
119 return _time_overall + extra_time;
122 double throughput_counter::bytes_per_second() const
124 double total = total_time() / SECOND_ms;
125 return double(bytes_sent()) / total;
128 double throughput_counter::kilobytes_per_second() const
129 { return bytes_per_second() / double(KILOBYTE); }
131 double throughput_counter::megabytes_per_second() const
132 { return kilobytes_per_second() / double(KILOBYTE); }