<commit_before>//Prevayler(TM) - The Free-Software Prevalence Layer.
//Copyright (C) 2001-2003 <NAME>
//This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

package org.prevayler.implementation;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import junit.framework.AssertionFailedError;

import org.prevayler.Prevayler;
import org.prevayler.PrevaylerFactory;
import org.prevayler.foundation.FileIOTest;
import org.prevayler.foundation.FileManager;

public class PersistenceTest extends FileIOTest {

	private Prevayler<AppendingSystem> _prevayler;
	private String _prevalenceBase;
    
    public void tearDown() throws Exception {
        if (_prevayler != null) {
            _prevayler.close();
        }
        super.tearDown();
    }

    public void testPersistenceWithDiskSync() throws Exception {
        doTestPersistence(true);
    }

    public void testPersistenceWithoutDiskSync() throws Exception {
        doTestPersistence(false);
    }

    public void doTestPersistence(boolean journalDiskSync) throws Exception {
		newPrevalenceBase();

		crashRecover(journalDiskSync); //There is nothing to recover at first. A new system will be created.
		crashRecover(journalDiskSync);
		append("a","a");
		append("b","ab");
		verify("ab");

		crashRecover(journalDiskSync);
		verify("ab");

		append("c","abc");
		append("d","abcd");
		snapshot("0000000000000000004.snapshot");
		snapshot("0000000000000000004.snapshot");
		verify("abcd");

		crashRecover(journalDiskSync);
		snapshot("0000000000000000004.snapshot");
		append("e","abcde");
		snapshot("0000000000000000005.snapshot");
		append("f","abcdef");
		append("g","abcdefg");
		verify("abcdefg");

		crashRecover(journalDiskSync);
		append("h","abcdefgh");
		verify("abcdefgh");

		snapshot("0000000000000000008.snapshot");
		_prevayler.close();
		File lastSnapshot =   new File(_prevalenceBase, "0000000000000000008.snapshot");
		File lastTransactionLog =   new File(_prevalenceBase, "0000000000000000008.journal");
		newPrevalenceBase();
		FileManager.produceDirectory(_prevalenceBase);
		lastSnapshot.renameTo(new File(_prevalenceBase, "0000000000000000008.snapshot"));  //Moving the file.
		lastTransactionLog.renameTo(new File(_prevalenceBase, "0000000000000000008.journal"));

		crashRecover(journalDiskSync);
		append("i","abcdefghi");
		append("j","abcdefghij");
		crashRecover(journalDiskSync);
		append("k","abcdefghijk");
		append("l","abcdefghijkl");
		crashRecover(journalDiskSync);
		append("m","abcdefghijklm");
		append("n","abcdefghijklmn");
		crashRecover(journalDiskSync);
		verify("abcdefghijklmn");
	}

    public void testDiskSyncPerformance() throws Exception {
        long false1 = doDiskSyncPerformanceRun(false);
        long true1 = doDiskSyncPerformanceRun(true);
        long false2 = doDiskSyncPerformanceRun(false);
        long true2 = doDiskSyncPerformanceRun(true);
        long bestTrue = Math.min(true1, true2);
        long worstFalse = Math.max(false1, false2);
        assertTrue(bestTrue + " should be worse than " + worstFalse, bestTrue > worstFalse);
    }

    private long doDiskSyncPerformanceRun(boolean journalDiskSync) throws Exception {
        newPrevalenceBase();
        crashRecover(journalDiskSync);
        append("a", "a");
        long start = System.currentTimeMillis();
        String expected = "a";
        for (char c = 'b'; c <= 'z'; c++) {
            expected += c;
            append(String.valueOf(c), expected);
        }
        long end = System.currentTimeMillis();
        crashRecover(journalDiskSync);
        verify(expected);
        return end - start;
    }
<commit_msg>For some unknown reason #testDiskSyncPerformance fails when executed from within IntelliJ IDEA. Added a todo-note of that in the class and appended it as a message to the the assert fail message.<commit_after>//Prevayler(TM) - The Free-Software Prevalence Layer.
//Copyright (C) 2001-2003 <NAME>
//This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

package org.prevayler.implementation;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import junit.framework.AssertionFailedError;

import org.prevayler.Prevayler;
import org.prevayler.PrevaylerFactory;
import org.prevayler.foundation.FileIOTest;
import org.prevayler.foundation.FileManager;

public class PersistenceTest extends FileIOTest {

	private Prevayler<AppendingSystem> _prevayler;
	private String _prevalenceBase;
    
    public void tearDown() throws Exception {
        if (_prevayler != null) {
            _prevayler.close();
        }
        super.tearDown();
    }

    public void testPersistenceWithDiskSync() throws Exception {
        doTestPersistence(true);
    }

    public void testPersistenceWithoutDiskSync() throws Exception {
        doTestPersistence(false);
    }

    public void doTestPersistence(boolean journalDiskSync) throws Exception {
		newPrevalenceBase();

		crashRecover(journalDiskSync); //There is nothing to recover at first. A new system will be created.
		crashRecover(journalDiskSync);
		append("a","a");
		append("b","ab");
		verify("ab");

		crashRecover(journalDiskSync);
		verify("ab");

		append("c","abc");
		append("d","abcd");
		snapshot("0000000000000000004.snapshot");
		snapshot("0000000000000000004.snapshot");
		verify("abcd");

		crashRecover(journalDiskSync);
		snapshot("0000000000000000004.snapshot");
		append("e","abcde");
		snapshot("0000000000000000005.snapshot");
		append("f","abcdef");
		append("g","abcdefg");
		verify("abcdefg");

		crashRecover(journalDiskSync);
		append("h","abcdefgh");
		verify("abcdefgh");

		snapshot("0000000000000000008.snapshot");
		_prevayler.close();
		File lastSnapshot =   new File(_prevalenceBase, "0000000000000000008.snapshot");
		File lastTransactionLog =   new File(_prevalenceBase, "0000000000000000008.journal");
		newPrevalenceBase();
		FileManager.produceDirectory(_prevalenceBase);
		lastSnapshot.renameTo(new File(_prevalenceBase, "0000000000000000008.snapshot"));  //Moving the file.
		lastTransactionLog.renameTo(new File(_prevalenceBase, "0000000000000000008.journal"));

		crashRecover(journalDiskSync);
		append("i","abcdefghi");
		append("j","abcdefghij");
		crashRecover(journalDiskSync);
		append("k","abcdefghijk");
		append("l","abcdefghijkl");
		crashRecover(journalDiskSync);
		append("m","abcdefghijklm");
		append("n","abcdefghijklmn");
		crashRecover(journalDiskSync);
		verify("abcdefghijklmn");
	}

    public void testDiskSyncPerformance() throws Exception {
        long false1 = doDiskSyncPerformanceRun(false);
        long true1 = doDiskSyncPerformanceRun(true);
        long false2 = doDiskSyncPerformanceRun(false);
        long true2 = doDiskSyncPerformanceRun(true);
        long bestTrue = Math.min(true1, true2);
        long worstFalse = Math.max(false1, false2);
        // todo: This fails when executing the test from within IntelliJ IDEA
        // todo: but not when executed from command line.
        assertTrue(bestTrue + " should be worse than " + worstFalse + " (This test has been seen failing when executed from within IntelliJ IDEA, but should never fail when executed from command line using 'mvn install'.)", bestTrue > worstFalse);
    }

    private long doDiskSyncPerformanceRun(boolean journalDiskSync) throws Exception {

        newPrevalenceBase();
        crashRecover(journalDiskSync);
        append("a", "a");
        long start = System.nanoTime();
        String expected = "a";

        for (char c = 'b'; c <= 'z'; c++) {
            expected += c;
            append(String.valueOf(c), expected);
        }

        long end = System.nanoTime();
        crashRecover(journalDiskSync);
        verify(expected);

        return end - start;
    }

{'fraction_non_alphanumeric': 0.11583487369069624, 'fraction_numerical': 0.046087492298213184, 'mean_word_length': 4.256476683937824, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 18, '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': '6890347', 'n_tokens_mistral': 2765, 'n_tokens_neox': 2323, 'n_words': 548}