plugins {
    id 'me.champeau.gradle.jmh' version '0.2.0'
}

apply plugin: 'me.champeau.gradle.jmh'

uploadArchives.enabled = false
jar.enabled = false

task checkActions(type: JavaExec) {
    classpath = sourceSets.main.runtimeClasspath
    main = 'com.github.gumtree.dist.ActionsCollector'
    args 'check'
}

task collectActions(type: JavaExec) {
    classpath = sourceSets.main.runtimeClasspath
    main = 'com.github.gumtree.dist.ActionsCollector'
    args 'collect'
}

if (project.hasProperty('trees')) {
    task collectTrees(type: JavaExec) {
        classpath = sourceSets.main.runtimeClasspath
        main = 'com.github.gumtree.dist.BenchmarkCollector'
        args trees
    }
}

def getGitHash = { ->
  try {
    def stdout = new ByteArrayOutputStream()
    exec {
      commandLine 'git', 'describe', '--dirty=+', '--tags'
      standardOutput = stdout
    }
    return stdout.toString().trim()
  } catch (all) {
    return '0000000'
  }
}

jmh {
    fork = 0
    warmupIterations = 3
    iterations = 3
    benchmarkMode = 'avgt'
    benchmarkParameters = [
      'refPath' : new File("${project.projectDir}/src/jmh/resources/").listFiles()
        .collect { it.getAbsolutePath() }
        .findAll { it.matches(".*_v0_.*") }
        .join(",")
    ]
    humanOutputFile = project.file("${project.buildDir}/reports/jmh/human_${new Date().getTime()}_${getGitHash()}.txt")
    resultsFile = project.file("${project.buildDir}/reports/jmh/results_${new Date().getTime()}_${getGitHash()}.csv")
    resultFormat = 'CSV'
}

task jmhPlot(type: Exec, dependsOn: 'jmh') {
    commandLine "R", "-f", "${project.projectDir}/src/main/r/plotBenchmark.R", "${project.buildDir}/reports/jmh/"
}

dependencies {
    compile project(':client')
    compile project(':client.diff')
    compile project(':gen.antlr3')
    compile project(':gen.antlr3-antlr')
    compile project(':gen.antlr3-json')
    compile project(':gen.antlr3-php')
    compile project(':gen.antlr3-r')
    compile project(':gen.antlr3-xml')
    compile project(':gen.c')
    compile project(':gen.jdt')
    compile project(':gen.js')
    compile project(':gen.ruby')
}
