Convert check_mhz into sourcestats, using --exec-send, doing multi-source statistics

This commit is contained in:
Ian Gulliver
2016-03-11 14:37:47 -08:00
parent 2ad7ad7a8f
commit c389f3ec24
4 changed files with 91 additions and 53 deletions

View File

@@ -1,9 +0,0 @@
import json
import socket
def GetEvents(host, port):
with socket.create_connection((host, port)) as sock:
fh = sock.makefile()
for line in fh:
yield json.loads(line)

View File

@@ -1,44 +0,0 @@
#!/usr/bin/python3
import sys
import time
import adsblib
if len(sys.argv) != 3:
print('Usage: %s <host> <port>' % sys.argv[0])
print('Fetching samples...')
advertised_mhz = None
start_time = None
start_timestamp = None
last_timestamp = None
for i, event in enumerate(adsblib.GetEvents(sys.argv[1], sys.argv[2])):
advertised_mhz = event.get('mlat_timestamp_mhz', advertised_mhz)
if 'mlat_timestamp' not in event:
continue
if not start_time:
start_time = time.time()
start_timestamp = event['mlat_timestamp']
if last_timestamp:
if event['mlat_timestamp'] < last_timestamp:
print('\rERROR: timestamp went backwards (%d -> %d)\x1b[K' %
(last_timestamp, event['mlat_timestamp']))
last_timestamp = event['mlat_timestamp']
if i % 1000 == 0 and start_time and advertised_mhz:
time_diff = time.time() - start_time
value_diff = event['mlat_timestamp'] - start_timestamp
measured_mhz = value_diff / time_diff / 1000000
print(
'\r%d samples, %d seconds, advertised: %d MHz, measured: %d MHz\x1b[K' %
(i, time_diff, advertised_mhz, measured_mhz),
end='', flush=True)
print()

View File

@@ -0,0 +1,91 @@
#!/usr/bin/python3 -u
import collections
import fileinput
import json
import sys
import threading
import time
def Log(s):
print('{sourcestats} %s' % s, file=sys.stderr, flush=True)
class Source(object):
_first_event = None
def __init__(self):
self._count_by_type = collections.defaultdict(int)
def HandleEvent(self, event):
now = time.clock_gettime(time.CLOCK_MONOTONIC)
if not self._first_event:
self._first_event = event
self._first_event_timestamp = now
self._last_event = event
self._last_event_timestamp = now
self._count_by_type[event['type']] += 1
def _GetDuration(self):
return self._last_event_timestamp - self._first_event_timestamp
def GetTypeRate(self, t):
return self._count_by_type[t] / self._GetDuration()
def GetHz(self):
ticks = self._last_event['mlat_timestamp'] - self._first_event['mlat_timestamp']
return ticks / self._GetDuration()
class Reader(object):
def __init__(self):
self._sources = collections.defaultdict(Source)
self._input = fileinput.input()
self._LogStats()
def _GetEvents(self):
for line in self._input:
yield json.loads(line)
def _HandleHeader(self, event):
self._config = event
def _LogStats(self):
data = [
('Source ID', 'Mode-AC', 'Mode-S short', 'Mode-S long', 'Clock rate'),
]
for source_id, source in self._sources.items():
data.append((
source_id,
'%.02f/s' % source.GetTypeRate('Mode-AC'),
'%.02f/s' % source.GetTypeRate('Mode-S short'),
'%.02f/s' % source.GetTypeRate('Mode-S long'),
'%.04f' % (source.GetHz() / (self._config['mlat_timestamp_mhz'] * 1000000)),
))
col_width = [max(len(row[i]) for row in data) + 2 for i in range(len(data[0]))]
for row in data:
Log(''.join(word.ljust(col_width[i]) for i, word in enumerate(row)))
Log('')
self._timer = threading.Timer(60.0, self._LogStats)
self._timer.start()
def Read(self):
for event in self._GetEvents():
if event['type'] == 'header':
self._HandleHeader(event)
continue
source = self._sources[event['source_id']]
source.HandleEvent(event)
Log('Runtime data:')
Log('\tpython_version: %s' % sys.version.replace('\n', ''))
Log('')
reader = Reader()
reader.Read()