def test_save_events(self): events = [ ExperimentEvent('exp1', 1, 'begin'), ExperimentEvent('exp1', 2, 'start', 'function1'), ExperimentEvent('exp1', 3, 'stop', 'function1'), ] self.db.save_events(events) rows = self.db.db.fetchall('SELECT * FROM events') self.assertEqual(3, len(rows)) self.assertEqual(('exp1', 1.0, 'begin', None), rows[0]) self.assertEqual(('exp1', 2.0, 'start', 'function1'), rows[1]) self.assertEqual(('exp1', 3.0, 'stop', 'function1'), rows[2])
def test_save_and_get_events_for_experiment(self): events = [ ExperimentEvent('exp1', 1, 'begin'), ExperimentEvent('exp1', 2, 'start', 'function1'), ExperimentEvent('exp2', 3, 'stop', 'function1'), ] self.db.save_events(events) stored = self.db.get_events('exp1') self.assertEqual(2, len(stored)) self.assertEqual(ExperimentEvent('exp1', 1.0, 'begin', None), stored[0]) self.assertEqual(ExperimentEvent('exp1', 2.0, 'start', 'function1'), stored[1])
def test_batching_recorder_records_correctly(self): thread = ExperimentEventRecorderThread( BatchingExperimentEventRecorder(self.redis_resource.rds, self.db_resource.db, 'unittest', flush_every=1) ) thread.start() time.sleep(0.1) thread.recorder._record(Event(1., 'start', 'function1')) thread.recorder._record(Event(2., 'stop', 'function1')) thread.recorder._record(Event(3., 'exit')) thread.stop() records = self.db_resource.sql.fetchall('SELECT * FROM `events` WHERE EXP_ID = "unittest"') self.assertEqual(3, len(records)) self.assertEqual(ExperimentEvent('unittest', 1., 'start', 'function1'), records[0]) self.assertEqual(ExperimentEvent('unittest', 2., 'stop', 'function1'), records[1]) self.assertEqual(ExperimentEvent('unittest', 3., 'exit'), records[2])
def get_events(self, exp_id=None) -> List[ExperimentEvent]: fields = self.db.sql_field_list(ExperimentEvent._fields) if exp_id is None: sql = f'SELECT {fields} FROM `events`' entries = self.db.fetchall(sql) else: sql = f'SELECT {fields} FROM `events` WHERE EXP_ID = {self.db.placeholder}' entries = self.db.fetchall(sql, (exp_id, )) return list(map(lambda x: ExperimentEvent(*(tuple(x))), entries))
def event(self, name: str, value: str = None): """ Sends a galileo experiment event. :param name: the event name :param value: the optional event value """ ts = time.time() self.event_reporter.report(ExperimentEvent(ts, name, value)) if value: print(f'%.3f: %s = %s' % (ts, name, value)) else: print(f'%.3f: %s' % (ts, name))
def test_recorder_with_redis(self): thread = ExperimentEventRecorderThread( ExperimentEventRecorder(self.redis_resource.rds, self.db_resource.db, 'unittest') ) thread.start() time.sleep(0.5) self.redis_resource.rds.publish("galileo/events", "1. start function1") self.redis_resource.rds.publish("galileo/events", "2. stop function1") self.redis_resource.rds.publish("galileo/events", "3. exit") time.sleep(0.5) thread.stop() records = self.db_resource.sql.fetchall('SELECT * FROM `events` WHERE EXP_ID = "unittest"') self.assertEqual(3, len(records)) self.assertEqual(ExperimentEvent('unittest', 1., 'start', 'function1'), records[0]) self.assertEqual(ExperimentEvent('unittest', 2., 'stop', 'function1'), records[1]) self.assertEqual(ExperimentEvent('unittest', 3., 'exit'), records[2])
def save_event(self, event: ExperimentEvent): self.db.insert_one('events', event._asdict())
def _record(self, event: Event): self.db.save_event(ExperimentEvent(self.exp_id, *event))
def _record(self, event: Event): self._buffer.append(ExperimentEvent(self.exp_id, *event)) self._increment_and_flush()