<commit_before>/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.lmax.collections.coalescing.ring.buffer.performance;

import com.lmax.collections.coalescing.ring.buffer.CoalescingBuffer;
import com.lmax.collections.coalescing.ring.buffer.CoalescingBufferFactory;
import com.lmax.collections.coalescing.ring.buffer.MarketSnapshot;

import static com.lmax.collections.coalescing.ring.buffer.MarketSnapshot.createMarketSnapshot;

public class PerformanceTest {
    private static final long MILLION = 1000L * 1000L;
    private static final MarketSnapshot POISON_PILL = createMarketSnapshot(-1, -1, -1);
    private static final int NUMBER_OF_INSTRUMENTS = 10;
    private static final int SECONDS = 1000;

    private final CoalescingBuffer<Long, MarketSnapshot> buffer;
	private final long numberOfUpdates;

	public PerformanceTest(CoalescingBuffer<Long, MarketSnapshot> buffer, long numberOfUpdates) {
		this.buffer = buffer;
		this.numberOfUpdates = numberOfUpdates;
	}

	public long run() throws InterruptedException {
		gc();
		System.out.println("testing " + buffer.getClass() + " with " + numberOfUpdates + " updates...");
        StopWatch stopWatch = new StopWatch();

        Producer producer = new Producer(buffer, NUMBER_OF_INSTRUMENTS, numberOfUpdates, POISON_PILL, stopWatch);
		Consumer consumer = new Consumer(buffer, NUMBER_OF_INSTRUMENTS, POISON_PILL, stopWatch);

		producer.start();
		consumer.start();

		consumer.join();

        return computeAndPrintResults(consumer, stopWatch.nanosTaken());
    }

	private void gc() throws InterruptedException {
		for (int i = 0; i < 10; i++) {
			System.gc();
            Thread.sleep(100);
		}
	}

	private long computeAndPrintResults(Consumer consumer, long nanosTaken) {
        for (int i = 0; i < consumer.latestSnapshots.length; i++) {
            System.out.println(consumer.latestSnapshots[i]);
        }

        System.out.println(String.format("\ntime %.1fs", nanosTaken / 1000000000.0));

		double compressionRatio = (1.0 * numberOfUpdates) / consumer.readCounter;
		System.out.println(String.format("compression ratio = %.1f", compressionRatio));

		double megaOpsPerSecond = (1000.0 * numberOfUpdates) / nanosTaken;
		System.out.println(String.format("mops = %.1f", megaOpsPerSecond));

        return Math.round(megaOpsPerSecond);
	}

	public static void main(String[] args) throws Exception {
        long[] results = new long[3];
        int runNumber = 1;

        do {
            long result = run(runNumber++, 1000 * MILLION);
            update(results, result);
            Thread.sleep(5 * SECONDS);

        } while (!areAllResultsTheSame(results));
	}

    private static long run(int runNumber, long numberOfUpdates) throws InterruptedException {
        CoalescingBuffer<Long, MarketSnapshot> buffer = CoalescingBufferFactory.create(1 << 20);
        PerformanceTest test = new PerformanceTest(buffer, numberOfUpdates);

        System.out.println("\n======================================= run " + runNumber + " =======================================\n");
        return test.run();
    }<commit_msg>add performance result<commit_after>/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.lmax.collections.coalescing.ring.buffer.performance;

import com.lmax.collections.coalescing.ring.buffer.CoalescingBuffer;
import com.lmax.collections.coalescing.ring.buffer.CoalescingBufferFactory;
import com.lmax.collections.coalescing.ring.buffer.MarketSnapshot;

import static com.lmax.collections.coalescing.ring.buffer.MarketSnapshot.createMarketSnapshot;

public class PerformanceTest {
    private static final long MILLION = 1000L * 1000L;
    private static final MarketSnapshot POISON_PILL = createMarketSnapshot(-1, -1, -1);
    private static final int NUMBER_OF_INSTRUMENTS = 10;
    private static final int SECONDS = 1000;

    private final CoalescingBuffer<Long, MarketSnapshot> buffer;
	private final long numberOfUpdates;

	public PerformanceTest(CoalescingBuffer<Long, MarketSnapshot> buffer, long numberOfUpdates) {
		this.buffer = buffer;
		this.numberOfUpdates = numberOfUpdates;
	}

	public long run() throws InterruptedException {
		gc();
		System.out.println("testing " + buffer.getClass() + " with " + numberOfUpdates + " updates...");
        StopWatch stopWatch = new StopWatch();

        Producer producer = new Producer(buffer, NUMBER_OF_INSTRUMENTS, numberOfUpdates, POISON_PILL, stopWatch);
		Consumer consumer = new Consumer(buffer, NUMBER_OF_INSTRUMENTS, POISON_PILL, stopWatch);

		producer.start();
		consumer.start();

		consumer.join();

        return computeAndPrintResults(consumer, stopWatch.nanosTaken());
    }

	private void gc() throws InterruptedException {
		for (int i = 0; i < 10; i++) {
			System.gc();
            Thread.sleep(100);
		}
	}

	private long computeAndPrintResults(Consumer consumer, long nanosTaken) {
        for (int i = 0; i < consumer.latestSnapshots.length; i++) {
            System.out.println(consumer.latestSnapshots[i]);
        }

        System.out.println(String.format("\ntime %.1fs", nanosTaken / 1000000000.0));

		double compressionRatio = (1.0 * numberOfUpdates) / consumer.readCounter;
		System.out.println(String.format("compression ratio = %.1f", compressionRatio));

		double megaOpsPerSecond = (1000.0 * numberOfUpdates) / nanosTaken;
		System.out.println(String.format("mops = %.2f", megaOpsPerSecond));

        return Math.round(megaOpsPerSecond);
	}

	public static void main(String[] args) throws Exception {
        long[] results = createResults(3);
        int runNumber = 1;

        do {
            long result = run(runNumber++, 5 * MILLION);
            update(results, result);
            Thread.sleep(5 * SECONDS);

        } while (!areAllResultsTheSame(results));
	}

    private static long[] createResults(int size) {
        long[] results = new long[size];

        for (int i = 0; i < results.length; i++) {
            results[i] = -1;
        }

        return results;
    }

    private static long run(int runNumber, long numberOfUpdates) throws InterruptedException {
        CoalescingBuffer<Long, MarketSnapshot> buffer = CoalescingBufferFactory.create(1 << 20);
        PerformanceTest test = new PerformanceTest(buffer, numberOfUpdates);

        System.out.println("\n======================================= run " + runNumber + " =======================================\n");
        return test.run();
    }
{'fraction_non_alphanumeric': 0.1232729711602951, 'fraction_numerical': 0.015694164989939637, 'mean_word_length': 3.8072211476466795, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2283171', 'n_tokens_mistral': 2271, 'n_tokens_neox': 2074, 'n_words': 575}