Search code examples
antgulpgraphitestatsd

Defining "global" behavior in Gulp (measuring task duration)


I'm working on moving us from ant to gulp, and as part of the effort I want to write timing stats to Graphite. We're doing this in ant as well (no idea how, beside the point anyway). My question is, I'd prefer to not have to add some or other plugin manually to every task we have (we have over 60), but rather have some sort of global behavior, where for every task, before the task is run a timer is start, and when it signals completion we push some data to Graphite (over statsd).

Can someone point me in the right direction where to hook into gulp for this? I couldn't find anything particularly useful in the docs / recipes...

We're running gulp@4.


Solution

  • Instead of adding timing code to your numerous tasks, you could make use of the NPM gulp-duration package.

    A snippet of an example of it's use is shown below:

    function rebundle() {
     var uglifyTimer = duration('uglify time')
     var bundleTimer = duration('bundle time')
    
     return bundler.bundle()
       .pipe(source('bundle.js'))
       .pipe(bundleTimer)
       // start just before uglify recieves its first file 
       .once('data', uglifyTimer.start)
       .pipe(uglify())
       .pipe(uglifyTimer)
       .pipe(gulp.dest('example/'))
    }
    

    gulp-duration's duration function:

    Creates a new pass-through duration stream. When this stream is closed, it will log the amount of time since its creation to your terminal.

    will then allow you to log the duration of the task.

    Whilst this is not a global behaviour solution, at least you can specify the timing code in your gulp file, as opposed to having to modify all 60+ of your tasks.