<commit_before>/*
 * Copyright 2008 Network Engine for Objects in Lund AB [neotechnology.com]
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program 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.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.neo4j.graphalgo.benchmark;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.neo4j.api.core.EmbeddedNeo;
import org.neo4j.api.core.NeoService;

/**
 * Base class for benchmarks done on a NeoSevice. The method neoAlgoBenchMarkRun
 * will call the abstract methods setUpGlobal and tearDownGlobal and in between
 * them call the three methods setUp, runBenchMark and tearDown a number of
 * times defined by the numberOfRuns variable. An average time is calculated and
 * saved for later inspection.
 * @author <NAME>
 */
abstract public class NeoAlgoBenchmark
{
    protected NeoService neo;
    protected int numberOfWarmupRuns = 3;
    protected int numberOfRuns = 10;
    protected List<Object> internalIds;
    protected Object currentInternalId;
    private Map<Object,Double> results = new HashMap<Object,Double>();

    /**
     * Method called for setting up the global benchmark environment, such as
     * starting a NeoService and maybe generating graphs.
     * @param neoStoreDir
     */
    protected void setUpGlobal( String neoStoreDir )
    {
        neo = new EmbeddedNeo( neoStoreDir );
    }

    protected void setUpGlobal()
    {
        setUpGlobal( "var/benchmarkdata" );
    }

    protected void tearDownGlobal()
    {
        neo.shutdown();
    }

    abstract public String getTestId();

    /**
     * Method called before every single run of the runBenchMark method.
     */
    abstract protected void setUp();

    /**
     * Method called after every single run of the runBenchMark method.
     */
    abstract protected void tearDown();

    /**
     * Method called to run the benchmark once. This is the called that is
     * timed.
     */
    abstract protected void runBenchMark();

    public void neoAlgoBenchMarkRun()
    {
        if ( internalIds == null )
        {
            neoAlgoBenchMarkRunInternal();
        }
        else
        {
            for ( Object id : internalIds )
            {
                currentInternalId = id;
                neoAlgoBenchMarkRunInternal();
            }
            System.out.println( " --- Results ---" );
            for ( Object object : internalIds )
            {
                System.out
                    .println( object + ": " + results.get( object ) + "s" );
            }
        }
    }

    private void neoAlgoBenchMarkRunInternal()
    {
        System.out.println( "Starting benchmark " + getTestId() );
        setUpGlobal();
        // Warmup
        System.out.println( "Running warmup" );
        for ( int i = 0; i < numberOfWarmupRuns; ++i )
        {
            runBenchMark();
        }
        System.out.println( "Running benchmarks" );
        long start, end;
        long averageTime = 0;
        for ( int i = 0; i < numberOfRuns; ++i )
        {
            setUp();
            start = System.currentTimeMillis();
            runBenchMark();
            end = System.currentTimeMillis();
            tearDown();
            averageTime += end - start;
            System.out.println( "Time: " + ((end - start) / 1000.0) + "s" );
        }
        System.out.print( "Average time" );
        if ( currentInternalId != null )
        {
            System.out.print( " for " + currentInternalId );
            results.put( currentInternalId,
                (averageTime / numberOfRuns / 1000.0) );
        }
        System.out.println( ": " + (averageTime / numberOfRuns / 1000.0) + "s" );
        tearDownGlobal();
        if ( !saveStatistic( averageTime / numberOfRuns, true ) )
        {
            System.out.println( "Failed to save statistics" );
        }
    }

    /**
     * This method saves the measured time, by appending it to a normal text
     * file whose name is decided by getTestId().
     * @param milliSeconds
     * @param print
     *            If true, the time will be printed, together with a number of
     *            previous results.
     * @return False if something goes wrong, like no write permission to the
     *         file etc.
     */
    protected boolean saveStatistic( long milliSeconds, boolean print )
    {
        String fileName = "var/benchmarkstatistics/" + getTestId();
        String data = "";
        // Make directory
        new File( "var/benchmarkstatistics/" ).mkdirs();
        // Read
        try
        {
            BufferedReader reader = new BufferedReader( new FileReader(
                fileName ) );
            data = reader.readLine();
            reader.close();
        }
        catch ( Exception exception )
        {
        }<commit_msg>Stores test data under target/<commit_after>/*
 * Copyright 2008 Network Engine for Objects in Lund AB [neotechnology.com]
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program 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.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.neo4j.graphalgo.benchmark;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.neo4j.api.core.EmbeddedNeo;
import org.neo4j.api.core.NeoService;

/**
 * Base class for benchmarks done on a NeoSevice. The method neoAlgoBenchMarkRun
 * will call the abstract methods setUpGlobal and tearDownGlobal and in between
 * them call the three methods setUp, runBenchMark and tearDown a number of
 * times defined by the numberOfRuns variable. An average time is calculated and
 * saved for later inspection.
 * @author <NAME>
 */
abstract public class NeoAlgoBenchmark
{
    protected NeoService neo;
    protected int numberOfWarmupRuns = 3;
    protected int numberOfRuns = 10;
    protected List<Object> internalIds;
    protected Object currentInternalId;
    private Map<Object,Double> results = new HashMap<Object,Double>();

    /**
     * Method called for setting up the global benchmark environment, such as
     * starting a NeoService and maybe generating graphs.
     * @param neoStoreDir
     */
    protected void setUpGlobal( String neoStoreDir )
    {
        neo = new EmbeddedNeo( neoStoreDir );
    }

    protected void setUpGlobal()
    {
        setUpGlobal( "target/benchmarkdata" );
    }

    protected void tearDownGlobal()
    {
        neo.shutdown();
    }

    abstract public String getTestId();

    /**
     * Method called before every single run of the runBenchMark method.
     */
    abstract protected void setUp();

    /**
     * Method called after every single run of the runBenchMark method.
     */
    abstract protected void tearDown();

    /**
     * Method called to run the benchmark once. This is the called that is
     * timed.
     */
    abstract protected void runBenchMark();

    public void neoAlgoBenchMarkRun()
    {
        if ( internalIds == null )
        {
            neoAlgoBenchMarkRunInternal();
        }
        else
        {
            for ( Object id : internalIds )
            {
                currentInternalId = id;
                neoAlgoBenchMarkRunInternal();
            }
            System.out.println( " --- Results ---" );
            for ( Object object : internalIds )
            {
                System.out
                    .println( object + ": " + results.get( object ) + "s" );
            }
        }
    }

    private void neoAlgoBenchMarkRunInternal()
    {
        System.out.println( "Starting benchmark " + getTestId() );
        setUpGlobal();
        // Warmup
        System.out.println( "Running warmup" );
        for ( int i = 0; i < numberOfWarmupRuns; ++i )
        {
            runBenchMark();
        }
        System.out.println( "Running benchmarks" );
        long start, end;
        long averageTime = 0;
        for ( int i = 0; i < numberOfRuns; ++i )
        {
            setUp();
            start = System.currentTimeMillis();
            runBenchMark();
            end = System.currentTimeMillis();
            tearDown();
            averageTime += end - start;
            System.out.println( "Time: " + ((end - start) / 1000.0) + "s" );
        }
        System.out.print( "Average time" );
        if ( currentInternalId != null )
        {
            System.out.print( " for " + currentInternalId );
            results.put( currentInternalId,
                (averageTime / numberOfRuns / 1000.0) );
        }
        System.out.println( ": " + (averageTime / numberOfRuns / 1000.0) + "s" );
        tearDownGlobal();
        if ( !saveStatistic( averageTime / numberOfRuns, true ) )
        {
            System.out.println( "Failed to save statistics" );
        }
    }

    /**
     * This method saves the measured time, by appending it to a normal text
     * file whose name is decided by getTestId().
     * @param milliSeconds
     * @param print
     *            If true, the time will be printed, together with a number of
     *            previous results.
     * @return False if something goes wrong, like no write permission to the
     *         file etc.
     */
    protected boolean saveStatistic( long milliSeconds, boolean print )
    {
        String fileName = "target/benchmarkstatistics/" + getTestId();
        String data = "";
        // Make directory
        new File( "target/benchmarkstatistics/" ).mkdirs();
        // Read
        try
        {
            BufferedReader reader = new BufferedReader( new FileReader(
                fileName ) );
            data = reader.readLine();
            reader.close();
        }
        catch ( Exception exception )
        {
        }
{'fraction_non_alphanumeric': 0.0907689517591585, 'fraction_numerical': 0.005259339862169025, 'mean_word_length': 2.0876259798432253, 'pattern_counts': {'":': 4, '<': 17, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '3512259', 'n_tokens_mistral': 3072, 'n_tokens_neox': 2933, 'n_words': 996}