blob: 2ec1fd8c9b8fddf942a052c489e4bead016b1519 [file] [log] [blame]
Javi Merino4ec4aee2016-01-05 15:01:56 +00001# Copyright 2015-2016 ARM Limited
Javi Merino034e7cc2015-04-22 18:39:21 +01002#
Javi Merinoaace7c02015-08-10 14:10:47 +01003# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14#
15
KP Singh7319a882014-12-24 18:18:01 +000016
17import unittest
18import matplotlib
John Pocock96602fc2016-01-15 14:29:11 +000019import numpy as np
KP Singh7319a882014-12-24 18:18:01 +000020import pandas as pd
Kapileshwar Singh5ebf1a32015-02-06 15:50:41 +000021import tempfile
22import os
Javi Merino7b860d52015-12-17 11:14:07 +000023import warnings
KP Singh7319a882014-12-24 18:18:01 +000024
25from test_thermal import BaseTestThermal
Javi Merino435457c2015-08-10 15:59:10 +010026import trappy
KP Singh7319a882014-12-24 18:18:01 +000027
28
29class TestPlotter(BaseTestThermal):
30
31 """No Bombing testcases for plotter"""
32
33 def __init__(self, *args, **kwargs):
34 super(TestPlotter, self).__init__(*args, **kwargs)
35
36 def test_plot_no_pivot(self):
37 """Tests LinePlot with no pivot"""
Javi Merinoc26a3232015-12-11 18:00:30 +000038 trace1 = trappy.FTrace(name="first")
39 l = trappy.LinePlot(trace1, trappy.thermal.Thermal, column="temp")
Kapileshwar Singha2040442015-02-23 12:19:18 +000040 l.view(test=True)
KP Singh7319a882014-12-24 18:18:01 +000041
Javi Merinoc26a3232015-12-11 18:00:30 +000042 def test_plot_multi_trace(self):
43 """Tests LinePlot with no Pivot multi traces"""
44 trace1 = trappy.FTrace(name="first")
45 trace2 = trappy.FTrace(name="second")
Javi Merino435457c2015-08-10 15:59:10 +010046 l = trappy.LinePlot(
Javi Merinoc26a3232015-12-11 18:00:30 +000047 [trace1, trace2], trappy.thermal.Thermal, column="temp")
Kapileshwar Singha2040442015-02-23 12:19:18 +000048 l.view(test=True)
KP Singh7319a882014-12-24 18:18:01 +000049
50 def test_plot_multi(self):
51 """Tests LinePlot with no Pivot multi attrs"""
Javi Merinoc26a3232015-12-11 18:00:30 +000052 trace1 = trappy.FTrace(name="first")
53 trace2 = trappy.FTrace(name="second")
54 l = trappy.LinePlot([trace1,
55 trace2],
Javi Merino435457c2015-08-10 15:59:10 +010056 [trappy.thermal.Thermal,
57 trappy.thermal.ThermalGovernor],
Kapileshwar Singh18820752015-02-11 12:01:08 +000058 column=["temp",
59 "power_range"])
Kapileshwar Singha2040442015-02-23 12:19:18 +000060 l.view(test=True)
KP Singh7319a882014-12-24 18:18:01 +000061
62 def test_plot_filter(self):
63 """Tests LinePlot with no Pivot with filters"""
Javi Merinoc26a3232015-12-11 18:00:30 +000064 trace1 = trappy.FTrace(name="first")
65 trace2 = trappy.FTrace(name="second")
66 l = trappy.LinePlot([trace1,
67 trace2],
Javi Merino435457c2015-08-10 15:59:10 +010068 [trappy.cpu_power.CpuOutPower],
Kapileshwar Singh18820752015-02-11 12:01:08 +000069 column=["power"],
Javi Merinoeda46e82016-02-23 19:31:33 +000070 filters={"cdev_state": [0]})
Kapileshwar Singha2040442015-02-23 12:19:18 +000071 l.view(test=True)
KP Singh7319a882014-12-24 18:18:01 +000072
73 def test_plot_pivot(self):
74 """Tests LinePlot with Pivot"""
Javi Merinoc26a3232015-12-11 18:00:30 +000075 trace1 = trappy.FTrace(name="first")
Javi Merino435457c2015-08-10 15:59:10 +010076 l = trappy.LinePlot(
Javi Merinoc26a3232015-12-11 18:00:30 +000077 trace1,
Javi Merino435457c2015-08-10 15:59:10 +010078 trappy.thermal.Thermal,
KP Singh7319a882014-12-24 18:18:01 +000079 column="temp",
80 pivot="thermal_zone")
Kapileshwar Singha2040442015-02-23 12:19:18 +000081 l.view(test=True)
KP Singh7319a882014-12-24 18:18:01 +000082
Javi Merinoc26a3232015-12-11 18:00:30 +000083 def test_plot_multi_trace_pivot(self):
84 """Tests LinePlot with Pivot multi traces"""
85 trace1 = trappy.FTrace(name="first")
86 trace2 = trappy.FTrace(name="second")
Javi Merino435457c2015-08-10 15:59:10 +010087 l = trappy.LinePlot(
Javi Merinoc26a3232015-12-11 18:00:30 +000088 [trace1, trace2], trappy.cpu_power.CpuOutPower, column="power", pivot="cpus")
Kapileshwar Singha2040442015-02-23 12:19:18 +000089 l.view(test=True)
KP Singh7319a882014-12-24 18:18:01 +000090
91 def test_plot_multi_pivot(self):
92 """Tests LinePlot with Pivot with multi attrs"""
Javi Merinoc26a3232015-12-11 18:00:30 +000093 trace1 = trappy.FTrace(name="first")
94 trace2 = trappy.FTrace(name="second")
95 l = trappy.LinePlot([trace1,
96 trace2],
Javi Merino435457c2015-08-10 15:59:10 +010097 [trappy.cpu_power.CpuInPower,
98 trappy.cpu_power.CpuOutPower],
Kapileshwar Singh18820752015-02-11 12:01:08 +000099 column=["dynamic_power",
100 "power"],
101 pivot="cpus")
Kapileshwar Singha2040442015-02-23 12:19:18 +0000102 l.view(test=True)
KP Singh7319a882014-12-24 18:18:01 +0000103
104 def test_plot_multi_pivot_filter(self):
105 """Tests LinePlot with Pivot and filters"""
Javi Merinoc26a3232015-12-11 18:00:30 +0000106 trace1 = trappy.FTrace(name="first")
107 trace2 = trappy.FTrace(name="second")
Javi Merino435457c2015-08-10 15:59:10 +0100108 l = trappy.LinePlot(
Javi Merinoc26a3232015-12-11 18:00:30 +0000109 trace1,
Javi Merino435457c2015-08-10 15:59:10 +0100110 trappy.cpu_power.CpuInPower,
KP Singh7319a882014-12-24 18:18:01 +0000111 column=[
Kapileshwar Singh18820752015-02-11 12:01:08 +0000112 "dynamic_power",
KP Singh7319a882014-12-24 18:18:01 +0000113 "load1"],
114 filters={
115 "cdev_state": [
116 1,
117 0]},
118 pivot="cpus")
Kapileshwar Singha2040442015-02-23 12:19:18 +0000119 l.view(test=True)
Kapileshwar Singh5ebf1a32015-02-06 15:50:41 +0000120
121 def test_plot_savefig(self):
122 """Tests plotter: savefig"""
Javi Merinoc26a3232015-12-11 18:00:30 +0000123 trace1 = trappy.FTrace(name="first")
124 trace2 = trappy.FTrace(name="second")
Javi Merino435457c2015-08-10 15:59:10 +0100125 l = trappy.LinePlot(
Javi Merinoc26a3232015-12-11 18:00:30 +0000126 trace1,
Javi Merino435457c2015-08-10 15:59:10 +0100127 trappy.cpu_power.CpuInPower,
Kapileshwar Singh5ebf1a32015-02-06 15:50:41 +0000128 column=[
129 "dynamic_power",
130 "load1"],
131 filters={
132 "cdev_state": [
133 1,
134 0]},
135 pivot="cpus")
136 png_file = tempfile.mktemp(dir="/tmp", suffix=".png")
137 l.savefig(png_file)
138 self.assertTrue(os.path.isfile(png_file))
139 os.remove(png_file)
Kapileshwar Singh14d1d672015-11-27 15:48:52 +0000140
141
142 def test_signals(self):
143 """Test signals input for LinePlot"""
144
Javi Merinoc26a3232015-12-11 18:00:30 +0000145 trace1 = trappy.FTrace(name="first")
146 trace2 = trappy.FTrace(name="second")
Kapileshwar Singh14d1d672015-11-27 15:48:52 +0000147
Javi Merinoc26a3232015-12-11 18:00:30 +0000148 l = trappy.LinePlot([trace1,
149 trace2],
Kapileshwar Singh14d1d672015-11-27 15:48:52 +0000150 signals=["cpu_in_power:dynamic_power",
151 "cpu_out_power:power"],
152 pivot="cpus")
153
Javi Merino8cff44f2016-06-17 18:54:41 +0100154 self.assertTrue(isinstance(l.templates[0], type(trappy.cpu_power.CpuInPower)))
155 self.assertEquals(l._attr["column"][0], "dynamic_power")
156 self.assertTrue(l.templates[1], type(trappy.cpu_power.CpuOutPower))
157 self.assertEquals(l._attr["column"][1], "power")
Javi Merinoe5c35972016-06-17 19:51:47 +0100158 self.assertTrue("colors" not in l._attr)
Javi Merino8cff44f2016-06-17 18:54:41 +0100159
160 # Check that plotting doesn't barf
Kapileshwar Singh14d1d672015-11-27 15:48:52 +0000161 l.view(test=True)
162
163
164 def test_signals_exceptions(self):
165 """Test incorrect input combinations: signals"""
166
Javi Merinoc26a3232015-12-11 18:00:30 +0000167 trace1 = trappy.FTrace(name="first")
168 trace2 = trappy.FTrace(name="second")
Kapileshwar Singh14d1d672015-11-27 15:48:52 +0000169
170 with self.assertRaises(ValueError):
Javi Merinoc26a3232015-12-11 18:00:30 +0000171 l = trappy.LinePlot([trace1, trace2],
Kapileshwar Singh14d1d672015-11-27 15:48:52 +0000172 column=[
173 "dynamic_power",
174 "load1"],
175 signals=["cpu_in_power:dynamic_power",
176 "cpu_out_power:power"],
177 pivot="cpus")
178
179 with self.assertRaises(ValueError):
Javi Merinoc26a3232015-12-11 18:00:30 +0000180 l = trappy.LinePlot([trace1, trace2],
Kapileshwar Singh14d1d672015-11-27 15:48:52 +0000181 trappy.cpu_power.CpuInPower,
182 signals=["cpu_in_power:dynamic_power",
183 "cpu_out_power:power"],
184 pivot="cpus")
185
186 with self.assertRaises(ValueError):
Javi Merinoc26a3232015-12-11 18:00:30 +0000187 l = trappy.LinePlot([trace1, trace2],
Kapileshwar Singh14d1d672015-11-27 15:48:52 +0000188 trappy.cpu_power.CpuInPower,
189 column=[
190 "dynamic_power",
191 "load1"],
192 signals=["cpu_in_power:dynamic_power",
193 "cpu_out_power:power"],
194 pivot="cpus")
Javi Merino7b860d52015-12-17 11:14:07 +0000195
Javi Merinoe5c35972016-06-17 19:51:47 +0100196 def test_signals_colors(self):
197 """Test signals with colors in LinePlot"""
198
199 trace1 = trappy.FTrace(name="first")
200 trace2 = trappy.FTrace(name="second")
201
202 l = trappy.LinePlot([trace1, trace2],
203 signals=["thermal:temp:1,2,3",
204 "cpu_in_power:load2:200,100,0"],
205 pivot="cpus")
206
207 self.assertTrue(isinstance(l.templates[0], type(trappy.thermal.Thermal)))
208 self.assertEquals(l._attr["column"][0], "temp")
209 self.assertEquals(l._attr["colors"][0], [1, 2, 3])
210 self.assertTrue(l.templates[1], type(trappy.cpu_power.CpuInPower))
211 self.assertEquals(l._attr["column"][1], "load2")
212 self.assertEquals(l._attr["colors"][1], [200, 100, 0])
213
214 # Check that plotting doesn't barf
215 l.view(test=True)
216
217 # Test hex color
218 l = trappy.LinePlot([trace1, trace2],
219 signals=["thermal:prev_temp:0xff,0x3a,0x3"],
220 pivot="cpus")
221 self.assertEquals(l._attr["colors"][0], [0xff, 0x3a, 0x3])
222
John Pocock96602fc2016-01-15 14:29:11 +0000223 def test_lineplot_dataframe(self):
224 """LinePlot plots DataFrames without exploding"""
225 data = np.random.randn(4, 2)
226 dfr = pd.DataFrame(data, columns=["tick", "tock"]).cumsum()
227 trappy.LinePlot(dfr, column=["tick"]).view(test=True)
228
Javi Merino7b860d52015-12-17 11:14:07 +0000229 def test_get_trace_event_data_corrupted_trace(self):
230 """get_trace_event_data() works with a corrupted trace"""
231 from trappy.plotter.Utils import get_trace_event_data
232
Javi Merinoc26a3232015-12-11 18:00:30 +0000233 trace = trappy.FTrace()
Javi Merino7b860d52015-12-17 11:14:07 +0000234
235 # We create this trace:
236 #
237 # 1 15414 -> 15411
238 # 2 15411 -> 15414
239 # 3 15414 -> 15411 (corrupted, should be dropped)
240 # 4 15413 -> 15411
241 # 5 15411 -> 15413
242 #
243 # Which should plot like:
244 #
245 # CPU
246 # +-------+-------+
247 # 0 | 15411 | 15414 |
248 # +-------+-------+ +-------+
249 # 1 | 15411 |
250 # +-------+
251 # +-------+-------+-------+-------+
252 # 0.1 0.2 0.3 0.4 0.5
253
254 broken_trace = pd.DataFrame({
255 '__comm': ["task2", "task1", "task2", "task3", "task1"],
256 '__cpu': [0, 0, 0, 1, 1],
257 '__pid': [15414, 15411, 15414, 15413, 15411],
258 'next_comm': ["task1", "task2", "task1", "task1", "task3"],
259 'next_pid': [15411, 15414, 15411, 15411, 15413],
260 'prev_comm': ["task2", "task1", "task2", "task3", "task1"],
261 'prev_pid': [15414, 15411, 15414, 15413, 15411],
262 'prev_state': ["S", "R", "S", "S", "S"]},
263 index=pd.Series(range(1, 6), name="Time"))
264
Javi Merinoc26a3232015-12-11 18:00:30 +0000265 trace.sched_switch.data_frame = broken_trace
Javi Merino7b860d52015-12-17 11:14:07 +0000266
267 with warnings.catch_warnings(record=True) as warn:
Javi Merinoc26a3232015-12-11 18:00:30 +0000268 data, procs, window = get_trace_event_data(trace)
Javi Merino7b860d52015-12-17 11:14:07 +0000269 self.assertEquals(len(warn), 1)
270
271 warn_str = str(warn[-1])
272 self.assertTrue("15411" in warn_str)
273 self.assertTrue("4" in warn_str)
274
275 zipped_comms = zip(broken_trace["next_comm"], broken_trace["next_pid"])
276 expected_procs = set("-".join([comm, str(pid)]) for comm, pid in zipped_comms)
277
278 self.assertTrue([1, 2, 0] in data["task1-15411"])
279 self.assertTrue([2, 3, 0] in data["task2-15414"])
280 self.assertTrue([4, 5, 1] in data["task1-15411"])
281 self.assertEquals(procs, expected_procs)
282 self.assertEquals(window, [1, 5])
Javi Merino3ffa9e22016-01-29 10:19:24 +0100283
Javi Merinof2666cb2016-03-09 18:38:44 +0000284class TestILinePlotter(unittest.TestCase):
285 def test_simple_dfr(self):
Javi Merino44745132016-06-30 16:30:38 +0100286 """ILinePlot doesn't barf when plotting DataFrames"""
Javi Merinof2666cb2016-03-09 18:38:44 +0000287 dfr1 = pd.DataFrame([1, 2, 3, 4], columns=["a"])
288 dfr2 = pd.DataFrame([2, 3, 4, 5], columns=["a"])
289
290 trappy.ILinePlot([dfr1, dfr2], column=["a", "a"]).view(test=True)
291
Javi Merino44745132016-06-30 16:30:38 +0100292 with self.assertRaises(ValueError):
293 trappy.ILinePlot([dfr1, dfr2]).view(test=True)
294
Javi Merinofadc28a2016-03-09 18:39:31 +0000295 def test_duplicate_merging(self):
296 dfr1 = pd.DataFrame([1, 2, 3, 4], index=[0., 0., 1., 2.], columns=["a"])
297 dfr2 = pd.DataFrame([2, 3, 4, 5], index=[1., 1., 1., 2.], columns=["a"])
298
299 trappy.ILinePlot([dfr1, dfr2], column=["a", "a"]).view(test=True)
300
Michele Di Giorgio1af636b2016-04-19 18:16:08 +0100301 def test_independent_series_merging(self):
302 """ILinePlot fixes indexes of independent series"""
303 index1 = [0., 1., 2., 3.]
304 s1 = pd.Series([1, 2, 3, 4], index=index1)
305 index2 = [0.5, 1.5, 2.5, 3.5]
306 s2 = pd.Series([2, 3, 4, 5], index=index2)
307
308 dfr = pd.DataFrame([0, 1, 2, 3], columns=["a"])
309 iplot = trappy.ILinePlot(dfr, column=["a"])
Javi Merinoaa4bece2016-04-27 10:57:41 +0100310 s = {"s1": s1, "s2": s2}
Michele Di Giorgio1af636b2016-04-19 18:16:08 +0100311 merged = iplot._fix_indexes(s)
312
313 expected_index = index1 + index2
314 expected_index.sort()
315 self.assertEquals(expected_index, merged.index.tolist())
316
Javi Merinof4261b42016-06-17 17:58:13 +0100317 def test_dygraph_colors(self):
318 """Check that to_dygraph_colors() constructs a valid dygraph colors argument"""
319 from trappy.plotter.ColorMap import to_dygraph_colors
320
321 color_map = [[86, 58, 206]]
322 expected = '["rgb(86, 58, 206)"]'
323 self.assertEquals(to_dygraph_colors(color_map), expected)
324
325 color_map = [[0, 0, 0], [123, 23, 45]]
326 expected = '["rgb(0, 0, 0)", "rgb(123, 23, 45)"]'
327 self.assertEquals(to_dygraph_colors(color_map), expected)
328
Javi Merino3ffa9e22016-01-29 10:19:24 +0100329class TestBarPlot(unittest.TestCase):
330 def setUp(self):
331 self.dfr = pd.DataFrame({"foo": [1, 2, 3],
332 "bar": [2, 3, 1],
333 "baz": [3, 2, 1]})
334
335 def test_barplot_dfr(self):
336 """BarPlot plots dataframes without exploding"""
337 trappy.BarPlot(self.dfr, column=["foo", "bar"]).view(test=True)
338
339 def test_barplot_trace(self):
340 """BarPlot plots traces without exploding"""
341 trace = trappy.BareTrace()
342 trace.add_parsed_event("event", self.dfr)
343
344 trappy.BarPlot(trace, signals=["event:foo", "event:bar"]).view(test=True)