Enhancing the make_test_data script

-Adding random boundaries rather than exact value for samples
-Allowing to pass datetime object or string
-Extracting make_test_data func, so it can be imported elsewhere
-Converting the integer time span to datetime

Change-Id: Iffbc8d9711b2f301a1f28b49980691ab34d17c20
Related-Bug: #1349791
This commit is contained in:
Ladislav Smola
2014-07-28 09:41:21 +00:00
parent 9936b3469c
commit 700a7ce3bd

View File

@@ -16,13 +16,21 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
"""Command line tool for creating test data for ceilometer. """Command line tool for creating test data for Ceilometer.
Usage:
Generate testing data for e.g. for default time span
source .tox/py27/bin/activate
./tools/make_test_data.py --user 1 --project 1 1 cpu_util 20
""" """
from __future__ import print_function from __future__ import print_function
import argparse import argparse
import datetime import datetime
import logging import logging
import random
import sys import sys
from oslo.config import cfg from oslo.config import cfg
@@ -32,6 +40,62 @@ from ceilometer import sample
from ceilometer import storage from ceilometer import storage
from ceilometer.openstack.common import timeutils from ceilometer.openstack.common import timeutils
def make_test_data(conn, name, meter_type, unit, volume, random_min,
random_max, user_id, project_id, resource_id, start,
end, interval, resource_metadata={}, source='artificial',):
# Compute start and end timestamps for the new data.
if isinstance(start, datetime.datetime):
timestamp = start
else:
timestamp = timeutils.parse_strtime(start)
if not isinstance(end, datetime.datetime):
end = timeutils.parse_strtime(end)
increment = datetime.timedelta(minutes=interval)
print('Adding new events for meter %s.' % (name))
# Generate events
n = 0
total_volume = volume
while timestamp <= end:
if (random_min >= 0 and random_max >= 0):
# If there is a random element defined, we will add it to
# user given volume.
if isinstance(random_min, int) and isinstance(random_max, int):
total_volume += random.randint(random_min, random_max)
else:
total_volume += random.uniform(random_min, random_max)
c = sample.Sample(name=name,
type=meter_type,
unit=unit,
volume=total_volume,
user_id=user_id,
project_id=project_id,
resource_id=resource_id,
timestamp=timestamp,
resource_metadata=resource_metadata,
source=source,
)
data = utils.meter_message_from_counter(
c,
cfg.CONF.publisher.metering_secret)
conn.record_metering_data(data)
n += 1
timestamp = timestamp + increment
if (meter_type == 'gauge' or meter_type == 'delta'):
# For delta and gauge, we don't want to increase the value
# in time by random element. So we always set it back to
# volume.
total_volume = volume
print('Added %d new events for meter %s.' % (n, name))
def main(): def main():
cfg.CONF([], project='ceilometer') cfg.CONF([], project='ceilometer')
@@ -74,6 +138,18 @@ def main():
'--user', '--user',
help='User id of owner.', help='User id of owner.',
) )
parser.add_argument(
'--random_min',
help='The random min border of amount for added to given volume.',
type=int,
default=0,
)
parser.add_argument(
'--random_max',
help='The random max border of amount for added to given volume.',
type=int,
default=0,
)
parser.add_argument( parser.add_argument(
'resource', 'resource',
help='The resource id for the meter data.', help='The resource id for the meter data.',
@@ -110,34 +186,25 @@ def main():
args.project = r.project_id args.project = r.project_id
break break
# Compute start and end timestamps for the # Compute the correct time span
# new data. start = datetime.datetime.utcnow() - datetime.timedelta(days=args.start)
timestamp = timeutils.parse_isotime(args.start) end = datetime.datetime.utcnow() + datetime.timedelta(days=args.end)
end = timeutils.parse_isotime(args.end)
increment = datetime.timedelta(minutes=args.interval)
# Generate events make_test_data(conn = conn,
n = 0 name=args.counter,
while timestamp <= end: meter_type=args.type,
c = sample.Sample(name=args.counter, unit=args.unit,
type=args.type, volume=args.volume,
unit=args.unit, random_min=args.random_min,
volume=args.volume, random_max=args.random_max,
user_id=args.user, user_id=args.user,
project_id=args.project, project_id=args.project,
resource_id=args.resource, resource_id=args.resource,
timestamp=timestamp, start=start,
resource_metadata={}, end=end,
source='artificial', interval=args.interval,
) resource_metadata={},
data = utils.meter_message_from_counter( source='artificial',)
c,
cfg.CONF.publisher.metering_secret)
conn.record_metering_data(data)
n += 1
timestamp = timestamp + increment
print('Added %d new events' % n)
return 0 return 0