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 = 30):
        self.cpuUtilTask = SystemCpuUtilTask()
        self.memUtilTask = SystemMemUtilTask()
        self.scheduler = BackgroundScheduler()
        self.scheduler.add_job(self.handleTelemetry,
                               'interval',
                               seconds=pollRate)

    def handleTelemetry(self):
        self.cpuUtilPct = self.cpuUtilTask.getTelemetryValue()
        self.memUtilPct = self.memUtilTask.getTelemetryValue()
        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:
        pass

    def startManager(self):
        logging.info("Manager Started...!!!")
        self.scheduler.start()

    def stopManager(self):
        logging.info("Manager Stopped...!!!")
        self.scheduler.shutdown()
Beispiel #3
0
    def testWriteSystemPerformanceDataToCdaDataPath(self):
        logging.info("\n\n----- [SystemPerformanceData to JSON to file] -----")

        dataObj = SystemPerformanceData()
        """
		set CpuUtilization and MemoryUtilization
		"""
        scut = SystemCpuUtilTask()
        smut = SystemMemUtilTask()
        dataObj.setCpuUtilization(scut.getTelemetryValue())
        dataObj.setMemoryUtilization(smut.getTelemetryValue())

        dataStr = self.dataUtil.sensorDataToJson(dataObj)
        fileName = self.cdaDataPath + '/SystemPerformanceData.dat'

        logging.info("Sample SystemPerformanceData JSON (validated): " +
                     str(dataStr))
        logging.info("Writing SystemPerformanceData JSON to CDA data path: " +
                     fileName)

        fileRef = Path(fileName)
        fileRef.write_text(dataStr, encoding='utf-8')
Beispiel #4
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.")
class SystemPerformanceManager(object):
    """
    Shell representation of class for student implementation.

    """
    def __init__(self, pollRate: int = 30):
        """
        Initialization of class: SystemPerformanceManager.
        Setup BackgroundScheduler to pull sensor data with poll rate in background.
        @:parameter pollRate The rate of pulling messages.
        """
        logging.info("Initializing SystemPerformanceManager...")
        # Create tasks
        self.cpuUtilTask = SystemCpuUtilTask()
        self.memUtilTask = SystemMemUtilTask()
        # Initialize sensor data values
        self.cpuUtilPct: float = None
        self.memUtilPct: float = None
        # Initialize dataMsgListener
        self.dataMsgListener: IDataMessageListener = None
        # Initialize BackgroundScheduler
        self.scheduler = BackgroundScheduler()
        # Setup the BackgroundScheduler
        self.scheduler.add_job(self.handleTelemetry,
                               'interval',
                               seconds=pollRate)
        pass

    def handleTelemetry(self):
        """
        Get system performance telemetry and send it to dataMsgListener as SystemPerformanceData
        :return:
        """
        cpuVal = self.cpuUtilTask.getTelemetryValue()
        memVal = self.memUtilTask.getTelemetryValue()
        sysPerfData = SystemPerformanceData()
        sysPerfData.setCpuUtilization(cpuVal)
        sysPerfData.setMemoryUtilization(memVal)
        logging.info(
            'CPU utilization is %s percent, and memory utilization is %s percent.',
            str(cpuVal), str(memVal))
        self.dataMsgListener.handleSystemPerformanceMessage(sysPerfData)
        pass

    def setDataMessageListener(self, listener: IDataMessageListener) -> bool:
        if listener is None:
            return False
        self.dataMsgListener = listener
        return True
        pass

    def startManager(self):
        """
        Start the SystemPerformanceManager.

        """
        logging.info("Starting SystemPerformanceManager...")
        self.scheduler.start()
        logging.info("SystemPerformanceManager started.")
        pass

    def stopManager(self):
        """
        Stop the SystemPerformanceManager.

        """
        logging.info("SystemPerformanceManager stopping...")
        self.scheduler.shutdown()
        logging.info("SystemPerformanceManager stopped.")
        pass
class SystemPerformanceManager(object):
    def __init__(self):
        configUtil = ConfigUtil()

        self.cpuUtilPct = None
        self.memUtilPct = None

        self.pollRate = configUtil.getInteger(
            section=ConfigConst.CONSTRAINED_DEVICE,
            key=ConfigConst.POLL_CYCLES_KEY,
            defaultVal=ConfigConst.DEFAULT_POLL_CYCLES)
        self.locationID = configUtil.getProperty(
            section=ConfigConst.CONSTRAINED_DEVICE,
            key=ConfigConst.DEVICE_LOCATION_ID_KEY,
            defaultVal=ConfigConst.NOT_SET)

        if self.pollRate <= 0:
            self.pollRate = ConfigConst.DEFAULT_POLL_CYCLES

        self.scheduler = BackgroundScheduler()
        self.scheduler.add_job(self.handleTelemetry(),
                               'interval',
                               seconds=self.pollRate)

        self.cpuUtilTask = SystemCpuUtilTask()
        self.memUtilTask = SystemMemUtilTask()

        self.dataMsgListener = None
        # self.memUtilPct = None
        # self.cpuUtilPct = None

        logging.info("Initializing SystemPerformance Manager...")

    def handleTelemetry(self):
        self.cpuUtilPct = self.cpuUtilTask.getTelemetryValue()
        self.memUtilPct = self.memUtilTask.getTelemetryValue()
        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:
        pass

    def startManager(self):
        logging.info("Started SystemPerformanceManger.")

        if not self.scheduler.running:
            self.scheduler.start()
        else:
            logging.warning(
                "SystemPerformanceManager scheduler already started. Ignoring."
            )

    def stopManager(self):
        logging.info("Stopped SystemPerformanceManger.")

        try:
            self.scheduler.shutdown()
        except:
            logging.warning(
                "SystemPerformanceManager scheduler already stopped. Ignoring."
            )