Commit 60fb6ee2 authored by juga's avatar juga
Browse files

chg: Add subcommand to show exits with 2 in FlowCtrl

Closes #40132
parent 037dd9c3
Loading
Loading
Loading
Loading

sbws/core/flowctrl2.py

0 → 100644
+166 −0
Original line number Diff line number Diff line
import logging
from argparse import ArgumentDefaultsHelpFormatter

import sbws.util.stem as stem_utils
from sbws.lib.relaylist import RelayList
from sbws.util.state import State

log = logging.getLogger(__name__)


def gen_parser(sub):
    d = "Log the number of exits that have 2 in FlowCtrl."
    p = sub.add_parser(
        "flowctrl2",
        description=d,
        formatter_class=ArgumentDefaultsHelpFormatter,
    )
    return p


def main(args, conf):
    controller = stem_utils.launch_or_connect_to_tor(conf)

    # When there will be a refactor where conf is global, this can be removed
    # from here.
    state = State(conf.getpath("paths", "state_fname"))

    measurements_period = conf.getint("general", "data_period")
    rl = RelayList(args, conf, controller, measurements_period, state)
    exits = rl.exits_not_bad_allowing_port(443)
    log.info("Number of exits: %s", len(exits))
    exits_min_bw = rl.exit_min_bw()
    log.info("Exits minimum bandwidth: %s KB.", exits_min_bw / 1000)
    exits_with_min_bw = stem_utils.only_relays_with_bandwidth(
        controller, exits, min_bw=exits_min_bw
    )
    log.info(
        "Number of exits with minimum bandwidth: %s", len(exits_with_min_bw)
    )
    exits_sorted = sorted(
        exits, key=lambda r: r.consensus_bandwidth, reverse=True
    )
    log.info(
        "Exits lowest bandwidth: %s KB.",
        exits_sorted[-1].consensus_bandwidth / 1000,
    )
    log.info(
        "Exits highest bandwidth: %s KB.",
        exits_sorted[0].consensus_bandwidth / 1000,
    )

    non_exits = rl.non_exits
    log.info("Number of non exits: %s.", len(non_exits))

    non_exits_with_helpers_double_bw = 0
    non_exits_with_helpers_same_bw = 0
    non_exits_without_helpers_same_double_bw = 0
    for relay in non_exits:
        double_min_bw = max(exits_min_bw, relay.consensus_bandwidth * 2)
        helpers = stem_utils.only_relays_with_bandwidth(
            controller, exits_with_min_bw, min_bw=double_min_bw
        )
        if helpers:
            log.debug(
                "Number of helpers with double bandwidth for relay %s: %s.",
                relay.nickname,
                len(helpers),
            )
            non_exits_with_helpers_double_bw += 1
        else:
            min_bw = max(exits_min_bw, relay.consensus_bandwidth)
            helpers = stem_utils.only_relays_with_bandwidth(
                controller, exits_with_min_bw, min_bw=min_bw
            )
            if helpers:
                log.debug(
                    "Number of helpers for relay %s: %s.",
                    relay.nickname,
                    len(helpers),
                )
                non_exits_with_helpers_same_bw += 1
            else:
                log.debug("No helpers for relay %s", relay.nickname)
                non_exits_without_helpers_same_double_bw += 1
    log.info(
        "Number of non exits with helpers that have double bandwidth: %s.",
        non_exits_with_helpers_double_bw,
    )
    log.info(
        "Number of non exits with helpers that have same bandwidth: %s.",
        non_exits_with_helpers_same_bw,
    )
    log.info(
        "Number of non exits without helpers that have  double or same"
        " bandwidth: %s.",
        non_exits_without_helpers_same_double_bw,
    )

    exits_flowctrl2 = rl.exits_with_2_in_flowctrl(443)
    log.info(
        "Number of exits that have 2 in FlowCtrl: %s.", len(exits_flowctrl2)
    )
    exits_flowctrl2_min_bw = stem_utils.only_relays_with_bandwidth(
        controller, exits_flowctrl2, min_bw=exits_min_bw
    )
    log.info(
        "Number of exits that have 2 in FlowCtrl and minimum bandwidth: %s",
        len(exits_flowctrl2_min_bw),
    )
    exits_flowctrl2_sorted = sorted(
        exits_flowctrl2, key=lambda r: r.consensus_bandwidth, reverse=True
    )
    log.info(
        "Exits that have 2 in FlowCtrl lowest bandwidth: %s KB.",
        exits_flowctrl2_sorted[-1].consensus_bandwidth / 1000,
    )
    log.info(
        "Exits that have 2 in FlowCtrl highest bandwidth: %s KB.",
        exits_flowctrl2_sorted[0].consensus_bandwidth / 1000,
    )

    non_exits_with_helpers_flowctrl2_double_bw = 0
    non_exits_with_helpers_flowctrl2_same_bw = 0
    non_exits_without_helpers_flowctrl2_same_double_bw = 0
    for relay in non_exits:
        double_min_bw = max(exits_min_bw, relay.consensus_bandwidth * 2)
        helpers = stem_utils.only_relays_with_bandwidth(
            controller, exits_flowctrl2_min_bw, min_bw=double_min_bw
        )
        if helpers:
            log.debug(
                "Number of helpers with double bandwidth for relay %s: %s.",
                relay.nickname,
                len(helpers),
            )
            non_exits_with_helpers_flowctrl2_double_bw += 1
        else:
            min_bw = max(exits_min_bw, relay.consensus_bandwidth)
            helpers = stem_utils.only_relays_with_bandwidth(
                controller, exits_flowctrl2_min_bw, min_bw=min_bw
            )
            if helpers:
                log.debug(
                    "Number of helpers for relay %s: %s.",
                    relay.nickname,
                    len(helpers),
                )
                non_exits_with_helpers_flowctrl2_same_bw += 1
            else:
                log.debug("No helpers for relay %s", relay.nickname)
                non_exits_without_helpers_flowctrl2_same_double_bw += 1
    log.info(
        "Number of non exits with helpers that have 2 in FlowCtrl and double"
        " bandwidth: %s.",
        non_exits_with_helpers_flowctrl2_double_bw,
    )
    log.info(
        "Number of non exits with helpers that have 2 in FlowCtrl and same"
        " bandwidth: %s.",
        non_exits_with_helpers_flowctrl2_same_bw,
    )
    log.info(
        "Number of non exits without helpers that have 2 in FlowCtrl and"
        " double or same bandwidth: %s.",
        non_exits_without_helpers_flowctrl2_same_double_bw,
    )
+6 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ from requests.__version__ import __version__ as requests_version
from stem import __version__ as stem_version

import sbws.core.cleanup
import sbws.core.flowctrl2
import sbws.core.generate
import sbws.core.scanner
import sbws.core.stats
@@ -77,6 +78,11 @@ def main():
            "kw": def_kwargs,
        },
        "stats": {"f": sbws.core.stats.main, "a": def_args, "kw": def_kwargs},
        "flowctrl2": {
            "f": sbws.core.flowctrl2.main,
            "a": def_args,
            "kw": def_kwargs,
        },
    }
    try:
        if args.command not in known_commands:
+2 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ import os
from argparse import ArgumentParser, RawTextHelpFormatter

import sbws.core.cleanup
import sbws.core.flowctrl2
import sbws.core.generate
import sbws.core.scanner
import sbws.core.stats
@@ -32,4 +33,5 @@ def create_parser():
    sbws.core.scanner.gen_parser(sub)
    sbws.core.generate.gen_parser(sub)
    sbws.core.stats.gen_parser(sub)
    sbws.core.flowctrl2.gen_parser(sub)
    return p