import time


def test_engine(engine):
    """ Tests SQLAlchemy engine and returns response time """
    if not engine:
        return {'status': 'ERROR', 'error': 'No engine defined'}
    try:
        start = time.time()
        connection = engine.connect()
        if not connection.closed:
            connection.close()
        elapsed = '{:.3f}'.format(time.time() - start)
        return {
            'engine': str(engine),
            'label': getattr(engine, 'label', '<unknown>'),
            'status': 'OK',
            'time': elapsed
        }
    except Exception as err:
        return {'status': 'ERROR', 'error': str(err)}