def calc_segmentstores_for_latency(write_events_per_second, event_size, expected_max_latency, performance_profile):
    """
    Given the Segment Store throughput vs latency trade-off provided in the performance profile, we derive the necessary
    number of instances to effectively absorb a given write workload and guarantee an expected latency (e.g., 95th perc.).
    :param write_events_per_second: Number of write events per second in the target workload.
    :param event_size: Typical event size.
    :param expected_max_latency: Expected max latency of events in the 95th percentile.
    :param performance_profile: Class containing the performance of the Segment Store in the target environment.
    :return: Number of Segment Store instances to absorb the target workload respecting the latency requirements.
    """
    for (es, throughput_vs_latency) in performance_profile.segmentstore_latency_profile:
        # We analyzed some pre-defined event sizes.
        if event_size == es:
            for (throughput, latency) in sorted(throughput_vs_latency, reverse=True):
                # Once we reach the desired max latency value, calculate the number of instances.
                if latency < expected_max_latency:
                    return int(write_events_per_second / throughput + 1)
    raise Exception("Could not meet expected latency for Segment Store operations.")