~ubuntu-branches/ubuntu/vivid/ceilometer/vivid

« back to all changes in this revision

Viewing changes to tools/send_test_data.py

  • Committer: Package Import Robot
  • Author(s): James Page, Corey Bryant, James Page
  • Date: 2015-02-19 14:59:07 UTC
  • mfrom: (1.2.3)
  • Revision ID: package-import@ubuntu.com-20150219145907-9jojybdsl64zcn14
Tags: 2015.1~b2-0ubuntu1
[ Corey Bryant ]
* New upstream release.
  - d/control: Align requirements with upstream.
  - d/p/skip-test.patch: Rebased.

[ James Page ]
* d/rules,d/p/skip-gabbi.patch: Skip tests that rely on python-gabbi until
  packaging and MIR is complete.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Licensed under the Apache License, Version 2.0 (the "License"); you may
 
2
# not use this file except in compliance with the License. You may obtain
 
3
# a copy of the License at
 
4
#
 
5
# http://www.apache.org/licenses/LICENSE-2.0
 
6
#
 
7
# Unless required by applicable law or agreed to in writing, software
 
8
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
9
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
10
# License for the specific language governing permissions and limitations
 
11
# under the License.
 
12
 
 
13
"""Command line tool for sending test data for Ceilometer via oslo.messaging.
 
14
 
 
15
Usage:
 
16
 
 
17
Send messages with samples generated by make_test_data
 
18
 
 
19
source .tox/py27/bin/activate
 
20
./tools/send_test_data.py --count 1000 --resources_count 10 --topic metering
 
21
"""
 
22
import argparse
 
23
import datetime
 
24
import json
 
25
import random
 
26
import uuid
 
27
 
 
28
import make_test_data
 
29
from oslo_context import context
 
30
 
 
31
from ceilometer import messaging
 
32
from ceilometer import service
 
33
 
 
34
 
 
35
def send_batch(rpc_client, topic, batch):
 
36
    rpc_client.prepare(topic=topic).cast(context.RequestContext(),
 
37
                                         'record_metering_data', data=batch)
 
38
 
 
39
 
 
40
def get_rpc_client(config_file):
 
41
    service.prepare_service(argv=['/', '--config-file', config_file])
 
42
    transport = messaging.get_transport()
 
43
    rpc_client = messaging.get_rpc_client(transport, version='1.0')
 
44
    return rpc_client
 
45
 
 
46
 
 
47
def generate_data(rpc_client, make_data_args, samples_count,
 
48
                  batch_size, resources_count, topic):
 
49
    make_data_args.interval = 1
 
50
    make_data_args.start = (datetime.datetime.utcnow() -
 
51
                            datetime.timedelta(minutes=samples_count))
 
52
    make_data_args.end = datetime.datetime.utcnow()
 
53
 
 
54
    make_data_args.resource_id = None
 
55
    resources_list = [str(uuid.uuid4())
 
56
                      for _ in xrange(resources_count)]
 
57
    resource_samples = {resource: 0 for resource in resources_list}
 
58
    batch = []
 
59
    count = 0
 
60
    for sample in make_test_data.make_test_data(**make_data_args.__dict__):
 
61
        count += 1
 
62
        resource = resources_list[random.randint(0, len(resources_list) - 1)]
 
63
        resource_samples[resource] += 1
 
64
        sample['resource_id'] = resource
 
65
        batch.append(sample)
 
66
        if len(batch) == batch_size:
 
67
            send_batch(rpc_client, topic, batch)
 
68
            batch = []
 
69
        if count == samples_count:
 
70
            send_batch(rpc_client, topic, batch)
 
71
            return resource_samples
 
72
    send_batch(rpc_client, topic, batch)
 
73
    return resource_samples
 
74
 
 
75
 
 
76
def get_parser():
 
77
    parser = argparse.ArgumentParser()
 
78
    parser.add_argument(
 
79
        '--batch-size',
 
80
        dest='batch_size',
 
81
        type=int,
 
82
        default=100
 
83
    )
 
84
    parser.add_argument(
 
85
        '--config-file',
 
86
        default='/etc/ceilometer/ceilometer.conf'
 
87
    )
 
88
    parser.add_argument(
 
89
        '--topic',
 
90
        default='perfmetering'
 
91
    )
 
92
    parser.add_argument(
 
93
        '--samples-count',
 
94
        dest='samples_count',
 
95
        type=int,
 
96
        default=1000
 
97
    )
 
98
    parser.add_argument(
 
99
        '--resources-count',
 
100
        dest='resources_count',
 
101
        type=int,
 
102
        default=100
 
103
    )
 
104
    parser.add_argument(
 
105
        '--result-directory',
 
106
        dest='result_dir',
 
107
        default='/tmp'
 
108
    )
 
109
    return parser
 
110
 
 
111
 
 
112
def main():
 
113
    args = get_parser().parse_known_args()[0]
 
114
    make_data_args = make_test_data.get_parser().parse_known_args()[0]
 
115
    rpc_client = get_rpc_client(args.config_file)
 
116
    result_dir = args.result_dir
 
117
    del args.config_file
 
118
    del args.result_dir
 
119
 
 
120
    resource_writes = generate_data(rpc_client, make_data_args,
 
121
                                    **args.__dict__)
 
122
    result_file = "%s/sample-by-resource-%s" % (result_dir,
 
123
                                                random.getrandbits(32))
 
124
    with open(result_file, 'w') as f:
 
125
        f.write(json.dumps(resource_writes))
 
126
    return result_file
 
127
 
 
128
 
 
129
if __name__ == '__main__':
 
130
    main()