import base64


def invoke_lambda_and_get_duration(lambda_client, payload, function_name):
    """
    Invokes Lambda and return the duration.
    :param lambda_client: Lambda client.
    :param payload: payload to send.
    :param function_name: function name.
    :return: duration.
    """
    response = lambda_client.invoke(
        FunctionName=function_name,
        InvocationType='RequestResponse',
        LogType='Tail',
        Payload=payload,
    )

    # Extract duration from Lambda log
    lambda_log = base64.b64decode(response['LogResult']).decode('utf-8')
    report_data = \
        [line for line in lambda_log.split('\n')
         if line.startswith('REPORT')
        ][0]
    duration = \
        [col for col in report_data.split('\t')
         if col.startswith('Duration')
         ][0]
    duration = float(duration.split()[1])
    return duration