Relic Solution: Measuring a file download in Synthetics

There are two types of file downloads that this technique may work for:

  1. A file that is downloaded after clicking a button on a website.
  2. A file that is force downloaded upon hitting a URL.

The underlying technique for measuring the files is the same, so I'm going to walk through a single example.


You're going to need to import the fs module in order to help you traverse the file system:

var fs = require('fs');


Get familiar with recording custom attributes from within a synthetics monitor. For example, before you start a download, you'll need to note the start time. That may look something like this:

$util.insights.set('download_start', Math.round(+new Date()));

Note that I'm recording the time in milliseconds; it's clean and easy to work with integers as opposed to date objects.


This step is highly variable; it depends on how large the file is, what type of download method you're using, if there is anything in the DOM that indicates the completion of download, etc. It's important to take a moment here to ensure you understand why I'm highlighting this point. Only you will understand when to initiate the measurement of the downloaded file. It's important that the file download is complete before measuring it. This may mean, for example, that you utilize a callback or add a sleep call to wait for the download to finish. Another example would be that an element appears in the DOM once the download is complete, and you can wait for this element to appear before accessing the downloaded file.


Here's where things get exciting! Once the file has been downloaded, it can be stuffed into a variable like so:

var downloadedFile = fs.readFileSync('../user/Downloads/FILENAME.pdf');

Note the use of the fs module here. In our case, however, we're not particularly interested in holding the file within a variable. What we're interested in is the download time. In this case, we have a measurement of the start time that we took before we initiated the download. So, what we want to know is the time when the file download completed. In this case, the fs module gives us a pretty nifty way to grab this value using a stat function. For example, that may look like this:

fs.stat('../user/Downloads/FILENAME.pdf', function(err, stats){
   $util.insights.set('download_end', Math.round(+stats['mtime']));

In the example above, stats['mtime'] contains a timestamp denoting the last time the file was modified. To make things cleaner, you may consider simply subtracting download_start from download_end and sending only the duration to Insights.


Construct custom dashboards in Insights to show the value of what you've built!


Did this technique help you? Can you improve upon it? Is something wrong with this technique? I highly encourage you to share feedback so we can ensure that this technique is either improved or disproved. Thank you!