diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 index ca611cf..9bdfd77 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,14 @@ +.idea +*.iml +build +/bin .gradle -build/ - -# Ignore Gradle GUI config -gradle-app.setting +.poject +.settings +.classpath +commitHash.txt +logs +.log +.zip +/out +*~ diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..5c977d9 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,7 @@ +jdk: + - oraclejdk8 + - oraclejdk7 + - openjdk6 + +script: +- ./gradlew clean build \ No newline at end of file diff --git a/README.md b/README.md index b9eab70..4ee8c91 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,79 @@ +[![Build Status](https://travis-ci.org/marcingrzejszczak/gradle-test-profiler.svg)](https://travis-ci.org/marcingrzejszczak/gradle-test-profiler) + # gradle-test-profiler -## How to add it +Created with @AChudzik + +## Since version 0.1.0 + +### How to add it + +You have to add `jcenter` to buildscript repositories + +``` + +buildscript { + repositories { + jcenter() + } + dependencies { + classpath 'com.blogspot.toomuchcoding:gradle-test-profiler:0.1.0' + } +} + +apply plugin: 'com.blogspot.toomuchcoding.testprofiler' + +``` + + +### How to run it + +It's enough to execute + +``` +./gradlew clean profileTests + +``` + + +### How to configure it + +You have a special section called `testprofiler` + +``` +testprofiler { + + // Separator of columns in the output report + separator = '\t' + + // Headers in the report + outputReportHeaders = "module${separator}test class name${separator}test name${separator}test execution time in [s]${separator}test class execution time in [s]\n" + + // Closure that will be converted to a Comparator to compare row entries + comparator = DefaultTestExecutionComparator.DEFAULT_TEST_EXECUTION_COMPARATOR + + // Closure that converts a reporter row entry to a single String + rowFromReport = ReportStorer.DEFAULT_ROW_FROM_REPORT_CONVERTER + + // Base directory where reports will be gathered. The parent of this directory is build dir of the project + reportOutputDir = "reports/test_profiling" + + // Filename of a single report + reportOutputCsvFilename = "testsProfile.csv" + + // Base directory where merged summary of reports will be kept. The parent of directory is the top root project dir + mergedSummaryDir = "reports/test_profiling" + + // Filename of a merged summary of reports + mergedSummaryFileName = "summary.csv" +} + +``` + + +## Deprecated (up till version 0.0.4) + +### How to add it For the time being just enter in your project @@ -10,7 +83,7 @@ if (project.hasProperty('testsProfiling')) { } ``` -## How to run it? +### How to run it? Execute diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..b4ef197 --- /dev/null +++ b/build.gradle @@ -0,0 +1,95 @@ +buildscript { + repositories { + mavenLocal() + jcenter() + } + dependencies { + classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:+' + classpath 'com.ofg:uptodate-gradle-plugin:1.2.0' + if (project.hasProperty('coverage')) { + classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:1.0.2' + } + } +} + +apply plugin: 'com.jfrog.bintray' +apply plugin: 'com.ofg.uptodate' +apply plugin: 'maven-publish' +apply plugin: 'groovy' + +repositories { + mavenLocal() + jcenter() +} + +dependencies { + compile gradleApi() + compile localGroovy() + compile "org.slf4j:slf4j-api:1.7.10" + + testRuntime 'org.slf4j:slf4j-log4j12:1.7.10' + testCompile('org.spockframework:spock-core:0.7-groovy-2.0') { + exclude module: 'groovy-all' + } + testRuntime 'cglib:cglib-nodep:2.2.2' + testRuntime 'org.objenesis:objenesis:1.2' +} + +bintrayUpload.dependsOn 'build' + +task sourcesJar(type: Jar, dependsOn: classes) { + classifier = 'sources' + from sourceSets.main.allSource +} + +task javadocJar(type: Jar, dependsOn: groovydoc) { + classifier = 'javadoc' + from groovydoc.destinationDir +} + +artifacts { + archives sourcesJar + archives javadocJar +} + +publishing { + publications { + plugin(MavenPublication) { + from components.java + artifact sourcesJar + artifact javadocJar + } + } +} + +bintray { + user = project.properties['bintrayUser'] + key = project.properties['bintrayKey'] + publications = ['plugin'] + pkg { + repo = 'com-blogspot-toomuchcoding' + name = 'gradle-test-profiler' + desc = 'Gradle plugin that profiles your tests. Gives you a CSV file sorted bby tests execution time.' + licenses = ['Apache-2.0'] + labels = ['gradle', 'testing'] + } +} + +if (project.hasProperty('coverage')) { + apply plugin: 'jacoco' + apply plugin: 'com.github.kt3k.coveralls' + + jacoco { + toolVersion = '0.7.1.201405082137' + } + + jacocoTestReport { + reports { + xml.enabled = true // coveralls plugin depends on xml format report + html.enabled = true + } + } + test { + ignoreFailures = true + } +} diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..30a5c67 --- /dev/null +++ b/gradle.properties @@ -0,0 +1,2 @@ +group=com.blogspot.toomuchcoding +version=0.1.0-SNAPSHOT \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..2322723 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..45be0a1 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Thu Jan 08 13:54:43 CET 2015 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-2.2-all.zip diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..91a7e26 --- /dev/null +++ b/gradlew @@ -0,0 +1,164 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; +esac + +# For Cygwin, ensure paths are in UNIX format before anything is touched. +if $cygwin ; then + [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` +fi + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >&- +APP_HOME="`pwd -P`" +cd "$SAVED" >&- + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules +function splitJvmOpts() { + JVM_OPTS=("$@") +} +eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS +JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" + +exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100755 index 0000000..8a0b282 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,90 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windowz variants + +if not "%OS%" == "Windows_NT" goto win9xME_args +if "%@eval[2+2]" == "4" goto 4NT_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* +goto execute + +:4NT_args +@rem Get arguments from the 4NT Shell from JP Software +set CMD_LINE_ARGS=%$ + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/src/main/groovy/com/blogspot/toomuchcoding/testprofiler/DefaultTestExecutionComparator.groovy b/src/main/groovy/com/blogspot/toomuchcoding/testprofiler/DefaultTestExecutionComparator.groovy new file mode 100644 index 0000000..67d3a3d --- /dev/null +++ b/src/main/groovy/com/blogspot/toomuchcoding/testprofiler/DefaultTestExecutionComparator.groovy @@ -0,0 +1,32 @@ +package com.blogspot.toomuchcoding.testprofiler + +import groovy.transform.PackageScope +import groovy.transform.CompileStatic +import groovy.util.logging.Slf4j + +@PackageScope +@CompileStatic +@Slf4j +class DefaultTestExecutionComparator implements Comparator { + + protected static final Closure DEFAULT_TEST_EXECUTION_COMPARATOR = { ReportRow o1, ReportRow o2 -> + if (o1.testExecutionResult.testExecutionTime <=> o2.testExecutionResult.testExecutionTime != 0) { + return o2.testExecutionResult.testExecutionTime <=> o1.testExecutionResult.testExecutionTime + } + if (o1.testExecutionResult.testClassName <=> o2.testExecutionResult.testClassName != 0) { + return o2.testExecutionResult.testClassName <=> o1.testExecutionResult.testClassName + } + if (o1.testExecutionResult.testName <=> o2.testExecutionResult.testName != 0) { + return o2.testExecutionResult.testName <=> o1.testExecutionResult.testName + } + if (o1.testClassExecutionTime <=> o2.testClassExecutionTime != 0) { + return o2.testClassExecutionTime <=> o1.testClassExecutionTime + } + return o2.module <=> o1.module + } + + @Override + int compare(ReportRow o1, ReportRow o2) { + return DEFAULT_TEST_EXECUTION_COMPARATOR(o1, o2) + } +} diff --git a/src/main/groovy/com/blogspot/toomuchcoding/testprofiler/ProfiledTest.groovy b/src/main/groovy/com/blogspot/toomuchcoding/testprofiler/ProfiledTest.groovy new file mode 100644 index 0000000..0793ba3 --- /dev/null +++ b/src/main/groovy/com/blogspot/toomuchcoding/testprofiler/ProfiledTest.groovy @@ -0,0 +1,32 @@ +package com.blogspot.toomuchcoding.testprofiler +import groovy.transform.CompileStatic +import groovy.transform.PackageScope +import groovy.util.logging.Slf4j +import org.gradle.api.tasks.OutputDirectory +import org.gradle.api.tasks.testing.Test + +import java.util.concurrent.ConcurrentHashMap + +@PackageScope +@CompileStatic +@Slf4j +class ProfiledTest extends Test { + + TestProfilerPluginExtension testProfilerPluginExtension + @OutputDirectory File reportDir + @OutputDirectory File mergedTestProfilingSummaryDir + + ProfiledTest() { + Set testExecutionResults = Collections.newSetFromMap(new ConcurrentHashMap()) + addTestListener(new TestExecutionResultSavingTestListener(testExecutionResults)) + doLast storeReport(testExecutionResults) + } + + private Closure storeReport(Set testExecutionResults) { + return { + log.debug("Stored results are $testExecutionResults") + new ReportStorer(getTestProfilerPluginExtension(), project, getReportDir(), getMergedTestProfilingSummaryDir()).storeReport(testExecutionResults) + } + } + +} diff --git a/src/main/groovy/com/blogspot/toomuchcoding/testprofiler/ReportMerger.groovy b/src/main/groovy/com/blogspot/toomuchcoding/testprofiler/ReportMerger.groovy new file mode 100644 index 0000000..13b4465 --- /dev/null +++ b/src/main/groovy/com/blogspot/toomuchcoding/testprofiler/ReportMerger.groovy @@ -0,0 +1,42 @@ +package com.blogspot.toomuchcoding.testprofiler +import groovy.transform.CompileStatic +import groovy.transform.PackageScope +import groovy.util.logging.Slf4j +import org.gradle.api.DefaultTask +import org.gradle.api.tasks.OutputDirectory +import org.gradle.api.tasks.TaskAction + +@PackageScope +@CompileStatic +@Slf4j +class ReportMerger extends DefaultTask { + + TestProfilerPluginExtension testProfilerPluginExtension + @OutputDirectory File mergedTestProfilingSummaryDir + + @TaskAction + void testsProfileSummaryReport() { + getMergedTestProfilingSummaryDir().mkdirs() + File mergedTestProfilingSummary = new File(getMergedTestProfilingSummaryDir(), getTestProfilerPluginExtension().mergedSummaryFileName) + log.debug("Will store merged test profiling summary in [${mergedTestProfilingSummary}]") + String fileContent = mergedTestProfilingSummary.text + log.debug("Saving file [$mergedTestProfilingSummary] content [$fileContent]") + mergedTestProfilingSummary.text = getTestProfilerPluginExtension().outputReportHeaders + Set reportRows = new TreeSet(getTestProfilerPluginExtension().comparator as Comparator) + appendReportRow(fileContent, reportRows) + mergedTestProfilingSummary << reportRows.collect(rowFromReport()).join('\n') + println "Your combined report is available here [$mergedTestProfilingSummary]" + } + + private void appendReportRow(String fileContent, Set reportRows) { + fileContent.split('\n').findAll { !it.contains(getTestProfilerPluginExtension().getOutputReportHeaders()) }.each { String string -> + String[] row = string.split(getTestProfilerPluginExtension().separator) + log.debug("Converting row $row") + reportRows << new ReportRow(row[0], new TestExecutionResult(row[1], row[2], row[3] as Double), row[4] as Double) + } + } + + Closure rowFromReport() { + return getTestProfilerPluginExtension().rowFromReport.curry(getTestProfilerPluginExtension()) + } +} diff --git a/src/main/groovy/com/blogspot/toomuchcoding/testprofiler/ReportRow.groovy b/src/main/groovy/com/blogspot/toomuchcoding/testprofiler/ReportRow.groovy new file mode 100644 index 0000000..58a1595 --- /dev/null +++ b/src/main/groovy/com/blogspot/toomuchcoding/testprofiler/ReportRow.groovy @@ -0,0 +1,16 @@ +package com.blogspot.toomuchcoding.testprofiler + +import groovy.transform.CompileStatic +import groovy.transform.Immutable + +/** + * @author Adam Chudzik + * @author Marcin Grzejszczak + */ +@CompileStatic +@Immutable(knownImmutableClasses = [TestExecutionResult]) +class ReportRow { + String module + TestExecutionResult testExecutionResult + Double testClassExecutionTime +} diff --git a/src/main/groovy/com/blogspot/toomuchcoding/testprofiler/ReportStorer.groovy b/src/main/groovy/com/blogspot/toomuchcoding/testprofiler/ReportStorer.groovy new file mode 100644 index 0000000..c5f6f5e --- /dev/null +++ b/src/main/groovy/com/blogspot/toomuchcoding/testprofiler/ReportStorer.groovy @@ -0,0 +1,87 @@ +package com.blogspot.toomuchcoding.testprofiler + +import groovy.transform.PackageScope +import groovy.transform.CompileStatic +import groovy.util.logging.Slf4j +import org.gradle.api.Project + +@PackageScope +@CompileStatic +@Slf4j +class ReportStorer { + + protected static final Closure DEFAULT_ROW_FROM_REPORT_CONVERTER = { TestProfilerPluginExtension testProfilerPluginExtension , ReportRow reportRow -> + "${reportRow.module}${testProfilerPluginExtension.separator}${reportRow.testExecutionResult.testClassName}${testProfilerPluginExtension.separator}${reportRow.testExecutionResult.testName}${testProfilerPluginExtension.separator}${reportRow.testExecutionResult.testExecutionTime}${testProfilerPluginExtension.separator}${reportRow.testClassExecutionTime}".toString() + } + + private final TestProfilerPluginExtension testProfilerPluginExtension + private final Project project + private final File reportDir + private final File mergedTestProfilingSummaryDir + + ReportStorer(TestProfilerPluginExtension testProfilerPluginExtension, Project project, File reportDir, File mergedTestProfilingSummaryDir) { + this.testProfilerPluginExtension = testProfilerPluginExtension + this.project = project + this.reportDir = reportDir + this.mergedTestProfilingSummaryDir = mergedTestProfilingSummaryDir + } + + ReportStorer(TestProfilerPluginExtension testProfilerPluginExtension) { + this.testProfilerPluginExtension = testProfilerPluginExtension + } + + public void storeReport(Set testExecutionResults) { + log.debug("All test execution results $testExecutionResults") + File report = createNewReportFile() + addHeadersToFile(report) + Map classExecutionTime = calculateClassExecutionTime(testExecutionResults) + log.debug("Calculated class execution time $classExecutionTime") + String testExecutionResult = buildTestExecutionResult(classExecutionTime, testExecutionResults) + log.debug("Test execution result [$testExecutionResult]") + appendTestExecutionResultToFile(report, testExecutionResult) + println "Your tests report is ready at [$report.absolutePath]" + appendTestExecutionResultToMergedTestSummary(testExecutionResult) + } + + private void appendTestExecutionResultToMergedTestSummary(String testExecutionResult) { + mergedTestProfilingSummaryDir.mkdirs() + File mergedTestProfilingSummary = new File(mergedTestProfilingSummaryDir, testProfilerPluginExtension.mergedSummaryFileName) + mergedTestProfilingSummary << testExecutionResult << '\n' + log.debug("Stored [$testExecutionResult] in [$mergedTestProfilingSummary]") + } + + private File appendTestExecutionResultToFile(File report, String testExecutionResult) { + return report << testExecutionResult + } + + private File addHeadersToFile(File report) { + return report << testProfilerPluginExtension.outputReportHeaders + } + + private File createNewReportFile() { + File reportDir = new File(project.buildDir.absolutePath, testProfilerPluginExtension.reportOutputDir) + reportDir.mkdirs() + File report = new File(reportDir, testProfilerPluginExtension.reportOutputCsvFilename) + report.delete() + return report + } + + private String buildTestExecutionResult(Map classExecutionTime, Set testExecutionResults) { + return testExecutionResults.collect { + new ReportRow(project.path, it, classExecutionTime[it.testClassName]) + }.sort(testProfilerPluginExtension.comparator) + .collect(rowFromReport()).join('\n') + } + + private Map calculateClassExecutionTime(Set testExecutionResults) { + return testExecutionResults.groupBy { + TestExecutionResult testExecutionResult -> testExecutionResult.testClassName + }.collectEntries { + [it.key, (it.value.sum { TestExecutionResult testExecutionResult -> testExecutionResult.testExecutionTime } as Double).round(3)] + } as Map + } + + Closure rowFromReport() { + return testProfilerPluginExtension.rowFromReport.curry(testProfilerPluginExtension) + } +} diff --git a/src/main/groovy/com/blogspot/toomuchcoding/testprofiler/TestExecutionResult.groovy b/src/main/groovy/com/blogspot/toomuchcoding/testprofiler/TestExecutionResult.groovy new file mode 100644 index 0000000..78bb7c3 --- /dev/null +++ b/src/main/groovy/com/blogspot/toomuchcoding/testprofiler/TestExecutionResult.groovy @@ -0,0 +1,16 @@ +package com.blogspot.toomuchcoding.testprofiler + +import groovy.transform.CompileStatic +import groovy.transform.Immutable + +/** + * @author Adam Chudzik + * @author Marcin Grzejszczak + */ +@CompileStatic +@Immutable +class TestExecutionResult { + String testClassName + String testName + Double testExecutionTime +} diff --git a/src/main/groovy/com/blogspot/toomuchcoding/testprofiler/TestExecutionResultSavingTestListener.groovy b/src/main/groovy/com/blogspot/toomuchcoding/testprofiler/TestExecutionResultSavingTestListener.groovy new file mode 100644 index 0000000..a01c75f --- /dev/null +++ b/src/main/groovy/com/blogspot/toomuchcoding/testprofiler/TestExecutionResultSavingTestListener.groovy @@ -0,0 +1,36 @@ +package com.blogspot.toomuchcoding.testprofiler + +import groovy.transform.PackageScope +import groovy.transform.CompileStatic +import groovy.util.logging.Slf4j +import org.gradle.api.tasks.testing.TestDescriptor +import org.gradle.api.tasks.testing.TestListener +import org.gradle.api.tasks.testing.TestResult + +@PackageScope +@CompileStatic +@Slf4j +class TestExecutionResultSavingTestListener implements TestListener { + + private final Set testExecutionResults + + TestExecutionResultSavingTestListener(Set testExecutionResults) { + this.testExecutionResults = testExecutionResults + } + + @Override + void beforeSuite(TestDescriptor suite) { } + + @Override + void afterSuite(TestDescriptor suite, TestResult result) { } + + @Override + void beforeTest(TestDescriptor testDescriptor) { } + + @Override + void afterTest(TestDescriptor testDescriptor, TestResult result) { + TestExecutionResult testExecutionResult = new TestExecutionResult(testDescriptor.className, testDescriptor.name, ((result.endTime - result.startTime) / 1000) as Double) + log.info("Gathered Test Execution Result [$testExecutionResult]") + testExecutionResults << testExecutionResult + } +} diff --git a/src/main/groovy/com/blogspot/toomuchcoding/testprofiler/TestProfilerPlugin.groovy b/src/main/groovy/com/blogspot/toomuchcoding/testprofiler/TestProfilerPlugin.groovy new file mode 100644 index 0000000..f503289 --- /dev/null +++ b/src/main/groovy/com/blogspot/toomuchcoding/testprofiler/TestProfilerPlugin.groovy @@ -0,0 +1,39 @@ +package com.blogspot.toomuchcoding.testprofiler +import groovy.transform.PackageScope +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.Task + +class TestProfilerPlugin implements Plugin { + + @PackageScope static final String TEST_PROFILER_TASK_NAME = "calculateTestExecutionTime" + @PackageScope static final String SUMMARY_REPORT_TASK_NAME = "profileTests" + + void apply(Project project) { + TestProfilerPluginExtension extension = project.extensions.create(TEST_PROFILER_TASK_NAME, TestProfilerPluginExtension) + createSummaryReportTask(project, extension) + createProfiledTestTask(project, extension) + } + + private void createProfiledTestTask(Project project, TestProfilerPluginExtension extension) { + Task task = project.tasks.create(TEST_PROFILER_TASK_NAME, ProfiledTest) + task.group = 'Verification' + task.description = "Prepares a report with execution time of your tests and performs custom logic upon profiling" + task.conventionMapping.with { + testProfilerPluginExtension = { extension } + reportDir = { new File(project.buildDir, extension.reportOutputDir) } + mergedTestProfilingSummaryDir = { new File(project.rootDir, extension.mergedSummaryDir) } + } + } + + private void createSummaryReportTask(Project project, TestProfilerPluginExtension extension) { + Task task = project.tasks.create(SUMMARY_REPORT_TASK_NAME, ReportMerger) + task.group = 'Verification' + task.description = "Combines the reports into a single a file" + task.dependsOn(TEST_PROFILER_TASK_NAME) + task.conventionMapping.with { + testProfilerPluginExtension = { extension } + mergedTestProfilingSummaryDir = { new File(project.rootDir, extension.mergedSummaryDir) } + } + } +} \ No newline at end of file diff --git a/src/main/groovy/com/blogspot/toomuchcoding/testprofiler/TestProfilerPluginExtension.groovy b/src/main/groovy/com/blogspot/toomuchcoding/testprofiler/TestProfilerPluginExtension.groovy new file mode 100644 index 0000000..f6cdaa4 --- /dev/null +++ b/src/main/groovy/com/blogspot/toomuchcoding/testprofiler/TestProfilerPluginExtension.groovy @@ -0,0 +1,48 @@ +package com.blogspot.toomuchcoding.testprofiler + +import groovy.transform.CompileStatic +import groovy.transform.ToString + +@CompileStatic +@ToString +class TestProfilerPluginExtension { + /** + * Separator of columns in the output report + */ + String separator = '\t' + + /** + * Headers in the report + */ + String outputReportHeaders = "module${separator}test class name${separator}test name${separator}test execution time in [s]${separator}test class execution time in [s]\n" + + /** + * Closure that will be converted to a Comparator to compare row entries + */ + Closure comparator = DefaultTestExecutionComparator.DEFAULT_TEST_EXECUTION_COMPARATOR + + /** + * Closure that converts a reporter row entry to a single String + */ + Closure rowFromReport = ReportStorer.DEFAULT_ROW_FROM_REPORT_CONVERTER + + /** + * Base directory where reports will be gathered. The parent of this directory is build dir of the project + */ + String reportOutputDir = "reports/test_profiling" + + /** + * Filename of a single report + */ + String reportOutputCsvFilename = "testsProfile.csv" + + /** + * Base directory where merged summary of reports will be kept. The parent of directory is the top root project dir + */ + String mergedSummaryDir = "reports/test_profiling" + + /** + * Filename of a merged summary of reports + */ + String mergedSummaryFileName = "summary.csv" +} diff --git a/src/main/resources/META-INF/gradle-plugins/com.blogspot.toomuchcoding.testprofiler.properties b/src/main/resources/META-INF/gradle-plugins/com.blogspot.toomuchcoding.testprofiler.properties new file mode 100644 index 0000000..c24ecab --- /dev/null +++ b/src/main/resources/META-INF/gradle-plugins/com.blogspot.toomuchcoding.testprofiler.properties @@ -0,0 +1 @@ +implementation-class=com.blogspot.toomuchcoding.testprofiler.TestProfilerPlugin \ No newline at end of file