ractor_cluster 0.16.3

Distributed cluster environment of Ractor actors
Documentation
// Copyright (c) Sean Lawlor
//
// This source code is licensed under both the MIT license found in the
// LICENSE-MIT file in the root directory of this source tree.

//! Benchmarks the serialization generated by `RactorClusterMessage` for
//! representative single-field and multi-field messages.

use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion, Throughput};
use ractor::Message;
use ractor_cluster::RactorClusterMessage;

const SMALL_PAYLOAD_SIZE: usize = 32;
const LARGE_PAYLOAD_SIZE: usize = 4 * 1024;
const MULTI_FIELD_COUNT: usize = 4;

#[derive(RactorClusterMessage)]
enum SingleFieldMessage {
    Payload(Vec<u8>),
}

#[derive(RactorClusterMessage)]
enum MultiFieldMessage {
    Payload(Vec<u8>, Vec<u8>, Vec<u8>, Vec<u8>),
}

fn single_field_serialization(c: &mut Criterion) {
    let mut group = c.benchmark_group("cluster_message_serialization/single_field");

    for payload_size in [SMALL_PAYLOAD_SIZE, LARGE_PAYLOAD_SIZE] {
        group.throughput(Throughput::Bytes(payload_size as u64));
        group.bench_with_input(
            BenchmarkId::from_parameter(payload_size),
            &payload_size,
            |b, &payload_size| {
                b.iter_batched(
                    || vec![0x5a; payload_size],
                    |payload| {
                        SingleFieldMessage::Payload(payload)
                            .serialize()
                            .expect("benchmark message should serialize")
                    },
                    BatchSize::SmallInput,
                );
            },
        );
    }

    group.finish();
}

fn multi_field_serialization(c: &mut Criterion) {
    let mut group = c.benchmark_group("cluster_message_serialization/multi_field");

    for payload_size in [SMALL_PAYLOAD_SIZE, LARGE_PAYLOAD_SIZE] {
        let field_size = payload_size / MULTI_FIELD_COUNT;
        group.throughput(Throughput::Bytes(payload_size as u64));
        group.bench_with_input(
            BenchmarkId::from_parameter(payload_size),
            &field_size,
            |b, &field_size| {
                b.iter_batched(
                    || {
                        (
                            vec![0x5a; field_size],
                            vec![0x5a; field_size],
                            vec![0x5a; field_size],
                            vec![0x5a; field_size],
                        )
                    },
                    |(field_1, field_2, field_3, field_4)| {
                        MultiFieldMessage::Payload(field_1, field_2, field_3, field_4)
                            .serialize()
                            .expect("benchmark message should serialize")
                    },
                    BatchSize::SmallInput,
                );
            },
        );
    }

    group.finish();
}

criterion_group!(
    message_serialization,
    single_field_serialization,
    multi_field_serialization
);
criterion_main!(message_serialization);