# 10.awk脚本统计P50,P95,P99指标

# 统计脚本

#! /usr/bin/awk -f
{variance=0;sumCount+=$1;sumCost+=($2*$1);count[NR]=$1;cost[NR]=$2}
END { staticTotal[0]=50;
    staticTotal[1]=66;
    staticTotal[2]=80;
    staticTotal[3]=85;
    staticTotal[4]=90;
    staticTotal[5]=95;
    staticTotal[6]=98;
    staticTotal[7]=99;
    staticTotal[8]=100;

    staticFlag[0]=1;
    staticFlag[1]=1;
    staticFlag[2]=1;
    staticFlag[3]=1;
    staticFlag[4]=1;
    staticFlag[5]=1;
    staticFlag[6]=1;
    staticFlag[7]=1;
    staticFlag[8]=1;
    printf "%3s  %10s  %15s %15s\n", "static", "cost", "count", "diffPre";
    averageCost = sumCost/sumCount;

    for(i=1; i <=length(count); i++) {
        diff = (cost[i] - averageCost);
        variance += (diff*diff*count[i]/(sumCount-1));
        #printf("diff %s, variance %s, count[%s]: %s, cost[%s]: %s \n", diff, variance, i, count[i], i, cost[i]);
        countTotal += count[i];
        for (j=0; j <length(staticTotal); j++) {
          if (countTotal >= sumCount*staticTotal[j]/100) if (staticFlag[j]==1) {
              staticFlag[j]=sprintf("P%-3s  %10s %15s %15s", staticTotal[j],cost[i],countTotal, countTotal - countTotalPre);
               countTotalPre = countTotal;
          }
        }
    };

  for( i=0;i<length(staticFlag);i++)
    print staticFlag[i];

  printf "count total: %s\n", sumCount, countTotal;
  printf "average cost: %s \n", averageCost;
  printf "variance cost: %s \n", variance;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

# demo

[xueliang.wu@dev-asr-zhuanyong-001 ~/cnlu]$: cat cost.txt
300
300
100
200
500
700
1000
1
2
3
4
5
6
7
8
cat cost.txt |sort -n |uniq -c |awk -f calc_p95.sh

static        cost            count         diffPre
P50          300               4               4
P66          500               5               1
P80          700               6               1
P85          700               6               0
P90         1000               7               1
P95         1000               7               0
P98         1000               7               0
P99         1000               7               0
P100        1000               7               0
count total: 7
average cost: 442.857 
variance cost: 99523.8 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Last Updated: 6/20/2023, 9:16:18 AM
Apache License 2.0 | Copyright © 2022 by xueliang.wu 苏ICP备15016087号