def test_load_metrics_from_kv_store_can_load_all_values(storage):
    events = generate_events(10)
    step = timedelta(seconds=5)
    ts = MockTimestamp()
    metrics = KvStoreMetricsCollector(storage, ts)
    expected_stats = MetricsStats(step)

    for ev in events:
        ts.value = ev.timestamp
        metrics.store_event(ev.name, ev.value)
        expected_stats.add(ev.timestamp, ev.name, ev.value)

    stats = load_metrics_from_kv_store(storage, step=step)
    assert stats == expected_stats
def test_kv_store_metrics_collector_stores_properly_encoded_data(storage: KeyValueStorage, value):
    ts = MockTimestamp(gen_next_timestamp())
    metrics = KvStoreMetricsCollector(storage, ts)
    assert len([(k, v) for k, v in storage.iterator()]) == 0

    id = gen_metrics_name()
    event = MetricsEvent(ts.value, id, value)
    encoded_key, encoded_value = KvStoreMetricsFormat.encode(event)

    metrics.add_event(id, value)
    stored_events = [(k, v) for k, v in storage.iterator()]

    assert len(stored_events) == 1
    assert stored_events[0][0] == encoded_key
    assert stored_events[0][1] == encoded_value
def test_kv_store_metrics_collector_store_all_events_with_same_timestamp(storage: KeyValueStorage):
    ts = MockTimestamp()
    metrics = KvStoreMetricsCollector(storage, ts)
    values = [10, 2, 54, 2]

    for v in values:
        metrics.add_event(MetricsName.BACKUP_THREE_PC_BATCH_SIZE, v)
    events = [KvStoreMetricsFormat.decode(k, v) for k, v in storage.iterator()]

    # Check that all events are stored
    assert len(events) == len(values)
    # Check that all events are stored in correct order
    assert sorted(events, key=lambda ev: ev.timestamp) == events
    # Check that all events stored were in source events
    for ev in events:
        assert ev.value in values
Example #4
0
def test_kv_store_metrics_collector_can_store_data(storage: KeyValueStorage):
    ts = MockTimestamp(datetime(2018, month=7, day=24, hour=18, minute=12, second=35, microsecond=456000))
    metrics = KvStoreMetricsCollector(storage, ts)
    assert len([(k, v) for k, v in storage.iterator()]) == 0

    id = MetricType.LOOPER_RUN_TIME_SPENT
    value = 6.2
    metrics.add_event(id, value)
    result = [(k, v) for k, v in storage.iterator()]
    assert len(result) == 1

    k, v = result[0]
    decoded_id, decoded_ts = decode_key(k)
    assert decoded_id == id
    assert decoded_ts == ts.value
    assert decode_value(v) == value
def test_load_metrics_from_kv_store_can_filter_values(storage):
    events = generate_events(10)
    step = timedelta(seconds=3)
    ts = MockTimestamp()
    metrics = KvStoreMetricsCollector(storage, ts)
    expected_stats = MetricsStats(step)

    timestamps = sorted(ev.timestamp for ev in events)
    min_ts = timestamps[len(events) // 3]
    max_ts = timestamps[2 * len(events) // 3]

    for ev in events:
        ts.value = ev.timestamp
        metrics.store_event(ev.name, ev.value)
        if min_ts <= ev.timestamp <= max_ts:
            expected_stats.add(ev.timestamp, ev.name, ev.value)

    stats = load_metrics_from_kv_store(storage, min_ts, max_ts, step)
    assert stats == expected_stats
def test_kv_store_metrics_collector_store_all_data_in_order(
        storage: KeyValueStorage):
    ts = MockTimestamp()
    metrics = KvStoreMetricsCollector(storage, ts)
    events = generate_events(10)

    for e in events:
        ts.value = e.timestamp
        metrics.add_event(e.name, e.value)
    stored_events = [
        KvStoreMetricsFormat.decode(k, v) for k, v in storage.iterator()
    ]

    # Check that all events are stored
    assert len(stored_events) == len(events)
    # Check that all events are stored in correct order
    assert sorted(stored_events, key=lambda v: v.timestamp) == stored_events
    # Check that all events stored were in source events
    for ev in stored_events:
        assert ev in events
Example #7
0
def test_kv_store_metrics_collector_store_all_data_in_order(storage: KeyValueStorage):
    ts = MockTimestamp()
    metrics = KvStoreMetricsCollector(storage, ts)

    events = [
        (MetricType.CLIENT_STACK_MESSAGES_PROCESSED, 30),
        (MetricType.LOOPER_RUN_TIME_SPENT, 1.2),
        (MetricType.CLIENT_STACK_MESSAGES_PROCESSED, 163),
        (MetricType.LOOPER_RUN_TIME_SPENT, 5.2),
        (MetricType.CLIENT_STACK_MESSAGES_PROCESSED, 6),
        (MetricType.LOOPER_RUN_TIME_SPENT, 0.3)
    ]
    for id, value in events:
        metrics.add_event(id, value)
        ts.value += timedelta(seconds=0.1)
    result = [(*decode_key(k), decode_value(v)) for k, v in storage.iterator()]

    # Check that all events are stored
    assert len(result) == 6
    # Check that all events are stored in correct order
    assert sorted(result, key=lambda v: (v[0], v[1])) == result
    # Check that all events stored were in source events
    for v in result:
        assert (v[0], v[2]) in events