"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 ops_1 = require("../ops.cjs");
const revertibles_1 = require("../revertibles.cjs");
const testUtils_1 = require("./testUtils.cjs");
const snapshot_utils_1 = require("./snapshot.utils.cjs");
describe("MergeTree remove", () => {
    let summary;
    (0, benchmark_1.benchmark)({
        type: benchmark_1.BenchmarkType.Measurement,
        // baseline summary benchmark to compare to other remove tests. such a
        // comparison should give a (rough) sense of overhead caused by summary
        // loading
        title: "baseline summary load",
        category: "remove",
        before: () => {
            const str = new snapshot_utils_1.TestString("id", {});
            for (let i = 0; i < 1000; i++) {
                str.append("a", false);
            }
            str.applyPendingOps();
            summary = str.getSummary();
        },
        benchmarkFnAsync: async () => {
            await (0, snapshot_utils_1.loadSnapshot)(summary);
        },
    });
    (0, benchmark_1.benchmark)({
        type: benchmark_1.BenchmarkType.Measurement,
        title: "remove large range of large tree",
        category: "remove",
        before: () => {
            const str = new snapshot_utils_1.TestString("id", {});
            for (let i = 0; i < 1000; i++) {
                str.append("a", false);
            }
            str.applyPendingOps();
            summary = str.getSummary();
        },
        benchmarkFnAsync: async () => {
            const str = await (0, snapshot_utils_1.loadSnapshot)(summary);
            (0, testUtils_1.markRangeRemoved)({
                mergeTree: str.mergeTree,
                start: 0,
                end: 1000,
                refSeq: 1000,
                clientId: 0,
                seq: 1001,
                opArgs: { op: { type: ops_1.MergeTreeDeltaType.REMOVE } },
                overwrite: false,
            });
        },
    });
    for (const length of [10, 100, 1000]) {
        (0, benchmark_1.benchmark)({
            type: benchmark_1.BenchmarkType.Measurement,
            title: "remove large range of large tree with undo-redo",
            category: "remove",
            before: () => {
                const str = new snapshot_utils_1.TestString("id", {});
                for (let i = 0; i < length / 2; i++) {
                    str.append("a", true);
                    str.appendMarker(true);
                }
                str.applyPendingOps();
                summary = str.getSummary();
            },
            benchmarkFnAsync: async () => {
                const str = await (0, snapshot_utils_1.loadSnapshot)(summary);
                const revertibles = [];
                str.on("delta", (_op, delta) => {
                    (0, revertibles_1.appendToMergeTreeDeltaRevertibles)(delta, revertibles);
                });
                const op = str.removeRangeLocal(0, length - 1);
                str.applyMsg(str.makeOpMessage(op, 
                /* seq */ length + 1, 
                /* refSeq */ length, str.longClientId, 
                /* minSeq */ length));
            },
        });
    }
    (0, benchmark_1.benchmark)({
        type: benchmark_1.BenchmarkType.Measurement,
        title: "remove start of large tree",
        category: "remove",
        before: () => {
            const str = new snapshot_utils_1.TestString("id", {});
            for (let i = 0; i < 1000; i++) {
                str.append("a", false);
            }
            str.applyPendingOps();
            summary = str.getSummary();
        },
        benchmarkFnAsync: async () => {
            const str = await (0, snapshot_utils_1.loadSnapshot)(summary);
            (0, testUtils_1.markRangeRemoved)({
                mergeTree: str.mergeTree,
                start: 0,
                end: 1,
                refSeq: 1000,
                clientId: 0,
                seq: 1001,
                opArgs: { op: { type: ops_1.MergeTreeDeltaType.REMOVE } },
                overwrite: false,
            });
        },
    });
    (0, benchmark_1.benchmark)({
        type: benchmark_1.BenchmarkType.Measurement,
        title: "remove middle of large tree",
        category: "remove",
        before: () => {
            const str = new snapshot_utils_1.TestString("id", {});
            for (let i = 0; i < 1000; i++) {
                str.append("a", false);
            }
            str.applyPendingOps();
            summary = str.getSummary();
        },
        benchmarkFnAsync: async () => {
            const str = await (0, snapshot_utils_1.loadSnapshot)(summary);
            (0, testUtils_1.markRangeRemoved)({
                mergeTree: str.mergeTree,
                start: 499,
                end: 501,
                refSeq: 1000,
                clientId: 0,
                seq: 1001,
                opArgs: { op: { type: ops_1.MergeTreeDeltaType.REMOVE } },
                overwrite: false,
            });
        },
    });
    (0, benchmark_1.benchmark)({
        type: benchmark_1.BenchmarkType.Measurement,
        title: "remove end of large tree",
        category: "remove",
        before: () => {
            const str = new snapshot_utils_1.TestString("id", {});
            for (let i = 0; i < 1000; i++) {
                str.append("a", false);
            }
            str.applyPendingOps();
            summary = str.getSummary();
        },
        benchmarkFnAsync: async () => {
            const str = await (0, snapshot_utils_1.loadSnapshot)(summary);
            (0, testUtils_1.markRangeRemoved)({
                mergeTree: str.mergeTree,
                start: 999,
                end: 1000,
                refSeq: 1000,
                clientId: 0,
                seq: 1001,
                opArgs: { op: { type: ops_1.MergeTreeDeltaType.REMOVE } },
                overwrite: false,
            });
        },
    });
});
//# sourceMappingURL=Removal.perf.spec.cjs.map