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

Add time_now() func, use it in sbws/

GH: see #67
parent d4f8b3db
from sbws.globals import (fail_hard, is_initted)
from sbws.globals import (fail_hard, is_initted, time_now)
from argparse import ArgumentDefaultsHelpFormatter
from datetime import date
from datetime import timedelta
......@@ -40,7 +40,7 @@ def _get_older_files_than(dname, num_days_ago, extensions):
all_fnames.add(fname)
# Figure out what files are too new
new_fnames = set()
today = date.fromtimestamp(time.time())
today = date.fromtimestamp(time_now())
oldest_day = today - timedelta(days=num_days_ago)
working_day = oldest_day
while working_day < today:
......
......@@ -9,7 +9,7 @@ from ..lib.relaylist import RelayList
from ..lib.relayprioritizer import RelayPrioritizer
from ..lib.helperrelay import HelperRelayList
from ..util.simpleauth import authenticate_to_server
from sbws.globals import (fail_hard, is_initted)
from sbws.globals import (fail_hard, is_initted, time_now)
from sbws.globals import (MIN_REQ_BYTES, MAX_REQ_BYTES, SOCKET_TIMEOUT)
import sbws.util.stem as stem_utils
from stem.control import EventType
......@@ -80,7 +80,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.time()
start_time = time_now()
while yet_to_read > 0:
limit = min(conf.getint('client', 'max_recv_per_read'), yet_to_read)
try:
......@@ -91,7 +91,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.time()
end_time = time_now()
return end_time - start_time
......@@ -101,7 +101,7 @@ def measure_rtt_to_server(sock, conf):
RTTs (in seconds). '''
rtts = []
for _ in range(0, conf.getint('client', 'num_rtts')):
start_time = time.time()
start_time = time_now()
if not tell_server_amount(sock, MIN_REQ_BYTES):
log.info('Unable to ping server on', sock.fileno())
return
......@@ -110,7 +110,7 @@ def measure_rtt_to_server(sock, conf):
except (socket.timeout, ConnectionResetError, BrokenPipeError) as e:
log.info(e)
return
end_time = time.time()
end_time = time_now()
if amount_read == 0:
log.info('No pong from server on', sock.fileno())
return
......
from sbws import version
from sbws.globals import (fail_hard, is_initted)
from sbws.globals import (fail_hard, is_initted, time_now)
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
......@@ -121,7 +121,7 @@ def main(args, conf, log_):
data_lines = scale_lines(args, data_lines)
log_stats(data_lines)
with open(args.output, 'wt') as fd:
fd.write('{}\n'.format(int(time.time())))
fd.write('{}\n'.format(int(time_now())))
fd.write('version={}\n'.format(version))
for line in data_lines:
fd.write('{}\n'.format(str(line)))
from ..util.simpleauth import authenticate_client
from ..util.sockio import read_line
from sbws.globals import (fail_hard, is_initted)
from sbws.globals import (fail_hard, is_initted, time_now)
from sbws.globals import (MIN_REQ_BYTES, MAX_REQ_BYTES, SOCKET_TIMEOUT)
from argparse import ArgumentDefaultsHelpFormatter
from functools import lru_cache
......@@ -62,12 +62,12 @@ def _generate_random_string(length):
another thing that hurts its randomness.
'''
assert length > 0
# start = time.time()
# start = time_now()
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.time()
# stop = time_now()
# _generate_random_string.acc += stop - start
# if stop >= 60 + _generate_random_string.last_log:
# log.notice('Spent', _generate_random_string.acc,
......@@ -82,7 +82,7 @@ _generate_random_string.alphabet = list('abcdefghijklmnopqrstuvwxyz'
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
'0123456789')
# _generate_random_string.acc = 0
# _generate_random_string.last_log = time.time()
# _generate_random_string.last_log = time_now()
def write_to_client(sock, conf, amount):
......
import os
import time
from sbws.lib.pastlylogger import PastlyLogger
......@@ -54,6 +55,16 @@ def fail_hard(*s, log=None):
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 _log_level_string_to_int(s):
if s == 'debug':
return 4
......
from sbws.globals import time_now
import sbws.util.stem as stem_utils
from stem import Flag
import time
......@@ -23,7 +24,7 @@ class RelayList:
@property
def relays(self):
if time.time() >= self._last_refresh + self.REFRESH_INTERVAL:
if time_now() >= self._last_refresh + self.REFRESH_INTERVAL:
self._refresh()
return self._relays
......@@ -85,4 +86,4 @@ class RelayList:
def _refresh(self):
self._relays = self._init_relays()
self._last_refresh = time.time()
self._last_refresh = time_now()
......@@ -3,6 +3,7 @@ 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 time
import copy
......@@ -58,7 +59,7 @@ class RelayPrioritizer:
get around to giving the relay another chance at a getting a successful
measurement.
'''
fn_tstart = Decimal(time.time())
fn_tstart = Decimal(time_now())
if self.measure_authorities:
relays = copy.deepcopy(self.relay_list.relays)
else:
......@@ -70,7 +71,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.time() - self.fresh_seconds
oldest_allowed = time_now() - self.fresh_seconds
for result in results:
assert isinstance(result, Result)
# Ignore results that are too far in the past
......@@ -93,7 +94,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.time())
fn_tstop = Decimal(time_now())
fn_tdelta = (fn_tstop - fn_tstart) * 1000
self.log.info('Spent {0:.3f} msecs calculating relay best priority'.
format(fn_tdelta))
......
from sbws.globals import time_now
import os
import time
import json
......@@ -61,7 +62,7 @@ def trim_results(fresh_days, results, log_fn=print):
assert isinstance(fresh_days, int)
assert isinstance(results, list)
data_period = fresh_days * 24*60*60
oldest_allowed = time.time() - data_period
oldest_allowed = time_now() - data_period
out_results = []
for result in results:
if result.time >= oldest_allowed:
......@@ -78,7 +79,7 @@ def load_recent_results_in_datadir(fresh_days, datadir, success_only=False,
assert isinstance(fresh_days, int)
assert os.path.isdir(datadir)
results = []
today = date.fromtimestamp(time.time())
today = date.fromtimestamp(time_now())
data_period = fresh_days + 2
oldest_day = today - timedelta(days=data_period)
working_day = oldest_day
......@@ -141,7 +142,7 @@ class Result:
self._circ = circ
self._server_host = server_host
self._scanner = client_nick
self._time = time.time() if t is None else t
self._time = time_now() if t is None else t
@property
def type(self):
......
Supports Markdown
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