/*
 * Copyright 2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.performance.experiment.java

import org.gradle.performance.AbstractCrossBuildPerformanceTest
import org.gradle.performance.categories.PerformanceExperiment
import org.gradle.performance.results.BaselineVersion
import org.gradle.performance.results.CrossBuildPerformanceResults
import org.junit.experimental.categories.Category
import spock.lang.Unroll

import static org.gradle.performance.generator.JavaTestProject.LARGE_JAVA_MULTI_PROJECT

@Category(PerformanceExperiment)
class JavaLibraryPluginPerformanceTest extends AbstractCrossBuildPerformanceTest {

    @Unroll
    def "java-library vs java on #testProject"() {
        def javaLibraryRuns = "java-library-plugin"
        def javaRuns = "java-plugin"

        given:
        runner.testGroup = "java plugins"
        runner.buildSpec {
            warmUpCount = warmUpRuns
            invocationCount = runs
            projectName(testProject.projectName).displayName(javaLibraryRuns).invocation {
                tasksToRun("clean", "assemble").gradleOpts("-Xms${testProject.daemonMemory}", "-Xmx${testProject.daemonMemory}")
            }
        }
        runner.baseline {
            warmUpCount = warmUpRuns
            invocationCount = runs
            projectName(testProject.projectName).displayName(javaRuns).invocation {
                tasksToRun("clean", "assemble").args("-PnoJavaLibraryPlugin").gradleOpts("-Xms${testProject.daemonMemory}", "-Xmx${testProject.daemonMemory}")
            }
        }

        when:
        def results = runner.run()

        then:
        def javaResults = buildBaselineResults(results, javaRuns)
        def javaLibraryResults = results.buildResult(javaLibraryRuns)
        def speedStats = javaResults.getSpeedStatsAgainst(javaLibraryResults.name, javaLibraryResults)
        println(speedStats)
        if (javaResults.significantlySlowerThan(javaLibraryResults)) {
            throw new AssertionError(speedStats)
        }

        where:
        testProject                   | warmUpRuns | runs
        LARGE_JAVA_MULTI_PROJECT      | 2          | 6
    }

    private static BaselineVersion buildBaselineResults(CrossBuildPerformanceResults results, String name) {
        def baselineResults = new BaselineVersion(name)
        baselineResults.results.name = name
        baselineResults.results.addAll(results.buildResult(name))
        return baselineResults
    }
}

Add 'java-library' vs 'java' performance test
Add 'java-library' vs 'java' performance test
