udp load test: minor performance improvements

This commit is contained in:
Joakim Frostegård 2022-02-20 12:07:28 +01:00
parent e9ced08547
commit 0f60ffbb75

View file

@ -114,21 +114,21 @@ fn create_random_request(
transaction_id: TransactionId, transaction_id: TransactionId,
torrent_peer: &TorrentPeer, torrent_peer: &TorrentPeer,
) -> Request { ) -> Request {
let weights = vec![ const ITEMS: [RequestType; 3] = [
config.requests.weight_announce as u32,
config.requests.weight_connect as u32,
config.requests.weight_scrape as u32,
];
let items = vec![
RequestType::Announce, RequestType::Announce,
RequestType::Connect, RequestType::Connect,
RequestType::Scrape, RequestType::Scrape,
]; ];
let dist = WeightedIndex::new(&weights).expect("random request weighted index"); let weights = [
config.requests.weight_announce as u32,
config.requests.weight_connect as u32,
config.requests.weight_scrape as u32,
];
match items[dist.sample(rng)] { let dist = WeightedIndex::new(weights).expect("random request weighted index");
match ITEMS[dist.sample(rng)] {
RequestType::Announce => create_announce_request(config, rng, torrent_peer, transaction_id), RequestType::Announce => create_announce_request(config, rng, torrent_peer, transaction_id),
RequestType::Connect => create_connect_request(transaction_id), RequestType::Connect => create_connect_request(transaction_id),
RequestType::Scrape => create_scrape_request(&info_hashes, torrent_peer, transaction_id), RequestType::Scrape => create_scrape_request(&info_hashes, torrent_peer, transaction_id),
@ -209,7 +209,7 @@ fn create_torrent_peer(
scrape_hash_indeces, scrape_hash_indeces,
connection_id, connection_id,
peer_id: generate_peer_id(), peer_id: generate_peer_id(),
port: Port(rand::random()), port: Port(rng.gen()),
} }
} }