├── .gitignore ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── src ├── main ├── groovy │ └── net │ │ └── jokubasdargis │ │ └── buildtimer │ │ ├── BuildTimerPlugin.groovy │ │ ├── BuildTimerPluginExtension.groovy │ │ └── TimingsListener.groovy └── resources │ └── META-INF │ └── gradle-plugins │ └── net.jokubasdargis.build-timer.properties └── test └── groovy └── net └── jokubasdargis └── buildtimer ├── BuildTimerPluginBuildTest.groovy └── BuildTimerPluginTest.groovy /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | local.properties 3 | .DS_Store 4 | .idea 5 | build 6 | *.iml 7 | classes -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Gradle Build Timer Plugin 2 | ========================= 3 | 4 | A Gradle plugin which reports timings of a project build and individual project tasks. See what tasks take the longest to run: 5 | 6 | ``` 7 | Task timings over threshold: 8 | 128ms :app:mergeDebugAssets 9 | 1099ms :app:mergeDebugResources 10 | 788ms :app:processDebugResources 11 | 5000ms :app:compileDebugJava 12 | 13757ms :app:proguardDebug 13 | 15965ms :app:dexDebug 14 | 3449ms :app:packageDebug 15 | 158ms :app:zipalignDebug 16 | ``` 17 | 18 | Usage 19 | ---- 20 | Add the plugin to your `buildscript`'s `dependencies` section: 21 | 22 | ```groovy 23 | classpath 'net.jokubasdargis.buildtimer:gradle-plugin:0.3.0' 24 | ``` 25 | 26 | Apply the `build-timer` plugin: 27 | 28 | ```groovy 29 | apply plugin: 'net.jokubasdargis.build-timer' 30 | ``` 31 | 32 | Optionally configure the plugin: 33 | 34 | ```groovy 35 | buildTimer { 36 | reportAbove = 100L // The lowest time threshold (in ms) this plugin should report above, default is 50L 37 | sort = 'asc' // Sort timings by ms, possible values: 'none', 'asc', 'desc' 38 | } 39 | ``` 40 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | jcenter() 4 | mavenCentral() 5 | } 6 | 7 | dependencies { 8 | classpath 'com.bmuschko:gradle-nexus-plugin:2.2' 9 | } 10 | } 11 | 12 | apply plugin: 'idea' 13 | apply plugin: 'groovy' 14 | apply plugin: 'maven' 15 | apply plugin: 'com.bmuschko.nexus' 16 | 17 | group = 'net.jokubasdargis.buildtimer' 18 | version = '0.3.0' 19 | 20 | sourceCompatibility = JavaVersion.VERSION_1_6 21 | 22 | repositories { 23 | mavenCentral() 24 | } 25 | 26 | dependencies { 27 | compile gradleApi() 28 | compile localGroovy() 29 | 30 | testCompile 'junit:junit:4.10' 31 | testCompile 'org.easytesting:fest-assert-core:2.0M10' 32 | testCompile gradleTestKit() 33 | } 34 | 35 | install { 36 | repositories.mavenInstaller { 37 | pom.artifactId = 'gradle-plugin' 38 | } 39 | } 40 | 41 | modifyPom { 42 | project { 43 | artifactId = 'gradle-plugin' 44 | name 'Gradle Build Timer Plugin' 45 | description 'A Gradle plugin which reports timings of a project build and individual project tasks' 46 | url 'https://github.com/eleventigers/gradle-build-timer-plugin' 47 | inceptionYear '2015' 48 | 49 | scm { 50 | url 'https://github.com/eleventigers/gradle-build-timer-plugin' 51 | connection 'scm:git:git://github.com/eleventigers/gradle-build-timer-plugin.git' 52 | developerConnection 'scm:git:ssh://git@github.com/eleventigers/gradle-build-timer-plugin.git' 53 | } 54 | 55 | licenses { 56 | license { 57 | name 'The Apache Software License, Version 2.0' 58 | url 'http://www.apache.org/licenses/LICENSE-2.0.txt' 59 | distribution 'repo' 60 | } 61 | } 62 | 63 | developers { 64 | developer { 65 | id 'eleventigers' 66 | name 'Jokubas Dargis' 67 | email 'jokubas@obviousengine.com' 68 | } 69 | } 70 | 71 | organization { 72 | name 'Obvious Engineering, Ltd.' 73 | url 'http://obviousengine.com/' 74 | } 75 | } 76 | } 77 | 78 | task wrapper(type: Wrapper) { 79 | gradleVersion = '4.2' 80 | } -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eleventigers/gradle-build-timer-plugin/26c53f66910613325d795bf0f1dce5987ff7178f/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Sep 26 22:45:01 PDT 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.2-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /src/main/groovy/net/jokubasdargis/buildtimer/BuildTimerPlugin.groovy: -------------------------------------------------------------------------------- 1 | package net.jokubasdargis.buildtimer 2 | 3 | import org.gradle.api.Plugin 4 | import org.gradle.api.Project 5 | 6 | class BuildTimerPlugin implements Plugin { 7 | 8 | public static final String TAG = 'taggedByTimerPlugin' 9 | 10 | private BuildTimerPluginExtension extension 11 | 12 | @Override 13 | void apply(Project project) { 14 | extension = project.extensions.create( 15 | BuildTimerPluginExtension.NAME, BuildTimerPluginExtension) 16 | 17 | if (!project.rootProject.hasProperty(TAG)) { 18 | project.gradle.addListener(new TimingsListener(this)) 19 | project.rootProject.ext[TAG] = true 20 | } 21 | } 22 | 23 | BuildTimerPluginExtension.SortOrder getSortOrder() { 24 | extension.sort 25 | } 26 | } -------------------------------------------------------------------------------- /src/main/groovy/net/jokubasdargis/buildtimer/BuildTimerPluginExtension.groovy: -------------------------------------------------------------------------------- 1 | package net.jokubasdargis.buildtimer 2 | 3 | class BuildTimerPluginExtension { 4 | static enum SortOrder { 5 | NONE, ASC, DESC 6 | } 7 | 8 | final static String NAME = "buildTimer" 9 | final static long DEFAULT_REPORT_ABOVE = 50L 10 | final static SortOrder DEFAULT_SORT_ORDER = SortOrder.NONE 11 | long reportAbove = DEFAULT_REPORT_ABOVE 12 | SortOrder sort = DEFAULT_SORT_ORDER 13 | } -------------------------------------------------------------------------------- /src/main/groovy/net/jokubasdargis/buildtimer/TimingsListener.groovy: -------------------------------------------------------------------------------- 1 | package net.jokubasdargis.buildtimer 2 | 3 | import net.jokubasdargis.buildtimer.BuildTimerPluginExtension.SortOrder 4 | import org.gradle.BuildListener 5 | import org.gradle.BuildResult 6 | import org.gradle.api.Task 7 | import org.gradle.api.execution.TaskExecutionListener 8 | import org.gradle.api.initialization.Settings 9 | import org.gradle.api.invocation.Gradle 10 | import org.gradle.api.tasks.TaskState 11 | import org.gradle.internal.time.Timer 12 | import org.gradle.internal.time.Time 13 | 14 | import java.util.concurrent.ConcurrentHashMap 15 | 16 | final class TimingsListener implements TaskExecutionListener, BuildListener { 17 | 18 | private final BuildTimerPlugin plugin 19 | final Map timings = new ConcurrentHashMap<>() 20 | 21 | TimingsListener(BuildTimerPlugin plugin) { 22 | this.plugin = plugin 23 | } 24 | 25 | @Override 26 | void beforeExecute(Task task) { 27 | timings.put(task, new Timing(task)) 28 | } 29 | 30 | @Override 31 | void afterExecute(Task task, TaskState taskState) { 32 | Timing timing = timings.get(task) 33 | 34 | if (timing) { 35 | timing.complete() 36 | 37 | if (timing.report) { 38 | task.project.logger.warn "${timing.path} took ${timing.ms}ms" 39 | } 40 | } 41 | } 42 | 43 | @Override 44 | void buildFinished(BuildResult result) { 45 | boolean reportWhenFinished = timings.values().find { Timing timing -> timing.report } 46 | 47 | if (reportWhenFinished) { 48 | println "Task timings over threshold:\n" 49 | Comparator comparator = Timing.orderByMsFor(plugin.sortOrder) 50 | timings.values().toSorted(comparator).each { Timing timing -> 51 | if (timing.report) { 52 | printf "%7sms %s\n", [timing.ms, timing.path] 53 | } 54 | } 55 | } 56 | timings.clear() 57 | } 58 | 59 | @Override 60 | void buildStarted(Gradle gradle) {} 61 | 62 | @Override 63 | void projectsEvaluated(Gradle gradle) {} 64 | 65 | @Override 66 | void projectsLoaded(Gradle gradle) {} 67 | 68 | @Override 69 | void settingsEvaluated(Settings settings) {} 70 | 71 | static final class Timing { 72 | 73 | private static final ORDER_BY_MS_DESC = new OrderBy([{ -it.ms }]) 74 | private static final ORDER_BY_MS_ASC = new OrderBy([{ it.ms }]) 75 | private static final ORDER_BY_NONE = new OrderBy() 76 | 77 | private final Task task 78 | private final Timer timer 79 | private boolean report 80 | private long ms 81 | 82 | Timing(Task task) { 83 | this.task = task 84 | this.timer = Time.startTimer() 85 | } 86 | 87 | String getPath() { 88 | task.path 89 | } 90 | 91 | void complete() { 92 | ms = timer.elapsedMillis 93 | report = (ms > getReportAboveForTask()) 94 | } 95 | 96 | long getReportAboveForTask() { 97 | BuildTimerPluginExtension extension = task.project.extensions.findByType( 98 | BuildTimerPluginExtension) 99 | extension?.reportAbove ?: BuildTimerPluginExtension.DEFAULT_REPORT_ABOVE 100 | } 101 | 102 | static Comparator orderByMsFor(SortOrder sortOrder) { 103 | switch (sortOrder) { 104 | case SortOrder.ASC: 105 | return ORDER_BY_MS_ASC; 106 | break 107 | case SortOrder.DESC: 108 | return ORDER_BY_MS_DESC; 109 | break 110 | case SortOrder.NONE: 111 | default: 112 | return ORDER_BY_NONE; 113 | break 114 | } 115 | } 116 | } 117 | } -------------------------------------------------------------------------------- /src/main/resources/META-INF/gradle-plugins/net.jokubasdargis.build-timer.properties: -------------------------------------------------------------------------------- 1 | implementation-class=net.jokubasdargis.buildtimer.BuildTimerPlugin -------------------------------------------------------------------------------- /src/test/groovy/net/jokubasdargis/buildtimer/BuildTimerPluginBuildTest.groovy: -------------------------------------------------------------------------------- 1 | package net.jokubasdargis.buildtimer 2 | 3 | import java.util.regex.Pattern 4 | 5 | import static org.gradle.testkit.runner.TaskOutcome.SUCCESS 6 | 7 | import org.gradle.testkit.runner.GradleRunner 8 | import org.junit.Before 9 | import org.junit.Rule 10 | import org.junit.Test 11 | import org.junit.rules.TemporaryFolder 12 | 13 | class BuildTimerPluginBuildTest { 14 | 15 | private static final String LABEL_OVER_THRESHOLD = "Task timings over threshold:" 16 | private static final Pattern TIMING_REGEX = Pattern.compile("^\\d*ms {2,}:") 17 | 18 | @Rule public final TemporaryFolder testProjectDir = new TemporaryFolder() 19 | 20 | File buildFile 21 | 22 | @Before 23 | void setUp() { 24 | buildFile = testProjectDir.newFile('build.gradle') 25 | } 26 | 27 | private static List getPluginClasspath() { 28 | System.getProperty("java.class.path").split(File.pathSeparator) 29 | .collect { new File(it) } 30 | } 31 | 32 | @Test 33 | void reportTimingsAboveDefault() { 34 | buildFile << """ 35 | plugins { 36 | id 'net.jokubasdargis.build-timer' 37 | } 38 | 39 | task longTask { 40 | doLast { 41 | Thread.sleep(60) 42 | } 43 | } 44 | """ 45 | 46 | def result = GradleRunner.create() 47 | .withProjectDir(testProjectDir.root) 48 | .withArguments('longTask') 49 | .withPluginClasspath(pluginClasspath) 50 | .build() 51 | 52 | assert result.output.contains("Task timings over threshold:") 53 | // cannot check exact timing string logged as it will always be slightly different 54 | assert result.output.contains("longTask took") 55 | assert result.task(':longTask').outcome == SUCCESS 56 | } 57 | 58 | @Test 59 | void reportMultipleTimingsAboveDefault() { 60 | buildFile << """ 61 | plugins { 62 | id 'net.jokubasdargis.build-timer' 63 | } 64 | 65 | task longTask1 { 66 | doLast { 67 | Thread.sleep(60) 68 | } 69 | } 70 | 71 | task longTask2 { 72 | doLast { 73 | Thread.sleep(100) 74 | } 75 | } 76 | """ 77 | 78 | def result = GradleRunner.create() 79 | .withProjectDir(testProjectDir.root) 80 | .withArguments('longTask1', 'longTask2') 81 | .withPluginClasspath(pluginClasspath) 82 | .build() 83 | 84 | assert result.output.contains("Task timings over threshold:") 85 | assert result.output.contains("longTask1 took") 86 | assert result.output.contains("longTask2 took") 87 | assert result.task(':longTask1').outcome == SUCCESS 88 | assert result.task(':longTask2').outcome == SUCCESS 89 | } 90 | 91 | @Test 92 | void reportMultipleTimingsSortOrderDesc() { 93 | buildFile << """ 94 | plugins { 95 | id 'net.jokubasdargis.build-timer' 96 | } 97 | 98 | buildTimer { 99 | reportAbove = 1L 100 | sort = 'desc' 101 | } 102 | 103 | task longTask1 { 104 | doLast { 105 | Thread.sleep(1) 106 | } 107 | } 108 | 109 | task longTask2 { 110 | doLast { 111 | Thread.sleep(50) 112 | } 113 | } 114 | 115 | task longTask3 { 116 | doLast { 117 | Thread.sleep(100) 118 | } 119 | } 120 | """ 121 | 122 | def result = GradleRunner.create() 123 | .withProjectDir(testProjectDir.root) 124 | .withArguments('longTask1', 'longTask2', 'longTask3') 125 | .withPluginClasspath(pluginClasspath) 126 | .build() 127 | 128 | 129 | assert result.task(':longTask1').outcome == SUCCESS 130 | assert result.task(':longTask2').outcome == SUCCESS 131 | assert result.task(':longTask3').outcome == SUCCESS 132 | 133 | def output = result.output; 134 | 135 | def expected = ["longTask3", "longTask2", "longTask1"] 136 | def actual = output 137 | .substring(output.indexOf(LABEL_OVER_THRESHOLD) + LABEL_OVER_THRESHOLD.length() + 1) 138 | .tokenize("\n").collect{ it.trim().replaceFirst(TIMING_REGEX, "") } 139 | 140 | assert expected == actual 141 | } 142 | 143 | @Test 144 | void reportMultipleTimingsSortOrderAsc() { 145 | buildFile << """ 146 | plugins { 147 | id 'net.jokubasdargis.build-timer' 148 | } 149 | 150 | buildTimer { 151 | reportAbove = 1L 152 | sort = 'asc' 153 | } 154 | 155 | task longTask1 { 156 | doLast { 157 | Thread.sleep(100) 158 | } 159 | } 160 | 161 | task longTask2 { 162 | doLast { 163 | Thread.sleep(10) 164 | } 165 | } 166 | 167 | task longTask3 { 168 | doLast { 169 | Thread.sleep(50) 170 | } 171 | } 172 | """ 173 | 174 | def result = GradleRunner.create() 175 | .withProjectDir(testProjectDir.root) 176 | .withArguments('longTask1', 'longTask2', 'longTask3') 177 | .withPluginClasspath(pluginClasspath) 178 | .build() 179 | 180 | 181 | assert result.task(':longTask1').outcome == SUCCESS 182 | assert result.task(':longTask2').outcome == SUCCESS 183 | assert result.task(':longTask3').outcome == SUCCESS 184 | 185 | def output = result.output; 186 | 187 | def expected = ["longTask2", "longTask3", "longTask1"] 188 | def actual = output 189 | .substring(output.indexOf(LABEL_OVER_THRESHOLD) + LABEL_OVER_THRESHOLD.length() + 1) 190 | .tokenize("\n").collect{ it.trim().replaceFirst(TIMING_REGEX, "") } 191 | 192 | assert expected == actual 193 | } 194 | 195 | @Test 196 | void reportTimingsAboveCustom() { 197 | def customAbove = 100L 198 | 199 | buildFile << """ 200 | plugins { 201 | id 'net.jokubasdargis.build-timer' 202 | } 203 | 204 | buildTimer { 205 | reportAbove = ${customAbove} 206 | } 207 | 208 | task longTask { 209 | doLast { 210 | Thread.sleep(110) 211 | } 212 | } 213 | """ 214 | 215 | def result = GradleRunner.create() 216 | .withProjectDir(testProjectDir.root) 217 | .withArguments('longTask') 218 | .withPluginClasspath(pluginClasspath) 219 | .build() 220 | 221 | assert result.output.contains("Task timings over threshold:") 222 | assert result.output.contains("longTask took") 223 | assert result.task(':longTask').outcome == SUCCESS 224 | } 225 | 226 | @Test 227 | void skipReportTimingsBelowDefault() { 228 | buildFile << """ 229 | plugins { 230 | id 'net.jokubasdargis.build-timer' 231 | } 232 | 233 | buildTimer { 234 | reportAbove = 1000 235 | } 236 | 237 | task quickTask { 238 | doLast { 239 | println 'hello quick task' 240 | } 241 | } 242 | """ 243 | 244 | def result = GradleRunner.create() 245 | .withProjectDir(testProjectDir.root) 246 | .withArguments('quickTask') 247 | .withPluginClasspath(pluginClasspath) 248 | .build() 249 | 250 | assert !result.output.contains("Task timings over threshold:") 251 | assert !result.output.contains("quickTask took") 252 | assert result.task(':quickTask').outcome == SUCCESS 253 | } 254 | 255 | @Test 256 | void combinedReportWithSubprojectBuild() { 257 | buildFile << """ 258 | plugins { 259 | id 'net.jokubasdargis.build-timer' 260 | } 261 | 262 | buildTimer { 263 | reportAbove = 250 264 | } 265 | 266 | task longTask { 267 | doLast { 268 | Thread.sleep(260) 269 | } 270 | } 271 | """ 272 | 273 | testProjectDir.newFile('settings.gradle') << "include 'submodule'" 274 | new File(testProjectDir.newFolder('submodule'), 'build.gradle') << """ 275 | apply plugin: 'net.jokubasdargis.build-timer' 276 | 277 | buildTimer { 278 | reportAbove = 100 279 | } 280 | 281 | task submoduleLongTask { 282 | doLast { 283 | Thread.sleep(110) 284 | } 285 | } 286 | """ 287 | 288 | def result = GradleRunner.create() 289 | .withProjectDir(testProjectDir.root) 290 | .withArguments('longTask', 'submoduleLongTask') 291 | .withPluginClasspath(pluginClasspath) 292 | .build() 293 | 294 | assert result.output.contains("Task timings over threshold:") 295 | assert result.output.contains("longTask took") 296 | assert result.output.contains("submoduleLongTask took") 297 | assert result.task(':longTask').outcome == SUCCESS 298 | assert result.task(':submodule:submoduleLongTask').outcome == SUCCESS 299 | } 300 | 301 | } -------------------------------------------------------------------------------- /src/test/groovy/net/jokubasdargis/buildtimer/BuildTimerPluginTest.groovy: -------------------------------------------------------------------------------- 1 | package net.jokubasdargis.buildtimer; 2 | 3 | import org.gradle.api.Project 4 | import org.gradle.api.Task 5 | import org.gradle.testfixtures.ProjectBuilder 6 | import org.junit.Test 7 | 8 | class BuildTimerPluginTest { 9 | 10 | @Test 11 | void pluginApplies() { 12 | Project project = ProjectBuilder.builder().build() 13 | project.apply plugin: 'net.jokubasdargis.build-timer' 14 | 15 | assert project.getPluginManager().hasPlugin('net.jokubasdargis.build-timer') 16 | } 17 | 18 | @Test 19 | void setupDefaultTimingsListener() { 20 | Project project = ProjectBuilder.builder().build() 21 | TimingsListener listener = new TimingsListener() 22 | 23 | Task task = project.tasks.create("task") 24 | listener.beforeExecute(task) 25 | 26 | TimingsListener.Timing timing = listener.timings.values().first() 27 | timing.getReportAboveForTask() == BuildTimerPluginExtension.DEFAULT_REPORT_ABOVE 28 | } 29 | 30 | @Test 31 | void setupCustomTimingsListener() { 32 | Project project = ProjectBuilder.builder().build() 33 | TimingsListener listener = new TimingsListener() 34 | project.apply plugin: 'net.jokubasdargis.build-timer' 35 | 36 | def customTime = 60L 37 | project.buildTimer.reportAbove = customTime 38 | 39 | Task task = project.tasks.create("task") 40 | listener.beforeExecute(task) 41 | 42 | TimingsListener.Timing timing = listener.timings.values().first() 43 | timing.getReportAboveForTask() == customTime 44 | } 45 | 46 | } --------------------------------------------------------------------------------