Commit fc69a02c authored by Matt Traudt's avatar Matt Traudt
Browse files

Finish move away from time_now() to time.time()

GH: closes #67
parent e92645fb
from sbws import version
from sbws.globals import (fail_hard, is_initted, time_now)
from sbws.globals import (fail_hard, is_initted)
from sbws.lib.resultdump import ResultSuccess
from sbws.lib.resultdump import load_recent_results_in_datadir
from sbws.lib.resultdump import group_results_by_relay
from argparse import ArgumentDefaultsHelpFormatter
from statistics import median
import os
import time
import logging
log = logging.getLogger(__name__)
......@@ -123,7 +124,7 @@ def main(args, conf):
log_stats(data_lines)
log.info('Writing v3bw file to %s', args.output)
with open(args.output, 'wt') as fd:
fd.write('{}\n'.format(int(time_now())))
fd.write('{}\n'.format(int(time.time())))
fd.write('version={}\n'.format(version))
for line in data_lines:
fd.write('{}\n'.format(str(line)))
......@@ -10,7 +10,7 @@ from ..lib.relayprioritizer import RelayPrioritizer
from ..lib.helperrelay import HelperRelayList
from ..util.simpleauth import authenticate_to_server
from ..util.sockio import (make_socket, close_socket)
from sbws.globals import (fail_hard, is_initted, time_now)
from sbws.globals import (fail_hard, is_initted)
from sbws.globals import (MIN_REQ_BYTES, MAX_REQ_BYTES)
import sbws.util.stem as stem_utils
from argparse import ArgumentDefaultsHelpFormatter
......@@ -45,7 +45,7 @@ def timed_recv_from_server(sock, conf, yet_to_read):
''' Return the time in seconds it took to read <yet_to_read> bytes from
the server. Return None if error '''
assert yet_to_read > 0
start_time = time_now()
start_time = time.time()
while yet_to_read > 0:
limit = min(conf.getint('scanner', 'max_recv_per_read'), yet_to_read)
try:
......@@ -56,7 +56,7 @@ def timed_recv_from_server(sock, conf, yet_to_read):
if read_this_time == 0:
return
yet_to_read -= read_this_time
end_time = time_now()
end_time = time.time()
return end_time - start_time
......@@ -66,7 +66,7 @@ def measure_rtt_to_server(sock, conf):
RTTs (in seconds). '''
rtts = []
for _ in range(0, conf.getint('scanner', 'num_rtts')):
start_time = time_now()
start_time = time.time()
if not tell_server_amount(sock, MIN_REQ_BYTES):
log.info('Unable to ping server on %d', sock.fileno())
return
......@@ -75,7 +75,7 @@ def measure_rtt_to_server(sock, conf):
except (socket.timeout, ConnectionResetError, BrokenPipeError) as e:
log.info(e)
return
end_time = time_now()
end_time = time.time()
if amount_read == 0:
log.info('No pong from server on %d', sock.fileno())
return
......
......@@ -65,12 +65,12 @@ def _generate_random_string(length):
another thing that hurts its randomness.
'''
assert length > 0
# start = time_now()
# start = time.time()
repeats = int(length / len(_generate_random_string.alphabet)) + 1
rng.shuffle(_generate_random_string.alphabet)
s = ''.join(_generate_random_string.alphabet)
s = s * repeats
# stop = time_now()
# stop = time.time()
# _generate_random_string.acc += stop - start
# if stop >= 60 + _generate_random_string.last_log:
# log.info('Spent', _generate_random_string.acc,
......@@ -85,7 +85,7 @@ _generate_random_string.alphabet = list('abcdefghijklmnopqrstuvwxyz'
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
'0123456789')
# _generate_random_string.acc = 0
# _generate_random_string.last_log = time_now()
# _generate_random_string.last_log = time.time()
def write_to_scanner(sock, conf, amount):
......
......@@ -42,16 +42,6 @@ def fail_hard(*a, **kw):
exit(1)
def time_now():
'''
Return the current time in seconds since 1970. This function exists to
make testing easier
:returns: Unix timestamp as a float
'''
return time.time()
def touch_file(fname, times=None):
'''
If **fname** exists, update its last access and modified times to now. If
......
from ..util.simpleauth import authenticate_to_server
from ..util.sockio import (make_socket, close_socket)
from sbws.globals import time_now
from sbws.lib.circuitbuilder import GapsCircuitBuilder as CB
import sbws.util.stem as stem_utils
from stem.descriptor.router_status_entry import RouterStatusEntryV3
......@@ -86,7 +85,7 @@ class HelperRelayList:
def _should_perform_reachability_test(self):
return self._last_reachability_test + self._reachability_test_every <\
time_now()
time.time()
def _build_circuit_with_relay(self, relay):
assert isinstance(relay, RouterStatusEntryV3)
......@@ -140,7 +139,7 @@ class HelperRelayList:
self._circuit_builder.close_circuit(circ_id)
log.debug('Helper %s is usable. Keeping it.', relay.nickname)
helpers.add(helper)
self._last_reachability_test = time_now()
self._last_reachability_test = time.time()
self._usable_helpers = list(helpers)
log.info('After performing reachability tests, we have %d/%d usable '
'helpers: %s', len(self._usable_helpers),
......
from sbws.globals import time_now
import sbws.util.stem as stem_utils
from stem import Flag
import random
import time
class RelayList:
......@@ -22,7 +22,7 @@ class RelayList:
@property
def relays(self):
if time_now() >= self._last_refresh + self.REFRESH_INTERVAL:
if time.time() >= self._last_refresh + self.REFRESH_INTERVAL:
self._refresh()
return self._relays
......@@ -84,4 +84,4 @@ class RelayList:
def _refresh(self):
self._relays = self._init_relays()
self._last_refresh = time_now()
self._last_refresh = time.time()
......@@ -3,8 +3,8 @@ from ..lib.resultdump import ResultDump
from ..lib.resultdump import Result
from ..lib.resultdump import ResultError
from ..lib.relaylist import RelayList
from sbws.globals import time_now
import copy
import time
import logging
log = logging.getLogger(__name__)
......@@ -56,7 +56,7 @@ class RelayPrioritizer:
get around to giving the relay another chance at a getting a successful
measurement.
'''
fn_tstart = Decimal(time_now())
fn_tstart = Decimal(time.time())
relays = copy.deepcopy(self.relay_list.relays)
if not self.measure_authorities:
relays = [r for r in relays
......@@ -66,7 +66,7 @@ class RelayPrioritizer:
results = rd.results_for_relay(relay)
priority = 0
# The time before which we do not consider results valid anymore
oldest_allowed = time_now() - self.fresh_seconds
oldest_allowed = time.time() - self.fresh_seconds
for result in results:
assert isinstance(result, Result)
# Ignore results that are too far in the past
......@@ -92,7 +92,7 @@ class RelayPrioritizer:
# relays at the front
relays = sorted(relays, key=lambda r: r.priority)
cutoff = max(int(len(relays) * PERCENT_TO_RETURN), MIN_TO_RETURN)
fn_tstop = Decimal(time_now())
fn_tstop = Decimal(time.time())
fn_tdelta = (fn_tstop - fn_tstart) * 1000
log.info('Spent %f msecs calculating relay best priority', fn_tdelta)
# Finally, slowly return the relays to the caller (after removing the
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment