2008-07-01 20:59:13 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2008, Arvid Norberg
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2007-07-03 01:48:06 +02:00
|
|
|
#include "test.hpp"
|
|
|
|
|
|
|
|
#include "libtorrent/bandwidth_manager.hpp"
|
2008-01-17 18:40:46 +01:00
|
|
|
#include "libtorrent/bandwidth_queue_entry.hpp"
|
|
|
|
#include "libtorrent/bandwidth_limit.hpp"
|
2009-09-16 06:41:35 +02:00
|
|
|
#include "libtorrent/bandwidth_socket.hpp"
|
2007-07-03 01:48:06 +02:00
|
|
|
#include "libtorrent/socket.hpp"
|
|
|
|
#include "libtorrent/stat.hpp"
|
|
|
|
#include "libtorrent/time.hpp"
|
2014-07-06 21:18:00 +02:00
|
|
|
#include "libtorrent/aux_/session_settings.hpp"
|
2007-07-03 01:48:06 +02:00
|
|
|
|
2016-07-10 05:17:55 +02:00
|
|
|
#include <cmath>
|
2016-05-25 06:31:52 +02:00
|
|
|
#include <functional>
|
|
|
|
#include <iostream>
|
2016-07-10 13:34:45 +02:00
|
|
|
#include <utility>
|
2007-07-03 01:48:06 +02:00
|
|
|
|
|
|
|
struct torrent;
|
|
|
|
struct peer_connection;
|
|
|
|
|
|
|
|
using namespace libtorrent;
|
2016-05-25 06:31:52 +02:00
|
|
|
using namespace std::placeholders;
|
2007-07-03 01:48:06 +02:00
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
const float sample_time = 20.f; // seconds
|
2007-08-27 01:40:58 +02:00
|
|
|
|
2007-08-27 12:20:23 +02:00
|
|
|
//#define VERBOSE_LOGGING
|
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
bandwidth_channel global_bwc;
|
2008-01-17 18:40:46 +01:00
|
|
|
|
2016-09-01 03:42:18 +02:00
|
|
|
struct peer_connection: bandwidth_socket, std::enable_shared_from_this<peer_connection>
|
2008-01-17 18:40:46 +01:00
|
|
|
{
|
2009-09-16 06:41:35 +02:00
|
|
|
peer_connection(bandwidth_manager& bwm
|
2009-04-26 02:21:59 +02:00
|
|
|
, bandwidth_channel& torrent_bwc, int prio, bool ignore_limits, std::string name)
|
|
|
|
: m_bwm(bwm)
|
|
|
|
, m_torrent_bandwidth_channel(torrent_bwc)
|
|
|
|
, m_priority(prio)
|
|
|
|
, m_ignore_limits(ignore_limits)
|
2016-07-10 13:34:45 +02:00
|
|
|
, m_name(std::move(name))
|
2009-04-26 02:21:59 +02:00
|
|
|
, m_quota(0)
|
|
|
|
{}
|
2008-01-17 18:40:46 +01:00
|
|
|
|
2016-07-10 02:10:38 +02:00
|
|
|
bool is_disconnecting() const override { return false; }
|
2008-01-17 18:40:46 +01:00
|
|
|
bool ignore_bandwidth_limits() { return m_ignore_limits; }
|
2016-07-10 02:10:38 +02:00
|
|
|
void assign_bandwidth(int channel, int amount) override;
|
2009-04-26 02:21:59 +02:00
|
|
|
|
|
|
|
void throttle(int limit) { m_bandwidth_channel.throttle(limit); }
|
|
|
|
|
2008-01-17 18:40:46 +01:00
|
|
|
void start();
|
|
|
|
|
2009-09-16 06:41:35 +02:00
|
|
|
bandwidth_manager& m_bwm;
|
2008-01-17 18:40:46 +01:00
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
bandwidth_channel m_bandwidth_channel;
|
|
|
|
bandwidth_channel& m_torrent_bandwidth_channel;
|
2008-01-17 18:40:46 +01:00
|
|
|
|
|
|
|
int m_priority;
|
|
|
|
bool m_ignore_limits;
|
|
|
|
std::string m_name;
|
2009-04-26 02:21:59 +02:00
|
|
|
int m_quota;
|
2008-01-17 18:40:46 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void peer_connection::assign_bandwidth(int channel, int amount)
|
2007-07-03 01:48:06 +02:00
|
|
|
{
|
2009-04-26 02:21:59 +02:00
|
|
|
m_quota += amount;
|
2007-08-27 12:20:23 +02:00
|
|
|
#ifdef VERBOSE_LOGGING
|
2009-04-26 02:21:59 +02:00
|
|
|
std::cerr << " [" << m_name
|
2008-01-17 18:40:46 +01:00
|
|
|
<< "] assign bandwidth, " << amount << std::endl;
|
2007-08-27 12:20:23 +02:00
|
|
|
#endif
|
2008-01-17 18:40:46 +01:00
|
|
|
TEST_CHECK(amount > 0);
|
2009-04-26 02:21:59 +02:00
|
|
|
start();
|
2008-01-17 18:40:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void peer_connection::start()
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
bandwidth_channel* channels[] = {
|
|
|
|
&m_bandwidth_channel
|
2009-04-26 02:21:59 +02:00
|
|
|
, &m_torrent_bandwidth_channel
|
2014-07-06 21:18:00 +02:00
|
|
|
, &global_bwc
|
|
|
|
};
|
|
|
|
|
|
|
|
m_bwm.request_bandwidth(shared_from_this(), 400000000, m_priority, channels, 3);
|
2008-01-17 18:40:46 +01:00
|
|
|
}
|
2007-12-31 10:41:50 +01:00
|
|
|
|
2007-07-03 01:48:06 +02:00
|
|
|
|
2016-09-01 03:42:18 +02:00
|
|
|
typedef std::vector<std::shared_ptr<peer_connection>> connections_t;
|
2007-07-03 01:48:06 +02:00
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
void do_change_rate(bandwidth_channel& t1, bandwidth_channel& t2, int limit)
|
2007-07-03 01:48:06 +02:00
|
|
|
{
|
2009-04-26 02:21:59 +02:00
|
|
|
static int counter = 10;
|
|
|
|
--counter;
|
2007-12-31 10:41:50 +01:00
|
|
|
if (counter == 0)
|
|
|
|
{
|
2009-04-26 02:21:59 +02:00
|
|
|
t1.throttle(limit);
|
|
|
|
t2.throttle(limit);
|
2007-12-31 10:41:50 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
t1.throttle(limit + limit / 2 * ((counter & 1)?-1:1));
|
|
|
|
t2.throttle(limit + limit / 2 * ((counter & 1)?1:-1));
|
2007-12-31 10:41:50 +01:00
|
|
|
}
|
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
void do_change_peer_rate(connections_t& v, int limit)
|
2007-12-31 10:41:50 +01:00
|
|
|
{
|
2009-04-26 02:21:59 +02:00
|
|
|
static int count = 10;
|
|
|
|
--count;
|
|
|
|
if (count == 0)
|
2007-12-31 10:41:50 +01:00
|
|
|
{
|
|
|
|
std::for_each(v.begin(), v.end()
|
2016-05-25 06:31:52 +02:00
|
|
|
, std::bind(&peer_connection::throttle, _1, limit));
|
2007-12-31 10:41:50 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
int c = count;
|
2007-12-31 10:41:50 +01:00
|
|
|
for (connections_t::iterator i = v.begin(); i != v.end(); ++i, ++c)
|
|
|
|
i->get()->throttle(limit + limit / 2 * ((c & 1)?-1:1));
|
|
|
|
}
|
|
|
|
|
2015-05-30 06:31:23 +02:00
|
|
|
static void nop() {}
|
2009-04-26 02:21:59 +02:00
|
|
|
|
|
|
|
void run_test(connections_t& v
|
2009-09-16 06:41:35 +02:00
|
|
|
, bandwidth_manager& manager
|
2016-08-13 03:31:55 +02:00
|
|
|
, std::function<void()> f = &nop)
|
2007-07-03 01:48:06 +02:00
|
|
|
{
|
2007-12-31 10:41:50 +01:00
|
|
|
std::cerr << "-------------" << std::endl;
|
2008-11-05 04:17:41 +01:00
|
|
|
|
2007-07-03 01:48:06 +02:00
|
|
|
std::for_each(v.begin(), v.end()
|
2016-05-25 06:31:52 +02:00
|
|
|
, std::bind(&peer_connection::start, _1));
|
2007-07-03 01:48:06 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
libtorrent::aux::session_settings s;
|
|
|
|
initialize_default_settings(s);
|
|
|
|
int tick_interval = s.get_int(settings_pack::tick_interval);
|
2014-01-13 07:49:47 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < int(sample_time * 1000 / tick_interval); ++i)
|
2009-04-26 02:21:59 +02:00
|
|
|
{
|
2014-01-13 07:49:47 +01:00
|
|
|
manager.update_quotas(milliseconds(tick_interval));
|
2009-04-26 02:21:59 +02:00
|
|
|
if ((i % 15) == 0) f();
|
|
|
|
}
|
2007-07-03 01:48:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool close_to(float val, float comp, float err)
|
|
|
|
{
|
|
|
|
return fabs(val - comp) <= err;
|
|
|
|
}
|
|
|
|
|
2009-09-16 06:41:35 +02:00
|
|
|
void spawn_connections(connections_t& v, bandwidth_manager& bwm
|
2009-04-26 02:21:59 +02:00
|
|
|
, bandwidth_channel& bwc, int num, char const* prefix)
|
2007-07-03 01:48:06 +02:00
|
|
|
{
|
|
|
|
for (int i = 0; i < num; ++i)
|
|
|
|
{
|
2009-09-18 09:00:40 +02:00
|
|
|
char name[200];
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(name, sizeof(name), "%s%d", prefix, i);
|
2016-09-01 03:42:18 +02:00
|
|
|
v.push_back(std::make_shared<peer_connection>(bwm, bwc, 200, false, name));
|
2007-07-03 01:48:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_equal_connections(int num, int limit)
|
|
|
|
{
|
2007-12-31 10:41:50 +01:00
|
|
|
std::cerr << "\ntest equal connections " << num << " " << limit << std::endl;
|
2009-09-16 06:41:35 +02:00
|
|
|
bandwidth_manager manager(0);
|
2009-04-26 02:21:59 +02:00
|
|
|
global_bwc.throttle(limit);
|
2007-07-03 01:48:06 +02:00
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
bandwidth_channel t1;
|
2007-07-03 01:48:06 +02:00
|
|
|
|
|
|
|
connections_t v;
|
2009-04-26 02:21:59 +02:00
|
|
|
spawn_connections(v, manager, t1, num, "p");
|
|
|
|
run_test(v, manager);
|
2007-07-03 01:48:06 +02:00
|
|
|
|
|
|
|
float sum = 0.f;
|
2007-07-06 04:38:21 +02:00
|
|
|
float err = (std::max)(limit / num * 0.3f, 1000.f);
|
2007-07-03 01:48:06 +02:00
|
|
|
for (connections_t::iterator i = v.begin()
|
|
|
|
, end(v.end()); i != end; ++i)
|
|
|
|
{
|
2009-04-26 02:21:59 +02:00
|
|
|
sum += (*i)->m_quota;
|
2007-07-03 01:48:06 +02:00
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
std::cerr << (*i)->m_quota / sample_time
|
2007-07-03 01:48:06 +02:00
|
|
|
<< " target: " << (limit / num) << " eps: " << err << std::endl;
|
2016-04-20 06:45:32 +02:00
|
|
|
TEST_CHECK(close_to((*i)->m_quota / sample_time, float(limit) / num, err));
|
2007-07-03 01:48:06 +02:00
|
|
|
}
|
2007-08-27 01:40:58 +02:00
|
|
|
sum /= sample_time;
|
2007-07-03 01:48:06 +02:00
|
|
|
std::cerr << "sum: " << sum << " target: " << limit << std::endl;
|
2007-08-27 12:20:23 +02:00
|
|
|
TEST_CHECK(sum > 0);
|
2016-04-20 06:45:32 +02:00
|
|
|
TEST_CHECK(close_to(sum, float(limit), 50));
|
2007-07-03 01:48:06 +02:00
|
|
|
}
|
|
|
|
|
2007-12-31 10:41:50 +01:00
|
|
|
void test_connections_variable_rate(int num, int limit, int torrent_limit)
|
|
|
|
{
|
|
|
|
std::cerr << "\ntest connections variable rate" << num
|
|
|
|
<< " l: " << limit
|
|
|
|
<< " t: " << torrent_limit
|
|
|
|
<< std::endl;
|
2009-09-16 06:41:35 +02:00
|
|
|
bandwidth_manager manager(0);
|
2009-04-26 02:21:59 +02:00
|
|
|
global_bwc.throttle(0);
|
2007-12-31 10:41:50 +01:00
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
bandwidth_channel t1;
|
2007-12-31 10:41:50 +01:00
|
|
|
if (torrent_limit)
|
2009-04-26 02:21:59 +02:00
|
|
|
t1.throttle(torrent_limit);
|
2007-12-31 10:41:50 +01:00
|
|
|
|
|
|
|
connections_t v;
|
2009-04-26 02:21:59 +02:00
|
|
|
spawn_connections(v, manager, t1, num, "p");
|
2007-12-31 10:41:50 +01:00
|
|
|
std::for_each(v.begin(), v.end()
|
2016-05-25 06:31:52 +02:00
|
|
|
, std::bind(&peer_connection::throttle, _1, limit));
|
2007-12-31 10:41:50 +01:00
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
run_test(v, manager, std::bind(&do_change_peer_rate
|
2016-08-13 03:31:55 +02:00
|
|
|
, std::ref(v), limit));
|
2007-12-31 10:41:50 +01:00
|
|
|
|
|
|
|
if (torrent_limit > 0 && limit * num > torrent_limit)
|
|
|
|
limit = torrent_limit / num;
|
|
|
|
|
|
|
|
float sum = 0.f;
|
|
|
|
float err = limit * 0.3f;
|
|
|
|
for (connections_t::iterator i = v.begin()
|
|
|
|
, end(v.end()); i != end; ++i)
|
|
|
|
{
|
2009-04-26 02:21:59 +02:00
|
|
|
sum += (*i)->m_quota;
|
2007-12-31 10:41:50 +01:00
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
std::cerr << (*i)->m_quota / sample_time
|
2007-12-31 10:41:50 +01:00
|
|
|
<< " target: " << limit << " eps: " << err << std::endl;
|
2016-04-20 06:45:32 +02:00
|
|
|
TEST_CHECK(close_to((*i)->m_quota / sample_time, float(limit), err));
|
2007-12-31 10:41:50 +01:00
|
|
|
}
|
|
|
|
sum /= sample_time;
|
|
|
|
std::cerr << "sum: " << sum << " target: " << (limit * num) << std::endl;
|
|
|
|
TEST_CHECK(sum > 0);
|
2016-04-20 06:45:32 +02:00
|
|
|
TEST_CHECK(close_to(sum, float(limit) * num, limit * 0.3f * num));
|
2007-12-31 10:41:50 +01:00
|
|
|
}
|
|
|
|
|
2007-07-03 01:48:06 +02:00
|
|
|
void test_single_peer(int limit, bool torrent_limit)
|
|
|
|
{
|
2007-12-31 10:41:50 +01:00
|
|
|
std::cerr << "\ntest single peer " << limit << " " << torrent_limit << std::endl;
|
2009-09-16 06:41:35 +02:00
|
|
|
bandwidth_manager manager(0);
|
2009-04-26 02:21:59 +02:00
|
|
|
bandwidth_channel t1;
|
|
|
|
global_bwc.throttle(0);
|
2007-07-03 01:48:06 +02:00
|
|
|
|
|
|
|
if (torrent_limit)
|
2009-04-26 02:21:59 +02:00
|
|
|
t1.throttle(limit);
|
2007-07-03 01:48:06 +02:00
|
|
|
else
|
2009-04-26 02:21:59 +02:00
|
|
|
global_bwc.throttle(limit);
|
2007-07-03 01:48:06 +02:00
|
|
|
|
|
|
|
connections_t v;
|
2009-04-26 02:21:59 +02:00
|
|
|
spawn_connections(v, manager, t1, 1, "p");
|
|
|
|
run_test(v, manager);
|
2007-07-03 01:48:06 +02:00
|
|
|
|
|
|
|
float sum = 0.f;
|
|
|
|
for (connections_t::iterator i = v.begin()
|
|
|
|
, end(v.end()); i != end; ++i)
|
|
|
|
{
|
2009-04-26 02:21:59 +02:00
|
|
|
sum += (*i)->m_quota;
|
2007-07-03 01:48:06 +02:00
|
|
|
}
|
2007-08-27 01:40:58 +02:00
|
|
|
sum /= sample_time;
|
2007-07-03 01:48:06 +02:00
|
|
|
std::cerr << sum << " target: " << limit << std::endl;
|
2007-08-27 12:20:23 +02:00
|
|
|
TEST_CHECK(sum > 0);
|
2016-04-20 06:45:32 +02:00
|
|
|
TEST_CHECK(close_to(sum, float(limit), 1000));
|
2007-07-03 01:48:06 +02:00
|
|
|
}
|
|
|
|
|
2007-08-27 01:40:58 +02:00
|
|
|
void test_torrents(int num, int limit1, int limit2, int global_limit)
|
2007-07-03 01:48:06 +02:00
|
|
|
{
|
2007-12-31 10:41:50 +01:00
|
|
|
std::cerr << "\ntest equal torrents " << num
|
|
|
|
<< " l1: " << limit1
|
|
|
|
<< " l2: " << limit2
|
|
|
|
<< " g: " << global_limit << std::endl;
|
2009-09-16 06:41:35 +02:00
|
|
|
bandwidth_manager manager(0);
|
2009-04-26 02:21:59 +02:00
|
|
|
global_bwc.throttle(global_limit);
|
2007-07-03 01:48:06 +02:00
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
bandwidth_channel t1;
|
|
|
|
bandwidth_channel t2;
|
2007-07-03 01:48:06 +02:00
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
t1.throttle(limit1);
|
|
|
|
t2.throttle(limit2);
|
2007-07-03 01:48:06 +02:00
|
|
|
|
|
|
|
connections_t v1;
|
2009-04-26 02:21:59 +02:00
|
|
|
spawn_connections(v1, manager, t1, num, "t1p");
|
2007-07-03 01:48:06 +02:00
|
|
|
connections_t v2;
|
2009-04-26 02:21:59 +02:00
|
|
|
spawn_connections(v2, manager, t2, num, "t2p");
|
2007-07-03 01:48:06 +02:00
|
|
|
connections_t v;
|
|
|
|
std::copy(v1.begin(), v1.end(), std::back_inserter(v));
|
|
|
|
std::copy(v2.begin(), v2.end(), std::back_inserter(v));
|
2009-04-26 02:21:59 +02:00
|
|
|
run_test(v, manager);
|
2007-07-03 01:48:06 +02:00
|
|
|
|
2007-12-31 10:41:50 +01:00
|
|
|
if (global_limit > 0 && global_limit < limit1 + limit2)
|
|
|
|
{
|
|
|
|
limit1 = (std::min)(limit1, global_limit / 2);
|
|
|
|
limit2 = global_limit - limit1;
|
|
|
|
}
|
2007-07-03 01:48:06 +02:00
|
|
|
float sum = 0.f;
|
|
|
|
for (connections_t::iterator i = v1.begin()
|
|
|
|
, end(v1.end()); i != end; ++i)
|
|
|
|
{
|
2009-04-26 02:21:59 +02:00
|
|
|
sum += (*i)->m_quota;
|
2007-07-03 01:48:06 +02:00
|
|
|
}
|
2007-08-27 01:40:58 +02:00
|
|
|
sum /= sample_time;
|
|
|
|
std::cerr << sum << " target: " << limit1 << std::endl;
|
2007-08-27 12:20:23 +02:00
|
|
|
TEST_CHECK(sum > 0);
|
2016-04-20 06:45:32 +02:00
|
|
|
TEST_CHECK(close_to(sum, float(limit1), 1000));
|
2007-07-03 01:48:06 +02:00
|
|
|
|
|
|
|
sum = 0.f;
|
|
|
|
for (connections_t::iterator i = v2.begin()
|
|
|
|
, end(v2.end()); i != end; ++i)
|
|
|
|
{
|
2009-04-26 02:21:59 +02:00
|
|
|
sum += (*i)->m_quota;
|
2007-07-03 01:48:06 +02:00
|
|
|
}
|
2007-08-27 01:40:58 +02:00
|
|
|
sum /= sample_time;
|
|
|
|
std::cerr << sum << " target: " << limit2 << std::endl;
|
2007-08-27 12:20:23 +02:00
|
|
|
TEST_CHECK(sum > 0);
|
2016-04-20 06:45:32 +02:00
|
|
|
TEST_CHECK(close_to(sum, float(limit2), 1000));
|
2007-07-03 01:48:06 +02:00
|
|
|
}
|
|
|
|
|
2007-12-31 10:41:50 +01:00
|
|
|
void test_torrents_variable_rate(int num, int limit, int global_limit)
|
|
|
|
{
|
|
|
|
std::cerr << "\ntest torrents variable rate" << num
|
|
|
|
<< " l: " << limit
|
|
|
|
<< " g: " << global_limit << std::endl;
|
2009-09-16 06:41:35 +02:00
|
|
|
bandwidth_manager manager(0);
|
2009-04-26 02:21:59 +02:00
|
|
|
global_bwc.throttle(global_limit);
|
2007-12-31 10:41:50 +01:00
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
bandwidth_channel t1;
|
|
|
|
bandwidth_channel t2;
|
2007-12-31 10:41:50 +01:00
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
t1.throttle(limit);
|
|
|
|
t2.throttle(limit);
|
2007-12-31 10:41:50 +01:00
|
|
|
|
|
|
|
connections_t v1;
|
2009-04-26 02:21:59 +02:00
|
|
|
spawn_connections(v1, manager, t1, num, "t1p");
|
2007-12-31 10:41:50 +01:00
|
|
|
connections_t v2;
|
2009-04-26 02:21:59 +02:00
|
|
|
spawn_connections(v2, manager, t2, num, "t2p");
|
2007-12-31 10:41:50 +01:00
|
|
|
connections_t v;
|
|
|
|
std::copy(v1.begin(), v1.end(), std::back_inserter(v));
|
|
|
|
std::copy(v2.begin(), v2.end(), std::back_inserter(v));
|
|
|
|
|
2016-08-13 03:31:55 +02:00
|
|
|
run_test(v, manager, std::bind(&do_change_rate, std::ref(t1), std::ref(t2), limit));
|
2007-12-31 10:41:50 +01:00
|
|
|
|
|
|
|
if (global_limit > 0 && global_limit < 2 * limit)
|
|
|
|
limit = global_limit / 2;
|
|
|
|
|
|
|
|
float sum = 0.f;
|
|
|
|
for (connections_t::iterator i = v1.begin()
|
|
|
|
, end(v1.end()); i != end; ++i)
|
|
|
|
{
|
2009-04-26 02:21:59 +02:00
|
|
|
sum += (*i)->m_quota;
|
2007-12-31 10:41:50 +01:00
|
|
|
}
|
|
|
|
sum /= sample_time;
|
|
|
|
std::cerr << sum << " target: " << limit << std::endl;
|
|
|
|
TEST_CHECK(sum > 0);
|
2016-04-20 06:45:32 +02:00
|
|
|
TEST_CHECK(close_to(sum, float(limit), 1000));
|
2007-12-31 10:41:50 +01:00
|
|
|
|
|
|
|
sum = 0.f;
|
|
|
|
for (connections_t::iterator i = v2.begin()
|
|
|
|
, end(v2.end()); i != end; ++i)
|
|
|
|
{
|
2009-04-26 02:21:59 +02:00
|
|
|
sum += (*i)->m_quota;
|
2007-12-31 10:41:50 +01:00
|
|
|
}
|
|
|
|
sum /= sample_time;
|
|
|
|
std::cerr << sum << " target: " << limit << std::endl;
|
|
|
|
TEST_CHECK(sum > 0);
|
2016-04-20 06:45:32 +02:00
|
|
|
TEST_CHECK(close_to(sum, float(limit), 1000));
|
2007-12-31 10:41:50 +01:00
|
|
|
}
|
|
|
|
|
2007-07-03 01:48:06 +02:00
|
|
|
void test_peer_priority(int limit, bool torrent_limit)
|
|
|
|
{
|
2007-12-31 10:41:50 +01:00
|
|
|
std::cerr << "\ntest peer priority " << limit << " " << torrent_limit << std::endl;
|
2009-09-16 06:41:35 +02:00
|
|
|
bandwidth_manager manager(0);
|
2009-04-26 02:21:59 +02:00
|
|
|
bandwidth_channel t1;
|
|
|
|
global_bwc.throttle(0);
|
2007-07-03 01:48:06 +02:00
|
|
|
|
|
|
|
if (torrent_limit)
|
2009-04-26 02:21:59 +02:00
|
|
|
t1.throttle(limit);
|
2007-07-03 01:48:06 +02:00
|
|
|
else
|
2009-04-26 02:21:59 +02:00
|
|
|
global_bwc.throttle(limit);
|
2007-07-03 01:48:06 +02:00
|
|
|
|
|
|
|
connections_t v1;
|
2009-04-26 02:21:59 +02:00
|
|
|
spawn_connections(v1, manager, t1, 10, "p");
|
2007-07-03 01:48:06 +02:00
|
|
|
connections_t v;
|
|
|
|
std::copy(v1.begin(), v1.end(), std::back_inserter(v));
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> p =
|
|
|
|
std::make_shared<peer_connection>(manager, t1, 1, false, "no-priority");
|
2007-07-03 01:48:06 +02:00
|
|
|
v.push_back(p);
|
2009-04-26 02:21:59 +02:00
|
|
|
run_test(v, manager);
|
2007-07-03 01:48:06 +02:00
|
|
|
|
|
|
|
float sum = 0.f;
|
|
|
|
for (connections_t::iterator i = v1.begin()
|
|
|
|
, end(v1.end()); i != end; ++i)
|
|
|
|
{
|
2009-04-26 02:21:59 +02:00
|
|
|
sum += (*i)->m_quota;
|
2007-07-03 01:48:06 +02:00
|
|
|
}
|
2007-08-27 01:40:58 +02:00
|
|
|
sum /= sample_time;
|
2007-07-03 01:48:06 +02:00
|
|
|
std::cerr << sum << " target: " << limit << std::endl;
|
2007-08-27 12:20:23 +02:00
|
|
|
TEST_CHECK(sum > 0);
|
2016-04-20 06:45:32 +02:00
|
|
|
TEST_CHECK(close_to(sum, float(limit), 50));
|
2007-07-03 01:48:06 +02:00
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
std::cerr << "non-prioritized rate: " << p->m_quota / sample_time
|
|
|
|
<< " target: " << (limit / 200 / 10) << std::endl;
|
2016-04-20 06:45:32 +02:00
|
|
|
TEST_CHECK(close_to(p->m_quota / sample_time, float(limit) / 200 / 10, 5));
|
2007-07-03 01:48:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_no_starvation(int limit)
|
|
|
|
{
|
2007-12-31 10:41:50 +01:00
|
|
|
std::cerr << "\ntest no starvation " << limit << std::endl;
|
2009-09-16 06:41:35 +02:00
|
|
|
bandwidth_manager manager(0);
|
2009-04-26 02:21:59 +02:00
|
|
|
bandwidth_channel t1;
|
|
|
|
bandwidth_channel t2;
|
2007-07-03 01:48:06 +02:00
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
global_bwc.throttle(limit);
|
2007-07-03 01:48:06 +02:00
|
|
|
|
|
|
|
const int num_peers = 20;
|
|
|
|
|
|
|
|
connections_t v1;
|
2009-04-26 02:21:59 +02:00
|
|
|
spawn_connections(v1, manager, t1, num_peers, "p");
|
2007-07-03 01:48:06 +02:00
|
|
|
connections_t v;
|
|
|
|
std::copy(v1.begin(), v1.end(), std::back_inserter(v));
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> p =
|
|
|
|
std::make_shared<peer_connection>(manager, t2, 1, false, "no-priority");
|
2007-07-03 01:48:06 +02:00
|
|
|
v.push_back(p);
|
2009-04-26 02:21:59 +02:00
|
|
|
run_test(v, manager);
|
2007-07-03 01:48:06 +02:00
|
|
|
|
|
|
|
float sum = 0.f;
|
|
|
|
for (connections_t::iterator i = v.begin()
|
|
|
|
, end(v.end()); i != end; ++i)
|
|
|
|
{
|
2009-04-26 02:21:59 +02:00
|
|
|
sum += (*i)->m_quota;
|
2007-07-03 01:48:06 +02:00
|
|
|
}
|
2007-08-27 01:40:58 +02:00
|
|
|
sum /= sample_time;
|
2007-07-03 01:48:06 +02:00
|
|
|
std::cerr << sum << " target: " << limit << std::endl;
|
2007-08-27 12:20:23 +02:00
|
|
|
TEST_CHECK(sum > 0);
|
2016-04-20 06:45:32 +02:00
|
|
|
TEST_CHECK(close_to(sum, float(limit), 50));
|
2007-07-03 01:48:06 +02:00
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
std::cerr << "non-prioritized rate: " << p->m_quota / sample_time
|
|
|
|
<< " target: " << (limit / 200 / num_peers) << std::endl;
|
2016-04-20 06:45:32 +02:00
|
|
|
TEST_CHECK(close_to(p->m_quota / sample_time, float(limit) / 200 / num_peers, 5));
|
2007-07-03 01:48:06 +02:00
|
|
|
}
|
|
|
|
|
2015-05-31 14:21:16 +02:00
|
|
|
TORRENT_TEST(equal_connection)
|
2007-07-03 01:48:06 +02:00
|
|
|
{
|
2007-08-27 01:40:58 +02:00
|
|
|
test_equal_connections(2, 20);
|
|
|
|
test_equal_connections(2, 2000);
|
2007-07-03 01:48:06 +02:00
|
|
|
test_equal_connections(2, 20000);
|
|
|
|
test_equal_connections(3, 20000);
|
|
|
|
test_equal_connections(5, 20000);
|
|
|
|
test_equal_connections(7, 20000);
|
|
|
|
test_equal_connections(33, 60000);
|
|
|
|
test_equal_connections(33, 500000);
|
2014-01-13 07:49:47 +01:00
|
|
|
test_equal_connections(1, 100000000);
|
2015-05-31 14:21:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(conn_var_rate)
|
|
|
|
{
|
2007-12-31 10:41:50 +01:00
|
|
|
test_connections_variable_rate(2, 20, 0);
|
|
|
|
test_connections_variable_rate(5, 20000, 0);
|
|
|
|
test_connections_variable_rate(3, 2000, 6000);
|
|
|
|
test_connections_variable_rate(5, 2000, 30000);
|
|
|
|
test_connections_variable_rate(33, 500000, 0);
|
2015-05-31 14:21:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(torrents)
|
|
|
|
{
|
2007-08-27 01:40:58 +02:00
|
|
|
test_torrents(2, 400, 400, 0);
|
|
|
|
test_torrents(2, 100, 500, 0);
|
|
|
|
test_torrents(2, 3000, 3000, 6000);
|
|
|
|
test_torrents(1, 40000, 40000, 0);
|
|
|
|
test_torrents(24, 50000, 50000, 0);
|
2007-12-31 10:41:50 +01:00
|
|
|
test_torrents(5, 6000, 6000, 3000);
|
|
|
|
test_torrents(5, 6000, 5000, 4000);
|
|
|
|
test_torrents(5, 20000, 20000, 30000);
|
2015-05-31 14:21:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(torrent_var_rate)
|
|
|
|
{
|
2007-12-31 10:41:50 +01:00
|
|
|
test_torrents_variable_rate(5, 6000, 3000);
|
|
|
|
test_torrents_variable_rate(5, 20000, 30000);
|
2015-05-31 14:21:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(bandwidth_limiter)
|
|
|
|
{
|
2007-07-03 01:48:06 +02:00
|
|
|
test_single_peer(40000, true);
|
|
|
|
test_single_peer(40000, false);
|
2015-05-31 14:21:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(peer_priority)
|
|
|
|
{
|
2007-07-03 01:48:06 +02:00
|
|
|
test_peer_priority(40000, false);
|
|
|
|
test_peer_priority(40000, true);
|
2015-05-31 14:21:16 +02:00
|
|
|
}
|
2007-07-03 01:48:06 +02:00
|
|
|
|
2015-05-31 14:21:16 +02:00
|
|
|
TORRENT_TEST(no_starvation)
|
|
|
|
{
|
|
|
|
test_no_starvation(40000);
|
2007-07-03 01:48:06 +02:00
|
|
|
}
|