class SystemCpuUtilTaskTest(unittest.TestCase):
	"""
	This test case class contains very basic unit tests for
	SystemCpuUtilTask. It should not be considered complete,
	but serve as a starting point for the student implementing
	additional functionality within their Programming the IoT
	environment.
	"""
	
	@classmethod
	def setUpClass(self):
		logging.basicConfig(format = '%(asctime)s:%(module)s:%(levelname)s:%(message)s', level = logging.DEBUG)
		logging.info("Testing SystemCpuUtilTask class...")
		self.cpuUtilTask = SystemCpuUtilTask()
		
	def setUp(self):
		pass

	def tearDown(self):
		pass
	
	@unittest.skip("Ignore for now.")
	def testGenerateTelemetry(self):
		sd = self.cpuUtilTask.generateTelemetry()
		
		self.assertIsNotNone(sd)
		self.assertGreaterEqual(sd.getValue(), 0.0)
		logging.info("CPU utilization SensorData: %s", str(sd))
			
	def testGetTelemetryValue(self):
		val = self.cpuUtilTask.getTelemetryValue()
		
		self.assertGreaterEqual(val, 0.0)
		logging.info("CPU utilization: %s", str(val))
Beispiel #2
0
class SystemPerformanceManager(object):
	"""
	Shell representation of class for student implementation.
	
	"""


	def __init__(self, pollRate: int = 10):
		"""
		Create an instance of SystemCpuUtilTask
		Create an instance of SystemMemUtilTask
		Add a job to the scheduler
		"""
		self.cpuUtilTask = SystemCpuUtilTask()
		self.memUtilTask = SystemMemUtilTask()
		self.scheduler = BackgroundScheduler()
		self.scheduler.add_job(self.handleTelemetry, 'interval', seconds = pollRate)
		
		self.dataMsgListener = None
									

	def handleTelemetry(self):
		"""
		Call getTelemetryValue() method to get the CPU utilization and memory utilization. 
		Logs the values of self.cpuUtilPct and self.memUtilPct
		
		"""
		
		self.cpuUtilPct = self.cpuUtilTask.getTelemetryValue()
		self.memUtilPct = self.memUtilTask.getTelemetryValue()
		
		cpuUtilSd = self.cpuUtilTask.generateTelemetry()
		memUtilSd = self.memUtilTask.generateTelemetry()
		
		##add by miaoyao in order to send SystemPerformanceData to gda instead of cpuUtilSd and memUtilSd
		systemPerformanceData = SystemPerformanceData()
		systemPerformanceData.setCpuUtilization(self.cpuUtilPct)
		systemPerformanceData.setMemoryUtilization(self.memUtilPct)
		
		
		
		
		
		
		##--add call back function to handle system information
		if self.dataMsgListener :
			##self.dataMsgListener.handleSystemPerformanceMessage(cpuUtilSd)
			##self.dataMsgListener.handleSystemPerformanceMessage(memUtilSd)
			self.dataMsgListener.handleSystemPerformanceMessage(systemPerformanceData)
		
		logging.info('CPU utilization is %s percent, and memory utilization is %s percent.', str(self.cpuUtilPct), str(self.memUtilPct))
		
	def setDataMessageListener(self, listener: IDataMessageListener) -> bool:
		if listener:
			self.dataMsgListener = listener
	
	def startManager(self):
		"""
		Start the System Performance Manager. Call scheduler.start() method to start the job scheduler
		
		"""
		logging.info("---> Started SystemPerformanceManager.")
		self.scheduler.start()
		
	def stopManager(self):
		"""
		Stop the System Performance Manager. Call scheduler.shutdown() method to shutdown the job scheduler
		
		"""
		self.scheduler.shutdown()
		logging.info("---> Stopped SystemPerformanceManager.")