TestML.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from utils import transport
  2. from utils.ml import ML, AnomalyDetection
  3. import unittest
  4. import json
  5. import os
  6. path = os.environ['MONITOR_CONFIG_PATH']
  7. f = open(path)
  8. CONFIG = json.loads( f.read())
  9. f.close()
  10. factory = transport.DataSourceFactory()
  11. class TestML(unittest.TestCase):
  12. def setUp(self):
  13. ref = CONFIG['store']['class']['read']
  14. p = CONFIG['store']['args']
  15. p['qid'] = ['apps']
  16. self.greader = factory.instance(type=ref,args=p)
  17. def test_has_date(self):
  18. r = self.greader.read()
  19. self.assertTrue(r)
  20. def test_Filter(self):
  21. r = self.greader.read()
  22. r = r['apps']
  23. #
  24. # To make this test case extensible we need to pull apps from the configuration
  25. #
  26. app = CONFIG['monitor']['processes']['config']['apps'][0]
  27. x = ML.Filter('label',app,r)
  28. for row in x:
  29. self.assertTrue(row['label'] == app)
  30. def test_Extract(self):
  31. r = self.greader.read()
  32. r = r['apps']
  33. app = CONFIG['monitor']['processes']['config']['apps'][0]
  34. x = ML.Filter('label',app,r)
  35. x_ = ML.Extract(['cpu_usage','memory_usage'], x)
  36. self.assertTrue (len (x) == len(x_))
  37. pass
  38. def test_Learn(self):
  39. ref = CONFIG['store']['class']['read']
  40. p = CONFIG['store']['args']
  41. greader = factory.instance(type=ref,args=p)
  42. data = greader.read()
  43. data = data['apps']
  44. app = CONFIG['monitor']['processes']['config']['apps'][1]
  45. lhandler = AnomalyDetection()
  46. features = CONFIG['learner']['anomalies']['features']
  47. label = CONFIG['learner']['anomalies']['label']
  48. x = lhandler.learn(data,'label',app,features,label)
  49. print x
  50. if __name__ == '__main__' :
  51. unittest.main()