summaryrefslogtreecommitdiffstats
path: root/tests/utilities_tests/data_handler_test.py
blob: b175c8754eb95f51ac71ba6263737d543b0fb1aa (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
##############################################################################
# Copyright (c) 2016 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 os
from storperf.utilities.data_handler import DataHandler
import unittest

import mock


class MockGraphiteDB(object):

    def __init__(self):
        self.called = False
        self.series = []

    def fetch_averages(self, job_id):
        self.called = True
        return None

    def fetch_series(self, job_id, timeframe):
        return self.series


class DataHandlerTest(unittest.TestCase):

    def setUp(self):
        self.event_listeners = set()
        self.data_handler = DataHandler()
        self._terminated = False
        self.args = None
        self.start_time = 0
        self.end_time = 1
        self.metadata = {}
        self.block_sizes = "1"
        self.queue_depths = "1"
        mock.job_id = "1"
        self.job_db = mock
        self.pushed = False
        self.current_workload = None
        pass

    @property
    def terminated(self):
        return self._terminated

    def push_results_to_db(self, *args):
        self.pushed = True
        pass

    def terminate(self):
        self._terminated = True

    @mock.patch("time.time")
    @mock.patch.dict(os.environ, {'TEST_DB_URL': 'mock'})
    @mock.patch("storperf.db.graphite_db.GraphiteDB.fetch_series")
    def test_lookup_prior_data(self, mock_graphite_db, mock_time):
        self._terminated = False
        expected = [[1480455910, 205.345],
                    [1480455920, 201.59],
                    [1480455930, 205.76],
                    [1480455970, 215.655],
                    [1480455980, 214.16],
                    [1480455990, 213.57],
                    [1480456030, 219.37],
                    [1480456040, 219.28],
                    [1480456050, 217.75]]
        mock_graphite_db.return_value = expected
        mock_time.return_value = expected[-1][0] + 10

        self.current_workload = ("%s.%s.queue-depth.%s.block-size.%s" %
                                 ("job_id",
                                  "rw",
                                  8,
                                  8192))

        actual = self.data_handler._lookup_prior_data(self, 'read', 'iops')
        self.assertEqual(expected, actual)

    def test_short_sample(self):
        series = [[1480455910, 205.345],
                  [1480455920, 201.59],
                  [1480455930, 205.76],
                  [1480455970, 215.655],
                  [1480455980, 214.16],
                  [1480455990, 213.57],
                  [1480456030, 219.37],
                  [1480456040, 219.28],
                  [1480456050, 217.75]]

        actual = self.data_handler._evaluate_prior_data(series)
        self.assertEqual(False, actual)

    def test_long_not_steady_sample(self):
        series = [[4804559100, 205345],
                  [4804559200, 20159],
                  [4804559300, 20576],
                  [4804560300, 21937],
                  [4804560400, 21928],
                  [4804560500, 21775]]
        actual = self.data_handler._evaluate_prior_data(series)
        self.assertEqual(False, actual)

    def test_long_steady_sample(self):
        series = [[4804559100, 205.345],
                  [4804559200, 201.59],
                  [4804559300, 205.76],
                  [4804560300, 219.37],
                  [4804560400, 219.28],
                  [4804560500, 217.75]]
        actual = self.data_handler._evaluate_prior_data(series)
        self.assertEqual(True, actual)

    @mock.patch.dict(os.environ, {'TEST_DB_URL': 'mock'})
    @mock.patch("storperf.db.test_results_db.push_results_to_db")
    @mock.patch("storperf.utilities.data_handler.GraphiteDB")
    def test_terminated_report(self, mock_graphite_db, mock_results_db):
        self._terminated = True
        mock_results_db.side_effect = self.push_results_to_db
        mock_graphite_db.side_effect = MockGraphiteDB

        self.data_handler.data_event(self)
        self.assertEqual(True, self.pushed)

    @mock.patch.dict(os.environ, {'TEST_DB_URL': 'mock'})
    @mock.patch("time.time")
    @mock.patch("storperf.db.test_results_db.push_results_to_db")
    @mock.patch("storperf.db.graphite_db.GraphiteDB.fetch_series")
    def test_non_terminated_report(self, mock_graphite_db, mock_results_db,
                                   mock_time):
        self._terminated = False
        mock_results_db.side_effect = self.push_results_to_db
        series = \
            [[1480455910, 205.345],
             [1480455920, 201.59],
             [1480455930, 205.76],
             [1480455970, 215.655],
             [1480455980, 214.16],
             [1480455990, 213.57],
             [1480456030, 219.37],
             [1480456040, 219.28],
             [1480456050, 217.75]]
        mock_graphite_db.return_value = series
        mock_time.return_value = series[-1][0] + 10
        expected_slope = 0.1185333530108134
        expected_range = 17.78
        expected_average = 212.49777777777774

        self.current_workload = ("%s.%s.queue-depth.%s.block-size.%s" %
                                 ("job_id",
                                  "rw",
                                  8,
                                  8192))

        self.data_handler.data_event(self)
        self.assertEqual(False, self.pushed)
        self.assertEqual(False, self._terminated)

        self.assertEqual(expected_slope, self.metadata['report_data']
                         ['lat.mean']
                         ['read']
                         ['slope'])
        self.assertEqual(expected_range, self.metadata['report_data']
                         ['lat.mean']
                         ['read']
                         ['range'])
        self.assertEqual(expected_average, self.metadata['report_data']
                         ['lat.mean']
                         ['read']
                         ['average'])
        self.assertEqual(series, self.metadata['report_data']
                         ['lat.mean']
                         ['read']
                         ['series'])

    @mock.patch.dict(os.environ, {'TEST_DB_URL': 'mock'})
    @mock.patch("time.time")
    @mock.patch("storperf.db.test_results_db.push_results_to_db")
    @mock.patch("storperf.db.graphite_db.GraphiteDB.fetch_series")
    def test_report_that_causes_termination(self,
                                            mock_graphite_db,
                                            mock_results_db,
                                            mock_time):
        self._terminated = False
        mock_results_db.side_effect = self.push_results_to_db
        series = [[4804559100, 205.345],
                  [4804559200, 201.59],
                  [4804559300, 205.76],
                  [4804560300, 219.37],
                  [4804560400, 219.28],
                  [4804560500, 217.75]]
        mock_graphite_db.return_value = series
        mock_time.return_value = 4804560500 + 10

        expected_slope = 0.011830471529818998
        expected_range = 17.78
        expected_average = 211.51583333333335

        self.current_workload = ("%s.%s.queue-depth.%s.block-size.%s" %
                                 ("job_id",
                                  "rw",
                                  8,
                                  8192))

        self.data_handler.data_event(self)

        self.assertEqual(expected_slope, self.metadata['report_data']
                         ['lat.mean']
                         ['read']
                         ['slope'])
        self.assertEqual(expected_range, self.metadata['report_data']
                         ['lat.mean']
                         ['read']
                         ['range'])
        self.assertEqual(expected_average, self.metadata['report_data']
                         ['lat.mean']
                         ['read']
                         ['average'])
        self.assertEqual(series, self.metadata['report_data']
                         ['lat.mean']
                         ['read']
                         ['series'])
        self.assertEqual(True, self._terminated)

        self.assertEqual(False, self.pushed)
        self.assertEqual(True, self._terminated)