def test_basic_reporter(self): test1 = EventCounter("test1", events=["test_event"]) register_counter(test1) v = SimpleValueReporter() register_reporter(v) start_auto_reporting(0.01) try: test1.report_event("test_event", "value", 2) sleep(0.1) self.assertEqual(v.values_wo_metadata, {"test1": 2}) test1.report_event("test_event", "value", 1) sleep(0.05) self.assertEqual(v.values_wo_metadata, {"test1": 3}) stop_auto_reporting() test1.report_event("test_event", "value", 1) sleep(0.05) self.assertEqual(v.values_wo_metadata, {"test1": 3}) finally: unregister_counter(counter=test1) unregister_reporter(v)
# self.request is the TCP socket connected to the client self.data = self.request.recv(1024).strip() print "%s wrote:" % self.client_address[0] print self.data # measure the average length of data report_value("requests_data_len",len(self.data)) # just send back the same data, but upper-cased self.request.send(self.data.upper()) if __name__ == "__main__": HOST, PORT = "localhost", 9999 JSONFile = "/tmp/server.counters.json" data_len_counter = counters.TotalCounter("requests_data_len") # create the counter register_counter(data_len_counter) # register it, so it will start processing events reporter = reporters.JSONFileReporter(output_file=JSONFile) register_reporter(reporter) start_auto_reporting() # Create the server, binding to localhost on port 9999 server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler) # Activate the server; this will keep running until you # interrupt the program with Ctrl-C server.serve_forever()
def __init__(self, RP=None): """ :param output_file: a file name to which the reports will be written. """ super(BaseReporter, self).__init__() # start_new_thread(self.serv_results, ()) def serv_results(self): listener = Listener("/tmp/rpssock") conn = listener.accept() conn.send_bytes(self.RPS) conn.close() def output_values(self, counter_values): #not sure but probably this method will be called from PyCounter's "leader" collector process if counter_values['requests_frequency'] and counter_values[ 'requests_frequency'] > 0: self.RPS = str(counter_values['requests_frequency']) self.serv_results() rps_counter = counters.FrequencyCounter( "requests_frequency", window_size=2) # not working when set to 1 register_counter(rps_counter) reporter = SocketReporter() # json_reporter = JSONFileReporter("/tmp/pyc.json") # register_reporter(json_reporter) register_reporter(reporter) start_auto_reporting(seconds=1) configure_multi_process_collection()
Serves reports using multiprocessing.Listener (to StatsGenerator) """ def __init__(self, RP=None): """ :param output_file: a file name to which the reports will be written. """ super(BaseReporter, self).__init__() # start_new_thread(self.serv_results, ()) def serv_results(self): listener = Listener("/tmp/rpssock") conn = listener.accept() conn.send_bytes(self.RPS) conn.close() def output_values(self, counter_values): #not sure but probably this method will be called from PyCounter's "leader" collector process if counter_values['requests_frequency'] and counter_values['requests_frequency'] > 0: self.RPS = str(counter_values['requests_frequency']) self.serv_results() rps_counter = counters.FrequencyCounter("requests_frequency", window_size=2) # not working when set to 1 register_counter(rps_counter) reporter = SocketReporter() # json_reporter = JSONFileReporter("/tmp/pyc.json") # register_reporter(json_reporter) register_reporter(reporter) start_auto_reporting(seconds=1) configure_multi_process_collection()