File: test_optimistic_unchoke.cpp

package info (click to toggle)
libtorrent-rasterbar 2.0.11-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,304 kB
  • sloc: cpp: 190,670; python: 7,142; makefile: 1,374; ansic: 574; sh: 317; xml: 104
file content (173 lines) | stat: -rw-r--r-- 5,390 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*

Copyright (c) 2016, 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.

*/

#include "setup_swarm.hpp"
#include "test.hpp"
#include "create_torrent.hpp"
#include "bittorrent_peer.hpp"
#include "settings.hpp"
#include "utils.hpp"
#include "simulator/utils.hpp"
#include "setup_transfer.hpp" // for addr()

#include "libtorrent/alert.hpp"
#include "libtorrent/alert_types.hpp"
#include "libtorrent/session.hpp"
#include "libtorrent/session_stats.hpp"
#include "libtorrent/io_context.hpp"
#include "libtorrent/torrent_info.hpp"
#include "libtorrent/deadline_timer.hpp"

#include <memory>

using namespace lt;

struct choke_state
{
	choke_state() : unchoke_duration(lt::seconds(0)), choked(true) {}
	lt::time_duration unchoke_duration;
	lt::time_point last_unchoke;
	bool choked;
};

TORRENT_TEST(optimistic_unchoke)
{
	int const num_nodes = 20;
	lt::time_duration const test_duration
		= lt::seconds(num_nodes * 90);

	dsl_config network_cfg;
	sim::simulation sim{network_cfg};

	io_context ios(sim, addr("50.1.0.0"));
	lt::time_point start_time(lt::clock_type::now());

	lt::add_torrent_params atp = ::create_torrent(0);
	atp.flags &= ~torrent_flags::auto_managed;
	atp.flags &= ~torrent_flags::paused;

	lt::settings_pack pack = settings();
	// only allow an optimistic unchoke slot
	pack.set_int(settings_pack::unchoke_slots_limit, 1);
	pack.set_int(settings_pack::num_optimistic_unchoke_slots, 1);
	pack.set_int(settings_pack::peer_timeout, 9999);

	std::vector<choke_state> peer_choke_state(num_nodes);

	session_proxy proxy;

	auto ses = std::make_shared<lt::session>(pack, ios);
	ses->async_add_torrent(atp);

	std::vector<std::shared_ptr<sim::asio::io_context>> io_context;
	std::vector<std::shared_ptr<peer_conn>> peers;

	print_alerts(*ses);

	sim::timer t(sim, lt::seconds(0), [&](boost::system::error_code const&)
	{
		for (int i = 0; i < num_nodes; ++i)
		{
			// create a new io_context
			char ep[30];
			std::snprintf(ep, sizeof(ep), "50.0.%d.%d", (i + 1) >> 8, (i + 1) & 0xff);
			io_context.push_back(std::make_shared<sim::asio::io_context>(
				sim, addr(ep)));
			peers.push_back(std::make_shared<peer_conn>(*io_context.back()
				, [&,i](int msg, char const* /* buf */, int /* len */)
				{
					choke_state& cs = peer_choke_state[i];
					if (msg == 0)
					{
						// choke
						if (!cs.choked)
						{
							cs.choked = true;
							cs.unchoke_duration += lt::clock_type::now() - cs.last_unchoke;
						}
					}
					else if (msg == 1)
					{
						// unchoke
						if (cs.choked)
						{
							cs.choked = false;
							cs.last_unchoke = lt::clock_type::now();
						}
					}
					else
					{
						return;
					}

					char const* msg_str[] = {"choke", "unchoke"};

					lt::time_duration d = lt::clock_type::now() - start_time;
					std::uint32_t const millis = std::uint32_t(
						lt::duration_cast<lt::milliseconds>(d).count());
					printf("\x1b[35m%4u.%03u: [%d] %s (%d ms)\x1b[0m\n"
						, millis / 1000, millis % 1000, i, msg_str[msg]
						, int(lt::duration_cast<lt::milliseconds>(cs.unchoke_duration).count()));
				}
				, *atp.ti
				, tcp::endpoint(addr("50.1.0.0"), 6881)
				, peer_conn::peer_mode_t::idle));
		}
	});

	sim::timer t2(sim, test_duration, [&](boost::system::error_code const&)
	{
		for (auto& p : peers)
		{
			p->abort();
		}
		proxy = ses->abort();
		ses.reset();
	});

	sim.run();

	std::int64_t const duration_ms = lt::duration_cast<lt::milliseconds>(test_duration).count();
	std::int64_t const average_unchoke_time = duration_ms / num_nodes;
	printf("EXPECT: %" PRId64 " ms\n", average_unchoke_time);
	for (auto& cs : peer_choke_state)
	{
		if (!cs.choked)
		{
			cs.choked = true;
			cs.unchoke_duration += lt::clock_type::now() - cs.last_unchoke;
		}
		std::int64_t const unchoke_duration = lt::duration_cast<lt::milliseconds>(cs.unchoke_duration).count();
		printf("%" PRId64 " ms\n", unchoke_duration);
		TEST_CHECK(std::abs(unchoke_duration - average_unchoke_time) < 1500);
	}
}