Is there an easy way to calculate quantiles using bash?

Suppose I have a log file from a web server with response time to a request:

_1st_request 1334
_2nd_request 345
_3rd_request 244
_4th_request 648
......... etc

Is there an easy way with a bash script to find the top decile (10- quantile )? In other words, to answer the question: how slow was the slowest query if I exclude the slowest 10% of the queries?

+3
source share
2 answers
awk '{print $2}' | sort -rn | perl -e '$d=.1;@l=<>;print $l[int($d*$#l)]'

It would be more elegant to do all this in perl. If you want to use a temporary file, you can use wc + head / tail to select a quantile from a sorted list of numbers.

+6
source

, , , , 10% .

FILE=responseTimes.log
TMPFILE=tmpfile
sort -k 2 -n $FILE > $TMPFILE
LINECOUNT=`wc -l $TMPFILE | sed -e 's/^ *//' -e 's/ .*$//'`
TARGETLINE=echo "$LINECOUNT * 9 / 10" | bc
sed -n "$TARGETLINE{p;q;}" $TMPFILE

, .

+2

Source: https://habr.com/ru/post/1716050/


All Articles