# Javascript calculator: Percentile

Document created by Stig_Skilbred on Mar 30, 2015Last modified by Guenter_Grossberger on May 28, 2015
Version 2Show Document

Hi all!

Some time ago Guenter_Grossberger asked me to help out on a PoC he was at. The customer wanted to see that we can report/monitor metric percentiles.

Percentiles are values most of us are familiar with I guess. In CEM for example, percentiles are calculated on the response times of a business transaction. For those of you not yet familiar with it, a quick web search give you what you're looking for

There are actually a couple of different ways to calculate a percentile, and you'll even find scientific documents about it. To keep things simple, I used an algorithm for dummies, and this is what it says:

To calculate the kth percentile (where k is any number between zero and one hundred), do the following steps:

1. Order all the values in the data set from smallest to largest.
2. Multiply k percent by the total number of values, n. This number is called the index.
3. If the index obtained in Step 2 is not a whole number, round it up to the nearest whole number and go to Step 4a.
If the index obtained in Step 2 is a whole number, go to Step 4b.
4. a) Count the values in your data set from left to right (from the smallest to the largest value) until you reach the number indicated by Step 3. The corresponding value in your data set is the kth percentile.
b) Count the values in your data set from left to right until you reach the number indicated by Step 2. The kth percentile is the average of that corresponding value in your data set and the value that directly follows it.

So, it is actually not that hard. With a JS calculator we can persist the values we get every 15 seconds. And then we calculate/find the percentile on this set of persisted values every n minutes.

The JS calculator "keeps" this value (to keep the agent from "graying out"), until it's time to calculate the value again. Also important - the set of persisted metrics are cleared after every calculation to prevent some kind of memory leak.

I've attached a zip, with different "versions" of the calculator (5 min interval, 10 min interval, etc.) as well as a management module to test the calculator (works with our standard demo). There is a short README at the beginning of the file, and the variables you might want to change are marked *** CHANGE ***. It shouldn't be to hard to get it.

Feel free to reach out to me with any question, and of course to use and modify the calculator. And remember to always keep an eye on the performance of your EM when running a lot of JS calculators I've only tested it locally, in a demo environment, so be careful at first. As Guenter would say: TEST, TEST and then do some TESTING

BR,

Stig