/*
 * Copyright 2018 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.regression.android

import org.gradle.performance.AbstractCrossBuildPerformanceTest
import org.gradle.performance.fixture.GradleInvocationSpec
import org.gradle.performance.results.BaselineVersion
import spock.lang.Unroll

class RealLifeAndroidDexingTransformsPerformanceTest extends AbstractCrossBuildPerformanceTest {

    @Unroll
    def "#tasks on #testProject without (android/transform) cache"() {
        given:
        def dexingTransform = "dexing transform"
        def dexingTask = "dexing task"

        runner.testGroup = "Android dexing"
        runner.buildSpec {
            projectName(testProject)
            displayName(dexingTransform)
            warmUpCount warmUpRuns
            invocationCount runs
            invocation {
                defaultInvocation(tasks, memory, delegate)
                args("-Pandroid.enableDexingArtifactTransform=true")
            }
        }

        runner.baseline {
            projectName(testProject)
            displayName(dexingTask)
            warmUpCount warmUpRuns
            invocationCount runs
            invocation {
                defaultInvocation(tasks, memory, delegate)
                args("-Pandroid.enableDexingArtifactTransform=false")
            }
        }

        when:
        def result = runner.run()

        then:
        result.assertEveryBuildSucceeds()
        and:
        def transformResults = result.buildResult(dexingTransform)
        def taskResults = new BaselineVersion("")
        taskResults.with {
            results.name = dexingTask
            results.addAll(result.buildResult(dexingTask))
        }
        def speedStats = taskResults.getSpeedStatsAgainst(dexingTransform, transformResults)
        println(speedStats)
        if (taskResults.significantlySlowerThan(transformResults)) {
            throw new AssertionError(speedStats)
        }

        where:
        testProject         | memory | warmUpRuns | runs | tasks
        'largeAndroidBuild' | '4g'   | 0          | 1    | 'clean phthalic:assembleDebug'
    }

    void defaultInvocation(String tasks, String memory, GradleInvocationSpec.InvocationBuilder builder) {
        with(builder) {
            tasksToRun(tasks.split(' '))
            cleanTasks("clean")
            gradleOpts("-Xms${memory}", "-Xmx${memory}")
            useDaemon()
            args("-Dorg.gradle.parallel=true", '-Dcom.android.build.gradle.overrideVersionCheck=true')
        }

    }
}

Add a cross build performance test
Add a cross build performance test
