From 0e476567de7bd6bf55c4dadd28a7f234b6008b42 Mon Sep 17 00:00:00 2001 From: Mehdi Abaakouk Date: Sat, 18 Jul 2015 18:46:32 +0200 Subject: [PATCH] Improves send_test_data tools Improves send_test_data.py tools so they can optionally send samples as notifications, not just rpc. Change-Id: Ib3cd809158029b0dbeca0b988e8d998bb2a313a7 --- tools/make_test_data.py | 6 +++++- tools/send_test_data.py | 43 ++++++++++++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/tools/make_test_data.py b/tools/make_test_data.py index f7f23e08ba..de52f4d30a 100755 --- a/tools/make_test_data.py +++ b/tools/make_test_data.py @@ -42,7 +42,11 @@ from ceilometer import storage def make_test_data(name, meter_type, unit, volume, random_min, random_max, user_id, project_id, resource_id, start, end, interval, resource_metadata=None, source='artificial'): - resource_metadata = resource_metadata or {} + resource_metadata = resource_metadata or {'display_name': 'toto', + 'host': 'tata', + 'image_ref_url': 'test', + 'instance_flavor_id': 'toto', + } # Compute start and end timestamps for the new data. if isinstance(start, datetime.datetime): timestamp = start diff --git a/tools/send_test_data.py b/tools/send_test_data.py index 62484decee..dd5e1f1c56 100644 --- a/tools/send_test_data.py +++ b/tools/send_test_data.py @@ -21,23 +21,39 @@ source .tox/py27/bin/activate """ import argparse import datetime +import functools import json import random import uuid import make_test_data from oslo_context import context +import oslo_messaging from six import moves from ceilometer import messaging from ceilometer import service -def send_batch(rpc_client, topic, batch): +def send_batch_rpc(rpc_client, topic, batch): rpc_client.prepare(topic=topic).cast(context.RequestContext(), 'record_metering_data', data=batch) +def send_batch_notifier(notifier, topic, batch): + notifier.sample({}, event_type=topic, payload=batch) + + +def get_notifier(config_file): + service.prepare_service(argv=['/', '--config-file', config_file]) + return oslo_messaging.Notifier( + messaging.get_transport(), + driver='messagingv2', + publisher_id='telemetry.publisher.test', + topic='metering', + ) + + def get_rpc_client(config_file): service.prepare_service(argv=['/', '--config-file', config_file]) transport = messaging.get_transport() @@ -45,7 +61,7 @@ def get_rpc_client(config_file): return rpc_client -def generate_data(rpc_client, make_data_args, samples_count, +def generate_data(send_batch, make_data_args, samples_count, batch_size, resources_count, topic): make_data_args.interval = 1 make_data_args.start = (datetime.datetime.utcnow() - @@ -65,17 +81,24 @@ def generate_data(rpc_client, make_data_args, samples_count, sample['resource_id'] = resource batch.append(sample) if len(batch) == batch_size: - send_batch(rpc_client, topic, batch) + send_batch(topic, batch) batch = [] if count == samples_count: - send_batch(rpc_client, topic, batch) + send_batch(topic, batch) return resource_samples - send_batch(rpc_client, topic, batch) + send_batch(topic, batch) return resource_samples def get_parser(): parser = argparse.ArgumentParser() + parser.add_argument( + '--notify', + dest='notify', + type=bool, + default=True + ) + parser.add_argument( '--batch-size', dest='batch_size', @@ -113,12 +136,18 @@ def get_parser(): def main(): args = get_parser().parse_known_args()[0] make_data_args = make_test_data.get_parser().parse_known_args()[0] - rpc_client = get_rpc_client(args.config_file) + if args.notify: + notifier = get_notifier(args.config_file) + send_batch = functools.partial(send_batch_notifier, notifier) + else: + rpc_client = get_rpc_client(args.config_file) + send_batch = functools.partial(send_batch_rpc, rpc_client) result_dir = args.result_dir + del args.notify del args.config_file del args.result_dir - resource_writes = generate_data(rpc_client, make_data_args, + resource_writes = generate_data(send_batch, make_data_args, **args.__dict__) result_file = "%s/sample-by-resource-%s" % (result_dir, random.getrandbits(32))