<?xml version="1.0"?>

<!-- =================================================== -->
<!-- Internal build targets for Berkeley DB Java Edition -->
<!-- All targets here are for internal use only and are  -->
<!-- not part of the published package.                  -->
<!-- =================================================== -->

<project name="JEInternal" default="jar" basedir="..">
<description>Internal targets for JE</description>

    <!--
    Properties that are intended to be passed via -D on the command line.
    -->
    <!-- Arguments to be passed on the command line of the <java> task. -->
    <property name="args" value=""/>
    <!-- Default jar file for testing is the je.jar built here. -->
    <property name="testjar" value="${jarfile}"/>

    <!--
    Common standalone test properties.
    -->
    <property name="standalone.srcdir" location="${basedir}/test/standalone"/>
    <property name="standalone.dir" location="${builddir}/test/standalone"/>
    <property name="standalone.destdir" location="${standalone.dir}/classes"/>
    <property name="standalone.datadir" location="${standalone.dir}/data"/>
    <property name="standalone.logfile" location="${standalone.dir}/log"/>
    <property name="standalone.propfile"
              location="${standalone.datadir}/je.properties"/>

    <path id="standalone.compile.classpath">
      <pathelement location="${destdir}"/>
      <pathelement location="${standalone.destdir}"/>
      <path refid="clover.classpath"/>
    </path>

    <path id="standalone.test.classpath">
      <pathelement location="${testjar}"/>
      <pathelement location="${standalone.destdir}"/>
      <path refid="clover.classpath"/>
    </path>

    <!-- ============================================================ -->
    <!-- Global Targets                                               -->
    <!-- ============================================================ -->

    <target name="init">
        <mkdir dir="${standalone.dir}"/>
        <mkdir dir="${standalone.destdir}"/>
    </target>

    <target name="clean" depends="clean-standalone"/>

    <target name="compile" depends="compile-standalone"/>

    <target name="init-standalone" depends="init">
        <delete dir="${standalone.datadir}"/>
        <mkdir dir="${standalone.datadir}"/>
    </target>

    <target name="clean-standalone" depends="init">
        <delete dir="${standalone.destdir}"/>
        <mkdir dir="${standalone.destdir}"/>
    </target>

    <target name="compile-standalone" depends="init-standalone">
        <ant antfile="ant/compile.xml" dir="." inheritall="false">
            <property name="srcdir" value="${standalone.srcdir}"/>
            <property name="destdir" value="${standalone.destdir}"/>
            <property name="include" value="**/*.java"/>
            <reference refid="standalone.compile.classpath"
	               torefid="compile.classpath"/>
        </ant>
    </target>


    <!-- Outputs the version of the ${jvm} so it can be visually verified. -->
    <target name="javaversion">
      <java classname="xxNotUsed" fork="yes" jvm="${jvm}">
        <jvmarg value="-version"/>
      </java>
    </target>

    <!-- =================== Standalone regression tests ======================
    Tests that have a standard Java main and are run separately via <java>
    rather than via <junit>.

    Specific test targets (e.g., MemoryStress) are also invoked via the
    'standalone' target in build.xml, using -Dtestcase=TestName where TestName
    is the name of the one of the targets below.  Additional program args and
    JVM args can be specified using -Dargs= and -Djvmargs=.
    
    Each test program should:
     + fail (System.exit with non-zero value) if the test fails
     + write log information and errors to System.out and System.err
     + take command line arguments for environment home and other parameters 
     + allow command line arguments to be repeated, so that the last specified
       argument takes precendence; this allows specifying -Dargs="..." to
       override arguments specified explicitly in the <java> task
     + Print the full command line at the start of the test
     + Check for data correctness, as well as for exceptions; if a
       correctness check fails, the test should fail
     + As part of correctness checking, check statistics to ensure that
       activities relevant to the test are occurring (cache misses,
       eviction, cleaning, etc)

    Each test target below should:
     + run the <java> task to invoke the standalone test program
     + use fork="yes" 
     + use failonerror="yes"
     + use output="${standalone.logfile}"
     + use the predefined standalone.test.classpath
     + pass ${standalone.datadir} as the environment directory argument
     + pass other program args using <arg>, followed by <arg line="${args}"/>
       to allow overriding program args using -Dargs="..."
     + pass JVM args using <jvmarg>, followed by <jvmarg line="${jvmargs}"/>
       to allow overriding JVM args using -Djvmargs="..."
     + for stress tests, use <jvmarg value="-ea"/> to enable assertions

    Each test may have any number of properties that can be passed via
    -Dargs="...".  However, a default regression test should run without
    specifying any such properties.

    Some tests (namely performance regressions such as BigRandom) are not meant
    to fail or pass in an automated way, and instead they output data that must
    be manually examined.

    By default a test is run against the je.jar built here.  -Dtestjar=JARFILE
    may be used to run the test againt a different release of JE, as long as
    the test only uses JE APIs that exist in that release, of course.  For
    example, this can be used to run BigRandom against an earlier released
    version of JE.

    Miscellaneous:
     + For long-running test, you can specify
       "-Djvmargs=-Xloggc:<file>, -XX:-PrintGCTimeStamps, -XX:-PrintGCDetails"
       to monitor GC behaviors. This would be useful for performance tests.
     + In case of performance tuning standalone tests, it would be helpful to
       print a full list of JE's perf tuning parameters(e.g. je.maxMemory,
       je.cleaner.threads, etc) to logs and then do the comparisons.
    ======================================================================= -->
    <target name="standalone" depends="javaversion">
      <echo message="Running: ${testcase}"/>
      <echo message="Using JE: ${testjar}"/>
      <echo message="Overriding: args=${args} jvmargs=${jvmargs}"/>
      <echo message="See results in: (${standalone.logfile})"/>
      <antcall target="${testcase}" inheritrefs="true"/>
    </target>

    <!--
    MemoryStress: Runs a stress test under low memory conditions to force an
    OutOfMemoryError to occur if a memory cache budget bug exists.  Also
    outputs a detailed log.
    
    Regression test:  Test should pass when run as follows:

      ant -Dtestcase=MemoryStress standalone
      ant -Dtestcase=MemoryStress -Dargs="-dups" standalone
    -->
    <target name="MemoryStress">
      <java fork="yes" jvm="${jvm}" failonerror="yes"
            classname="MemoryStress"
            output="${standalone.logfile}">
        <classpath refid="standalone.test.classpath"/>
        <jvmarg value="-Xmx42m"/>
	<jvmarg value="-ea"/>
	<jvmarg value="-DsetErrorListener=true"/>
	<jvmarg line="${jvmargs}"/>
        <arg value="-h"/>
        <arg value="${standalone.datadir}"/>
        <arg value="-nThreads"/>
        <arg value="20"/>
        <arg value="-nOps"/>
        <arg value="500000"/>
        <arg line="${args}"/>
      </java>
    </target>

    <!--
    EnvSharedCache: Runs a stress test comparing EnvironmentStats with/without
    setting EnvironmentConfig.setSharedCache(true) to see if the shared env
    cache works efficiently.  Also outputs a detailed log.
    
    Regression test:  Test should pass when run as follows:

      ant -Dtestcase=EnvSharedCache standalone
      ant -Dtestcase=EnvSharedCache -Dargs="-opentest" standalone
      ant -Dtestcase=EnvSharedCache -Dargs="-eventest" standalone
    -->
    <target name="EnvSharedCache">
      <java fork="yes" jvm="${jvm}" failonerror="yes"
            classname="EnvSharedCache"
            output="${standalone.logfile}">
        <classpath refid="standalone.test.classpath"/>
        <jvmarg value="-Xmx128m"/>
	<jvmarg value="-ea"/>
	<jvmarg value="-DsetErrorListener=true"/>
        <jvmarg line="${jvmargs}"/>
        <arg value="-v"/>
        <arg value="-h"/>
        <arg value="${standalone.datadir}"/>
        <arg value="-envs"/>
        <arg value="4"/>
        <arg value="-threads"/>
        <arg value="4"/>
        <arg value="-keys"/>
        <arg value="25000"/>
        <arg value="-initonly"/>
      </java>
      <!-- If it's opentest, use larger number of txns for a 12hour run. -->
      <condition property="num.txns" value="600000">
        <contains string="${args}" substring="-opentest"/>
      </condition>
      <condition property="num.txns" value="25000">
        <not>
          <isset property="num.txns"/>
        </not>
      </condition>
      <java fork="yes" jvm="${jvm}" failonerror="yes"
            classname="EnvSharedCache"
            output="${standalone.logfile}" append="true">
        <classpath refid="standalone.test.classpath"/>
        <jvmarg value="-Xmx128m"/>
	<jvmarg value="-ea"/>
	<jvmarg value="-DsetErrorListener=true"/>
        <jvmarg line="${jvmargs}"/>
        <arg value="-v"/>
        <arg value="-h"/>
        <arg value="${standalone.datadir}"/>
        <arg value="-envs"/>
        <arg value="4"/>
        <arg value="-threads"/>
        <arg value="4"/>
        <arg value="-txns"/>
        <arg value="${num.txns}"/>
        <arg value="-cachetest"/>
        <arg value="-shared"/>
        <arg line="${args}"/>
      </java>
    </target>

    <!--
    BigRandom: Outputs a detailed log and final throughput value.
    
    Regression test:  The final throughput (printed as the last line of the
    log) should be roughly equal or greater than for the prior release.  The
    test is normally run twice as follows, for each version of JE being tested;
    four times in all.  Each run takes apprx 12 to 24 hours.

      ant -Dtestcase=BigRandom standalone
      ant -Dtestcase=BigRandom -Dargs="-nosync" standalone

    After each test is run, be sure to save the log file for comparing it to
    subsequent runs and for generating graphs later.  For example:

      cp build/test/standalone/log bigrandom-je3.2.23-nosync-log

    Not yet done:
    + move maxMemory to a command line argument so it can be overridden using
      the args property.
    + automate running of gnuplot to produce graphs for comparing
      multiple runs.
    -->
    <target name="BigRandom">
      <echo message="je.maxMemory=268435456"
            file="${standalone.propfile}"/>
      <java fork="yes" jvm="${jvm}" failonerror="yes"
            classname="BigRandom"
            output="${standalone.logfile}">
        <classpath refid="standalone.test.classpath"/>
	<jvmarg value="-Xmx360m"/>
	<jvmarg value="-DsetErrorListener=true"/>
        <jvmarg line="${jvmargs}"/>
        <arg value="-h"/>
        <arg value="${standalone.datadir}"/>
        <arg value="-initonly"/>
        <arg value="-locality"/>
        <arg value="5"/>
        <arg value="-keys"/>
        <arg value="10000000"/>
        <arg line="${args}"/>
      </java>
      <echo message="result ${test.result}"/>
    </target>

    <!--
    RemoveDbStress: Make sure no bugs are spotted when remove/truncate
    database is being called and the log cleaner and checkpointer threads
    are interacting with the db. Also outputs a detailed log and final
    throughput value.
    
    Regression test:  The final throughput (printed as the last line of the
    log) should be roughly equal or greater than for the prior release.  The
    test is normally run once as follows when needed.
    Each run takes apprx 1 to 2 hours.

      ant -Dtestcase=RemoveDbStress standalone

    After each test is run, the log file is automatically copied to
    RemoveDbStress-<yyMMdd_HH>-log for comparing it to subsequent runs.

    -->
    <target name="RemoveDbStress">
      <java fork="yes" jvm="${jvm}" failonerror="yes"
            classname="RemoveDbStress"
            output="${standalone.logfile}">
        <classpath refid="standalone.test.classpath"/>
        <jvmarg value="-ea"/>
        <jvmarg value="-DsetErrorListener=true"/>
        <jvmarg line="${jvmargs}"/>
        <arg value="-h"/>
        <arg value="${standalone.datadir}"/>
        <arg value="-iter"/>
        <arg value="200000"/>
        <arg line="${args}"/>
      </java>
      <tstamp>
        <format property="log.time" pattern="yyMMdd_HH"/>
      </tstamp>
      <copy file="${standalone.logfile}"
            tofile="${standalone.dir}/RemoveDbStress-${log.time}-log"/>
    </target>

    <!--
    BigDW: Runs a stress test to check if deferred writed database works as
    properly as a normal one by performing insertion, dup-deletion and read
    operations against a normal database and a deferred writed database. The
    outputed throughput (printed as the last line of the log) should be around
    30 ops/sec, and 20000 txns should be enough for apprx 10 mins run.

    Regression test:  Test should pass when run as follows:

      ant -Dtestcase=BigDW standalone
      ant -Dtestcase=BigDW -Dargs="-txns 100000" standalone
    
    After each test is run, the log file is automatically copied to
    BigDW-<yyMMdd_HH>-log for comparing it to subsequent runs.
    -->
    <target name="BigDW">
      <java fork="yes" jvm="${jvm}" failonerror="yes"
            classname="BigDW"
            output="${standalone.logfile}">
        <classpath refid="standalone.test.classpath"/>
        <jvmarg value="-Xmx32m"/>
        <jvmarg line="${jvmargs}"/>
        <arg value="-v"/>
        <arg value="-h"/>
        <arg value="${standalone.datadir}"/>
        <arg value="-init"/>
        <arg value="-dupdel"/>
        <arg line="${args}"/>
      </java>
      <tstamp>
        <format property="log.time" pattern="yyMMdd_HH"/>
      </tstamp>
      <copy file="${standalone.logfile}"
            tofile="${standalone.dir}/BigDW-${log.time}-log"/>
    </target>

    <!--
    ClosedDbEviction: Runs a stress test to test the efficiency of eviction of
    closed database roots, to make sure that the eviction would not
    cause corruption or concurrency bugs. Also prints a detailed log.

    Regression test:  Test should pass when run as follows:

      ant -Dtestcase=ClosedDbEviction standalone
      ant -Dtestcase=ClosedDbEviction -Dargs="-recovery 10000000" standalone

    After each test is run, the log file is automatically copied to
    ClosedDbEviction-<yyMMdd_HH>-log for comparing it to subsequent runs.
    -->
    <target name="ClosedDbEviction">
      <java fork="yes" jvm="${jvm}" failonerror="yes"
            classname="ClosedDbEviction"
            output="${standalone.logfile}">
        <classpath refid="standalone.test.classpath"/>
        <jvmarg value="-Xmx512m"/>
        <jvmarg line="${jvmargs}"/>
        <arg value="-v"/>
        <arg value="-h"/>
        <arg value="${standalone.datadir}"/>
        <arg value="-init"/>
        <arg value="100000"/>
      </java>
      <java fork="yes" jvm="${jvm}" failonerror="yes"
            classname="ClosedDbEviction"
            output="${standalone.logfile}" append="true">
        <classpath refid="standalone.test.classpath"/>
        <jvmarg value="-Xmx32m"/>
        <jvmarg line="${jvmargs}"/>
        <arg value="-v"/>
        <arg value="-h"/>
        <arg value="${standalone.datadir}"/>
        <arg value="-contention"/>
        <arg value="100000000"/>
        <arg line="${args}"/>
      </java>
      <java fork="yes" jvm="${jvm}" failonerror="yes"
            classname="ClosedDbEviction"
            output="${standalone.logfile}" append="true">
        <classpath refid="standalone.test.classpath"/>
        <jvmarg value="-Xmx32m"/>
        <jvmarg line="${jvmargs}"/>
        <arg value="-v"/>
        <arg value="-h"/>
        <arg value="${standalone.datadir}"/>
        <arg value="-evict"/>
        <arg value="20000000"/>
        <arg line="${args}"/>
      </java>
      <tstamp>
        <format property="log.time" pattern="yyMMdd_HH"/>
      </tstamp>
      <copy file="${standalone.logfile}"
            tofile="${standalone.dir}/ClosedDbEviction-${log.time}-log"/>
    </target>

    <!--
    CleanWithSmallCache: Runs a stress test to test ...

    Regression test:  Test should pass when run as follows:

      ant -Dtestcase=CleanWithSmallCache standalone

    After each test is run, the log file is automatically copied to
    CleanWithSmallCache-<yyMMdd_HH>-log for comparing it to subsequent runs.
    -->
    <target name="CleanWithSmallCache">
      <echo message="CleanWithSmallCache: write"/>
      <java fork="yes" jvm="${jvm}" failonerror="yes"
            classname="CleanWithSmallCache"
            output="${standalone.logfile}">
        <classpath refid="standalone.test.classpath"/>
        <jvmarg value="-Xmx6m"/>
        <jvmarg line="${jvmargs}"/>
        <arg value="-h"/>
        <arg value="${standalone.datadir}"/>
        <arg line="-records 40000 -key 48 -data 10 -random -cache 250k -seconds 2000 -write 10000"/>
      </java>
      <echo message="CleanWithSmallCache: clean"/>
      <java fork="yes" jvm="${jvm}" failonerror="yes"
            classname="CleanWithSmallCache"
            output="${standalone.logfile}" append="true">
        <classpath refid="standalone.test.classpath"/>
        <jvmarg value="-Xmx15m"/>
        <jvmarg line="${jvmargs}"/>
        <arg value="-h"/>
        <arg value="${standalone.datadir}"/>
        <arg line="-records 40000 -key 48 -data 10 -random -cache 250k -seconds 22000 -read 10 -clean"/>
        <arg line="${args}"/>
      </java>
      <tstamp>
        <format property="log.time" pattern="yyMMdd_HH"/>
      </tstamp>
      <copy file="${standalone.logfile}"
            tofile="${standalone.dir}/CleanWithSmallCache-${log.time}-log"/>
    </target>
</project>
