<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.atlassian.performance.fisheye</groupId>
  <artifactId>automated-perftest</artifactId>
  <version>2.0-SNAPSHOT</version>
  <name>Fisheye Performance Test Runner</name>
  <description>Runs a performance test against a snapshot version of Fisheye</description>

  <properties>
    <!-- Static properties that are the same for all profiles-->
    <gc.jvm.args>-XX:+PrintGCTimeStamps -verbose:gc -XX:+PrintGCDetails -XX:+PrintTenuringDistribution -XX:NewSize=384m -XX:SurvivorRatio=2 -XX:+UseParallelGC -XX:+UseParallelOldGC  -Xloggc:${project.build.directory}/fisheye-gc.log</gc.jvm.args>
    <global.jvm.args>-Dorg.osgi.framework.bootdelegation=java.*,com.jprofiler.* -XX:MaxPermSize=128m -Xmx1024m ${gc.jvm.args}</global.jvm.args>
    <jvm.args>${global.jvm.args}</jvm.args>
    <!-- Performance tools related settings-->
    <jprofiler.home>/opt/performance/tools/jprofiler5/</jprofiler.home>
    <!--Where JProfiler is located-->
    <jmeter.home>/opt/performance/tools/jmeter/</jmeter.home>
    <!--Where JMeter was extracted too-->
    <performance.datadir>/opt/performance/datadir/basedata/fisheye/</performance.datadir>
    <!--Location of Fisheye Data-->
    <history.directory>/opt/performance/history/fisheye/${project.version}-${java.specification.version}-${dataset.version}</history.directory>
    <!--Where to save history files-->
    <script.fixedload.runtime>1800</script.fixedload.runtime>
    <!--Passed to JMeter - length of test-->
    <!-- Application and container settings-->
    <sleep.seconds>15</sleep.seconds>
    <fisheye.startup.opts>${jvm.args}</fisheye.startup.opts>
    <!--Fisheye comes in a version numbered directory from unzip-->
    <fisheye.installdir>${project.build.directory}/fisheye-2.0</fisheye.installdir>
    <!--These base directories are the containers for where the zips will be extracted.  See the xxx.location variables in the profile
        for the final location of the dataset and repository roots-->
    <!--Repository is the name of the svn repo-->
    <repository.basedir>${project.build.directory}/test-classes/repo</repository.basedir>
    <!--Dataset is the name of the CSV's for use in JMeter-->
    <dataset.basedir>${project.build.directory}/test-classes</dataset.basedir>
  </properties>
  <dependencies>
    <dependency>
      <groupId>com.atlassian.performance.fisheye</groupId>
      <artifactId>performance-test</artifactId>
      <type>zip</type>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>com.atlassian.performance.fisheye</groupId>
      <artifactId>source-repository</artifactId>
      <version>1</version>
      <type>zip</type>
      <scope>system</scope>
      <systemPath>${repository.zipfile}</systemPath>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.0-alpha-4</version>
        <executions>
          <execution>
            <id>30_unpack-fisheye-zip</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>unpack</goal>
            </goals>
            <configuration>
              <artifactItems>
                <artifactItem>
                  <groupId>com.atlassian.fisheye</groupId>
                  <artifactId>atlassian-fisheye</artifactId>
                  <version>2.0-SNAPSHOT</version>
                  <type>zip</type>
                </artifactItem>
              </artifactItems>
              <outputDirectory>${project.build.directory}</outputDirectory>
              <markersDirectory>${fisheye.installdir}/dependency-plugin-markers</markersDirectory>
            </configuration>
          </execution>
          <execution>
            <id>40_unpack-fisheye-dataset</id>
            <phase>generate-test-sources</phase>
            <goals>
              <goal>unpack-dependencies</goal>
            </goals>
            <configuration>
              <artifactItems>
                <artifactItem>
                  <groupId>com.atlassian.performance.fisheye</groupId>
                  <artifactId>source-repository</artifactId>
                </artifactItem>
              </artifactItems>
              <outputDirectory>${repository.basedir}</outputDirectory>
              <markersDirectory>${repository.basedir}/dependency-plugin-markers</markersDirectory>
            </configuration>
          </execution>
          <execution>
            <id>61_unpack-fisheye-performancetest</id>
            <phase>pre-integration-test</phase>
            <goals>
              <goal>unpack</goal>
            </goals>
            <configuration>
              <artifactItems>
                <artifactItem>
                  <groupId>com.atlassian.performance.fisheye</groupId>
                  <artifactId>performance-test</artifactId>
                  <type>zip</type>
                </artifactItem>
              </artifactItems>
              <outputDirectory>${project.build.directory}</outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
          <execution>
            <id>50_copy-fisheye-config</id>
            <phase>process-test-resources</phase>
            <configuration>
              <tasks>
                <replace file="${fisheye.config}" token="@repository-location@" value="${repository.location}"/>
                <copy file="${fisheye.config}" todir="${fisheye.installdir}" overwrite="true"/>
                <chmod dir="${fisheye.installdir}/bin" perm="ugo+rx" includes="**/*.sh"/><!--Not needed if processing a .tar file-->
              </tasks>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
          <execution>
            <id>65_sleep</id>
            <phase>integration-test</phase>
            <configuration>
              <tasks>
                <sleep seconds="${sleep.seconds}"/>
              </tasks>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.1</version>
        <configuration>
          <workingDirectory>${fisheye.installdir}</workingDirectory>
          <outputFile>${project.build.directory}/output.log</outputFile>
          <environmentVariables>
            <FISHEYE_OPTS>${fisheye.startup.opts}</FISHEYE_OPTS>
          </environmentVariables>
        </configuration>
        <executions>
          <execution>
            <id>60_start-fisheye</id>
            <phase>pre-integration-test</phase>
            <configuration>
              <executable>${fisheye.installdir}/bin/start.sh</executable>
            </configuration>
            <goals>
              <goal>exec</goal>
            </goals>
          </execution>
          <execution>
            <id>80_stop-fisheye</id>
            <phase>post-integration-test</phase>
            <configuration>
              <executable>${fisheye.installdir}/bin/stop.sh</executable>
            </configuration>
            <goals>
              <goal>exec</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>chronos-maven-plugin</artifactId>
        <version>1.0-atlassian-1</version>
        <configuration>
          <jmeterhome>${jmeter.home}</jmeterhome>
          <historydir>${history.directory}</historydir>
          <sysproperties>
            <property>
              <name>script.runtime</name>
              <value>${script.fixedload.runtime}</value>
            </property>
            <property>
              <name>script.base</name>
              <value>${project.build.directory}/performanceTest</value>
            </property>
            <property>
              <name>resource.dir</name>
              <value>${dataset.location}</value>
            </property>
          </sysproperties>
          <options>
            <value>-XX:+UseParallelGC</value>
            <value>-XX:+UseParallelOldGC</value>
          </options>
          <heap>756m</heap>
          <newsize>256m</newsize>
          <permsize>192m</permsize>
          <survivorratio>8</survivorratio>
        </configuration>
        <executions>
          <execution>
            <id>71_fixedload-test</id>
            <configuration>
              <dataid>fixedloadtest</dataid>
              <input>${project.build.directory}/performanceTest/jmeter-test-fixedload.jmx</input>
            </configuration>
            <goals>
              <goal>jmeter</goal>
              <goal>check</goal>
              <goal>savehistory</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <reporting>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>chronos-maven-plugin</artifactId>
        <version>1.0-SNAPSHOT</version>
        <configuration>
          <historydir>${history.directory}</historydir>
        </configuration>
        <reportSets>
          <reportSet>
            <id>fixedloadreport</id>
            <configuration>
              <dataid>fixedloadtest</dataid>
              <reportid>jmeter-fixedload-report</reportid>
              <title>JMeter Fixed Load Test Report</title>
              <description><![CDATA[Fixed Load Test Report]]></description>
            </configuration>
            <reports>
              <report>report</report>
              <report>historyreport</report>
            </reports>
          </reportSet>
        </reportSets>
      </plugin>
    </plugins>
  </reporting>
  <profiles>
    <profile>
      <id>dataset-sanity1</id>
      <properties>
        <repository.zipfile>${performance.datadir}/sanity1.zip</repository.zipfile>
        <repository.location>${repository.basedir}/sanity1</repository.location>
        <dataset.location>${dataset.basedir}/dataset-sanity1</dataset.location>
        <dataset.version>sanity1</dataset.version>
        <fisheye.config>${dataset.location}/config.xml</fisheye.config>
      </properties>
    </profile>
    <profile>
      <!--
        This profile does a few things:
        1.  Sets the JVM Args to contain the right stuff for running JProfiler
        2.  Search and replace on the JProfiler config to set the cpusnapshot save location to the ${project.build.directory}
        
        In order to make this work, you need a JProfiler config with triggers that fire every N minutes to save the CPU snapshot data out.  Edit the Jprofiler config
        and look for the output directory you set in the GUI.  Replace this with @project-dir@ and this ant-run task will replace it with the build dir.
        Finally, set the id=N (id=112 below) to the JProfiler session id you want to run.
        Keep the JProfiler config in src/test/resources/ and it will be copied to ${project.build.directory}/test-classes/ by maven.
        Don't forget to set LD_LIBRARY_PATH to preload the correct .so.
      -->
      <id>profile</id>
      <properties>
        <jprofiler.configfile>${project.build.directory}/test-classes/jprofilerconfig.xml</jprofiler.configfile>
        <jvm.args>-agentlib:jprofilerti=offline,id=112,config=${jprofiler.configfile} -Xbootclasspath/a:${jprofiler.home}/bin/agent.jar ${global.jvm.args}</jvm.args>
      </properties>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
              <execution>
                <id>51_update-jprofiler-paths</id>
                <!-- Antrun is broken WRT phases, so this is bound to process-test-classes instead of pre-integration-test -->
                <phase>process-test-resources</phase>
                <goals>
                  <goal>run</goal>
                </goals>
                <configuration>
                  <tasks>
                    <replace file="${jprofiler.configfile}" token="@project-dir@" value="${project.build.directory}"/>
                  </tasks>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
  <distributionManagement>
    <site>
      <id>atlassian-documentation</id>
      <url>dav:https://webdavshare/${project.artifactId}/${project.version}-${appserver.version}-${java.specification.version}-${dataset.version}</url>
    </site>
  </distributionManagement>
</project>
