• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""This is a script that inserts a bunch of entries into
6   elasticdb by reporting stats with metadata in the stats module.
7
8Usage:
9    # runs tests on all stats objects on prod instance of es
10    python stats_es_functionaltest.py --all --es_host=prod
11    # runs tests on all stats objects on test instance of es (localhost)
12    python stats_es_functionaltest.py --all --es_host=test
13
14    python stats_es_functionaltest.py --test=timer # runs tests on timer obj.
15"""
16
17import optparse
18import time
19
20import common
21from autotest_lib.client.common_lib.cros.graphite import autotest_es
22from autotest_lib.client.common_lib.cros.graphite import es_test_utils
23
24
25TESTS_ALL = ['timer', 'gauge', 'raw', 'average', 'counter']
26
27
28class StatsFunctionalTest(object):
29    """Test stats module with metadata"""
30
31    def __init__(self, es_host, es_port, index):
32        self.host = es_host
33        self.port = es_port
34        self.index = index
35        self.wait_time = 6 # Bulk flush is 5 seconds
36        if autotest_es.ES_USE_HTTP:
37            # No flush time for http requests.
38            self.wait_time = 2
39
40    def run_tests(self, tests=TESTS_ALL,
41                  num_entries=10,
42                  keys=['job_id', 'host_id', 'job_start']):
43        """Runs test listed in the param tests.
44
45        @param tests: list of tests to run
46        @param num_entries: number of metadata entries to insert
47        @param keys: keys each metadata dictionary will have
48
49        """
50        for test_type in tests:
51            if test_type not in TESTS_ALL:
52                print 'Skipping test %s, it is not supported. ' % (test_type)
53            es_test_utils.clear_index(index=self.index,
54                                      host=self.host,
55                                      port=self.port,
56                                      timeout=10)
57            print 'running %s test.' % (test_type)
58            self._run_one_test_metadata(test_type, num_entries, keys)
59
60
61    def _run_one_test_metadata(self, test_type, num_entries, keys):
62        """Puts many entries into elasticdb, then query it. """
63
64        print ('preparing to insert %s entries with keys %s into elasticdb...'
65               % (num_entries, keys))
66        es_test_utils.sequential_random_insert_ints(
67                keys=keys,
68                target_type=test_type,
69                index=self.index,
70                host=self.host,
71                port=self.port,
72                use_http = autotest_es.ES_USE_HTTP,
73                udp_port = autotest_es.ES_UDP_PORT,
74                num_entries=num_entries,
75                print_interval=num_entries/5)
76        # Wait a bit for es to be populated with the metadata entry.
77        # I set this to 6 seconds because bulk.udp.flush_interval (es server)
78        # is configured to be 5 seconds.
79        print 'waiting %s seconds...' % (self.wait_time)
80        time.sleep(self.wait_time)
81        result = autotest_es.query(host=self.host, port=self.port,
82                                   index=self.index, fields_returned=keys,
83                                   range_constraints=[('host_id', 0, None)])
84        if not result:
85            print ('%s test error: Index %s not found.'
86                   %(test_type, self.index))
87            return
88
89        # TODO(michaelliang): Check hits and total are valid keys at each layer.
90        num_entries_found = result.total
91        print('  Inserted %s entries, found %s entries.'
92              %(num_entries, num_entries_found))
93        if num_entries_found != num_entries:
94            print '\n\n%s test failed! \n\n' % (test_type)
95        else:
96            print '\n\n%s test passed! \n\n' % (test_type)
97
98
99def main():
100    """main script. """
101
102    parser = optparse.OptionParser()
103    parser.add_option('--all', action='store_true', dest='run_all',
104                      default=False,
105                      help='set --all flag to run all tests.')
106    parser.add_option('--test', type=str,
107            help=('Enter subset of [\'timer\', \'gauge\', \'raw\','
108                  '\'average\', \'counter\']'),
109            dest='test_to_run',
110            default=None)
111    parser.add_option('--es_host', type=str,
112            help=('Enter "prod" or "test" or an ip'),
113            dest='es_host',
114            default='localhost')
115    parser.add_option('--es_port', type=int,
116            help=('Enter port of es instance, usually 9200'),
117            dest='es_port',
118            default=9200)
119    options, _ = parser.parse_args()
120
121
122    if not options.run_all and not options.test_to_run:
123        print ('No tests specified.'
124               'For help: python stats_es_functionaltest.py -h')
125    if options.es_host == 'prod':
126        es_host = autotest_es.METADATA_ES_SERVER
127        es_port = autotest_es.ES_PORT
128    elif options.es_host == 'test':
129        es_host = 'http://localhost'
130        es_port = autotest_es.ES_PORT
131    else:
132        es_host = options.es_host
133        es_port = options.es_port
134    test_obj = StatsFunctionalTest(es_host,
135                                   es_port,
136                                   'stats_es_functionaltest')
137    if options.run_all:
138        test_obj.run_tests()
139    elif options.test_to_run:
140        test_obj.run_tests([options.test_to_run])
141
142
143if __name__ == '__main__':
144    main()
145