diff mbox series

[3/3] trace-cruncher: Add tests for the khist class

Message ID 20220106132156.168538-3-y.karadz@gmail.com (mailing list archive)
State Accepted
Headers show
Series [1/3] trace-cruncher: Add khist class to ft_utiles | expand

Commit Message

Yordan Karadzhov Jan. 6, 2022, 1:21 p.m. UTC
Testing the high-level Python APIs for kernel histograms.

Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
 .../tests/1_unit/test_01_ftracepy_unit.py     | 91 +++++++++++++++++++
 1 file changed, 91 insertions(+)
diff mbox series

Patch

diff --git a/tracecruncher/tests/1_unit/test_01_ftracepy_unit.py b/tracecruncher/tests/1_unit/test_01_ftracepy_unit.py
index 7e5b20e..87b2be8 100644
--- a/tracecruncher/tests/1_unit/test_01_ftracepy_unit.py
+++ b/tracecruncher/tests/1_unit/test_01_ftracepy_unit.py
@@ -10,6 +10,7 @@  import os
 import sys
 import unittest
 import tracecruncher.ftracepy as ft
+import tracecruncher.ft_utils as tc
 
 instance_name = 'test_instance1'
 another_instance_name = 'test_instance2'
@@ -600,5 +601,95 @@  class HistTestCase(unittest.TestCase):
         hist.close(inst)
 
 
+hist_name = 'h2d'
+evt_sys = 'kmem'
+evt_name = 'kmalloc'
+
+axes={'call_site': 'sym',
+      'bytes_req': 'n'}
+weights=['bytes_alloc']
+sort_keys=['bytes_req', 'bytes_alloc']
+sort_dir={'bytes_req': 'desc'}
+
+tgr_file = '{0}/instances/{1}_inst/events/{2}/{3}/trigger'.format(ft.dir(), hist_name, evt_sys, evt_name)
+
+class HistOopTestCase(unittest.TestCase):
+    def test_hist_create(self):
+        evt = tc.event(evt_sys, evt_name)
+        hist = tc.create_khist(name=hist_name, event=evt, axes=axes, weights=weights,
+                               sort_keys=sort_keys, sort_dir=sort_dir)
+        f = open(tgr_file)
+        h_buff = f.read()
+        self.assertTrue('hist:name=h2d' in h_buff)
+        self.assertTrue(':keys=call_site.sym,bytes_req' in h_buff)
+        self.assertTrue(':vals=hitcount,bytes_alloc' in h_buff)
+        self.assertTrue(':sort=bytes_req.descending,bytes_alloc' in str(h_buff))
+
+        f.close()
+
+    def test_hist_ctrl(self):
+        evt = tc.event(evt_sys, evt_name)
+        hist = tc.create_khist(name=hist_name, event=evt, axes=axes, weights=weights,
+                               sort_keys=sort_keys, sort_dir=sort_dir)
+        f = open(tgr_file)
+        h_buff = f.read()
+        self.assertTrue('[paused]' in h_buff)
+
+        hist.start()
+        f.seek(0)
+        h_buff = f.read()
+        self.assertTrue('[active]' in h_buff)
+
+        hist.stop()
+        f.seek(0)
+        h_buff = f.read()
+        self.assertTrue('[paused]' in h_buff)
+
+        hist.resume()
+        f.seek(0)
+        h_buff = f.read()
+        self.assertTrue('[active]' in h_buff)
+
+        h_buff = hist.data()
+        self.assertTrue('Totals:' in h_buff)
+
+        hist.stop()
+        hist.clear()
+        f.seek(0)
+        h_buff = f.read()
+        self.assertTrue('[paused]' in h_buff)
+        h_buff = hist.data()
+        self.assertTrue('Hits: 0' in h_buff)
+        self.assertTrue('Entries: 0' in h_buff)
+        self.assertTrue('Dropped: 0' in h_buff)
+
+        f.close()
+
+    def test_1_detach(self):
+        evt = tc.event(evt_sys, evt_name)
+        hist = tc.create_khist(name=hist_name, event=evt, axes=axes, weights=weights,
+                               sort_keys=sort_keys, sort_dir=sort_dir)
+        self.assertTrue(hist.is_attached())
+        hist.detach()
+        self.assertFalse(hist.is_attached())
+
+    def test_2_attach(self):
+        evt = tc.event(evt_sys, evt_name)
+        hist = tc.find_khist(name=hist_name, event=evt, axes=axes, weights=weights,
+                             sort_keys=sort_keys, sort_dir=sort_dir)
+        self.assertFalse(hist.is_attached())
+        hist.attach()
+        self.assertTrue(hist.is_attached())
+
+    def test_hist_err(self):
+        evt = tc.event(evt_sys, evt_name)
+
+        err = 'Failed to find histogram'
+        with self.assertRaises(Exception) as context:
+            hist = tc.find_khist(name=hist_name, event=evt, axes=axes, weights=weights,
+                                 sort_keys=sort_keys, sort_dir=sort_dir)
+        self.assertTrue(err in str(context.exception))
+
+
 if __name__ == '__main__':
     unittest.main()