summaryrefslogtreecommitdiffstats
path: root/storperf/utilities/data_handler.py
blob: ebc1bfd3dd1e784b0675e7894964d650fe7588ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
##############################################################################
# Copyright (c) 2016 Dell EMC and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

import logging
import os
from storperf.db import test_results_db
from storperf.db.graphite_db import GraphiteDB
from storperf.utilities import data_treatment as DataTreatment
from storperf.utilities import dictionary
from storperf.utilities import math as math
from storperf.utilities import steady_state as SteadyState
from time import sleep
import time


class DataHandler(object):

    def __init__(self):
        self.logger = logging.getLogger(__name__)
        self.samples = 11

    """
    """

    def data_event(self, executor):
        self.logger.debug("Event received")

        if executor.terminated:
            self._push_to_db(executor)
        else:
            steady_state = True
            metrics = {}
            for metric in ('lat.mean', 'iops', 'bw'):
                metrics[metric] = {}
                for io_type in ('read', 'write'):
                    metrics[metric][io_type] = {}

                    series = self._lookup_prior_data(executor, metric, io_type)
                    steady = self._evaluate_prior_data(series)

                    self.logger.debug("Steady state for %s %s: %s"
                                      % (io_type, metric, steady))

                    metrics[metric][io_type]['series'] = series
                    metrics[metric][io_type]['steady_state'] = steady
                    treated_data = DataTreatment.data_treatment(series)

                    metrics[metric][io_type]['slope'] = \
                        math.slope(treated_data['slope_data'])
                    metrics[metric][io_type]['range'] = \
                        math.range_value(treated_data['range_data'])
                    metrics[metric][io_type]['average'] = \
                        math.average(treated_data['average_data'])

                    if not steady:
                        steady_state = False

            executor.metadata['report_data'] = metrics
            executor.metadata['steady_state'] = steady_state

            workload_name = executor.current_workload.split('.')[1]

            if steady_state and not workload_name.startswith('_'):
                executor.terminate()

    def _lookup_prior_data(self, executor, metric, io_type):
        workload = executor.current_workload
        graphite_db = GraphiteDB()

        # A bit of a hack here as Carbon might not be finished storing the
        # data we just sent to it
        now = int(time.time())
        backtime = 60 * (self.samples + 2)
        data_series = graphite_db.fetch_series(workload,
                                               metric,
                                               io_type,
                                               now,
                                               backtime)
        most_recent_time = now
        if len(data_series) > 0:
            most_recent_time = data_series[-1][0]

        delta = now - most_recent_time
        self.logger.debug("Last update to graphite was %s ago" % delta)

        while (delta < 5 or (delta > 60 and delta < 120)):
            sleep(5)
            data_series = graphite_db.fetch_series(workload,
                                                   metric,
                                                   io_type,
                                                   now,
                                                   backtime)
            if len(data_series) > 0:
                most_recent_time = data_series[-1][0]
            delta = time.time() - most_recent_time
            self.logger.debug("Last update to graphite was %s ago" % delta)

        return data_series

    def _evaluate_prior_data(self, data_series):
        self.logger.debug("Data series: %s" % data_series)
        if len(data_series) == 0:
            return False
        earliest_timestamp = data_series[0][0]
        latest_timestamp = data_series[-1][0]
        duration = latest_timestamp - earliest_timestamp
        if (duration < 60 * self.samples):
            self.logger.debug("Only %s minutes of samples, ignoring" %
                              (duration / 60,))
            return False

        return SteadyState.steady_state(data_series)

    def _push_to_db(self, executor):
        test_db = os.environ.get('TEST_DB_URL')

        if test_db is not None:
            pod_name = dictionary.get_key_from_dict(executor.metadata,
                                                    'pod_name',
                                                    'Unknown')
            version = dictionary.get_key_from_dict(executor.metadata,
                                                   'version',
                                                   'Unknown')
            scenario = dictionary.get_key_from_dict(executor.metadata,
                                                    'scenario_name',
                                                    'Unknown')
            build_tag = dictionary.get_key_from_dict(executor.metadata,
                                                     'build_tag',
                                                     'Unknown')
            duration = executor.end_time - executor.start_time

            self.logger.info("Pushing results to %s" % (test_db))

            payload = executor.metadata
            payload['timestart'] = executor.start_time
            payload['duration'] = duration
            payload['status'] = 'OK'
            graphite_db = GraphiteDB()
            payload['metrics'] = graphite_db.fetch_averages(
                executor.job_db.job_id)
            criteria = {}
            criteria['block_sizes'] = executor.block_sizes
            criteria['queue_depths'] = executor.queue_depths

            try:
                test_results_db.push_results_to_db(test_db,
                                                   "storperf",
                                                   "Latency Test",
                                                   executor.start_time,
                                                   executor.end_time,
                                                   self.logger,
                                                   pod_name,
                                                   version,
                                                   scenario,
                                                   criteria,
                                                   build_tag,
                                                   payload)
            except:
                self.logger.exception("Error pushing results into Database")