"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
const benchmark_1 = require("@fluid-tools/benchmark");
const attributionCollection_js_1 = require("../attributionCollection.js");
const textSegment_js_1 = require("../textSegment.js");
function getCollectionSizes(ctor, baseSuiteType) {
    const singleKeyCollection = new ctor(5, { type: "op", seq: 42 });
    const tenKeyCollection = new ctor(2, { type: "op", seq: 0 });
    for (let i = 1; i < 10; i++) {
        tenKeyCollection.append(new ctor(3 * i, { type: "op", seq: i }));
    }
    const maxSizeCollection = new ctor(1, { type: "op", seq: 0 });
    for (let i = 1; i < textSegment_js_1.TextSegmentGranularity; i++) {
        maxSizeCollection.append(new ctor(1, { type: "op", seq: i }));
    }
    return [
        { name: "one key", collection: singleKeyCollection, type: benchmark_1.BenchmarkType.Diagnostic },
        { name: "ten keys", collection: tenKeyCollection, type: baseSuiteType },
        { name: "maximum keys", collection: maxSizeCollection, type: benchmark_1.BenchmarkType.Diagnostic },
    ];
}
function runAttributionCollectionSuite(ctor, suiteBaseType) {
    const collectionTestCases = getCollectionSizes(ctor, suiteBaseType);
    for (const { name, collection, type } of collectionTestCases) {
        describe(`using a collection with ${name}`, () => {
            const { length } = collection;
            (0, benchmark_1.benchmark)({
                title: "getAtOffset at the start",
                benchmarkFn: () => collection.getAtOffset(0),
                type,
            });
            (0, benchmark_1.benchmark)({
                title: "getAtOffset at the end",
                benchmarkFn: () => collection.getAtOffset(length - 1),
                type,
            });
            (0, benchmark_1.benchmark)({
                title: "getAtOffset in the middle",
                benchmarkFn: () => collection.getAtOffset(length / 2),
                type: benchmark_1.BenchmarkType.Diagnostic,
            });
            (0, benchmark_1.benchmark)({
                title: "getKeysInOffsetRange from start to end",
                benchmarkFn: () => collection.getKeysInOffsetRange(0),
                type,
            });
            (0, benchmark_1.benchmark)({
                title: "getKeysInOffsetRange from start to mid",
                benchmarkFn: () => collection.getKeysInOffsetRange(0, length / 2),
                type,
            });
            (0, benchmark_1.benchmark)({
                title: "getKeysInOffsetRange from mid to end",
                benchmarkFn: () => collection.getKeysInOffsetRange(length / 2, length - 1),
                type,
            });
            (0, benchmark_1.benchmark)({
                title: "getAll",
                benchmarkFn: () => collection.getAll(),
                type,
            });
            (0, benchmark_1.benchmark)({
                title: "clone",
                benchmarkFn: () => collection.clone(),
                type,
            });
            (0, benchmark_1.benchmark)({
                title: "split + append in the middle",
                benchmarkFn: () => {
                    const split = collection.splitAt(length / 2);
                    collection.append(split);
                },
                type,
            });
        });
    }
    (0, benchmark_1.benchmark)({
        title: "construction",
        benchmarkFn: () => new ctor(42, { type: "op", seq: 5 }),
        type: suiteBaseType,
    });
    const segmentsToSerialize = collectionTestCases.map(({ collection }) => ({
        attribution: collection,
        cachedLength: collection.length,
    }));
    (0, benchmark_1.benchmark)({
        title: "serializing",
        benchmarkFn: () => ctor.serializeAttributionCollections(segmentsToSerialize),
        type: suiteBaseType,
    });
    const summary = ctor.serializeAttributionCollections(segmentsToSerialize);
    const segments = Array.from({ length: 9 }, () => ({
        cachedLength: Math.floor(summary.length / 10),
    }));
    segments.push({
        cachedLength: summary.length - 9 * Math.floor(summary.length / 10),
    });
    (0, benchmark_1.benchmark)({
        title: "deserialize into 10 segments",
        benchmarkFn: () => {
            ctor.populateAttributionCollections(segments, summary);
        },
        type: suiteBaseType,
    });
}
describe("IAttributionCollection perf", () => {
    // There was a RedBlack tree based implementation for the collection entries, but the linear array based one won due to constant
    // factors/memory characteristics, so just kept the array based one.
    describe("list-based implementation", () => {
        runAttributionCollectionSuite(attributionCollection_js_1.AttributionCollection, benchmark_1.BenchmarkType.Measurement);
    });
});
//# sourceMappingURL=attributionCollection.perf.spec.js.map