diff --git a/poc/py/state_to_buttons.py b/poc/py/state_to_buttons.py index 80fb887..486541e 100644 --- a/poc/py/state_to_buttons.py +++ b/poc/py/state_to_buttons.py @@ -16,7 +16,7 @@ def main(): if not key in buckets: buckets[key] = False buckets[key] = False if line[0] == "/" else float(line.split()[-1]) - print(sorted([(int(i),buckets[i]) for i in buckets if buckets[i]])) + print("[", " ".join(sorted([f"{int(i)}={buckets[i]}" for i in buckets if buckets[i]])), "]") def get_args(): ap = argparse.ArgumentParser() diff --git a/poc/py/stream_to_state.py b/poc/py/stream_to_state.py index b525520..9dce33c 100644 --- a/poc/py/stream_to_state.py +++ b/poc/py/stream_to_state.py @@ -7,6 +7,7 @@ def main(): args = get_args() with_( N=args.n, + M=args.m, R=args.r, T=args.t, p=args.p, @@ -15,12 +16,13 @@ def main(): def get_args(): ap = argparse.ArgumentParser() ap.add_argument("-n", type=int, help="queue capacity", default=3) + ap.add_argument("-m", type=int, help="queue fill line", default=3) ap.add_argument("-r", type=int, help="drain rate per second", default=2) ap.add_argument("-t", type=int, help="threshold for state", default=2) ap.add_argument("-p", type=str, help="path to write out to", default="/tmp/cbappend.both.txt") return ap.parse_args() -def with_(N, R, T, p): +def with_(N, M, R, T, p): triggered = CBAppend(p) released = CBAppend(p) cb = CBFork(triggered, released) @@ -33,7 +35,7 @@ def with_(N, R, T, p): [buckets[i].pop() for i in buckets] else: if not got in buckets: - buckets[got] = Bucket(N, R, T, cb.cb(got)) + buckets[got] = Bucket(N, M, R, T, cb.cb(got)) buckets[got].push() def readline(): @@ -57,9 +59,10 @@ class State(): self.f = f class Bucket(): - def __init__(self, N, R, T, CB): + def __init__(self, N, M, R, T, CB): self.q = 0.0 self.N = N + self.M = M self.R = R self.T = T self.CB = CB @@ -81,7 +84,10 @@ class Bucket(): if new_state == self.__last_state: return self.__last_state = new_state - self.CB(State(new_state, int(100*self.q/self.N)/100.0)) + filledness = int(100*(self.q/self.M))/100.0 + if filledness > 1.0: + filledness = 1.0 + self.CB(State(new_state, filledness)) def state(self): return self.__last_state diff --git a/poc/py/test.sh b/poc/py/test.sh index 8b9b958..b296fd2 100644 --- a/poc/py/test.sh +++ b/poc/py/test.sh @@ -3,18 +3,29 @@ set -e set -o pipefail +peek() { + while read -r line; do + echo $line + echo $line >&2 + done +} cleanup() { kill -9 $(jobs -p) || true } trap cleanup EXIT python3 ./state_to_buttons.py & -python3 testdata/rand_0_13_stream.py \ -| ( - while read -r line; do - #echo $line >&2 - echo $line - done -) \ -| python3 ./stream_to_state.py +python3 ./testdata/rand_0_n_stream.py \ + -n 6 \ + -b-min 1 \ + -b-max 10 \ + -d-min 100 \ + -d-max 2000 \ + -between 100 \ +| peek \ +| python3 ./stream_to_state.py \ + -n 20 \ + -m 15 \ + -r 15 \ + -t 14