从wrk输出中提取文本

时间:2019-04-04 14:22:25

标签: jenkins awk sed graylog2 wrk

我正在用wrk2作为Jenkins的一项工作进行负载测试。我想将负载测试的结果发送给Graylog,但我只想存储Requests / Sec和平均延迟。

输出内容如下:

Running 30s test @ https://example.com

1 threads and 100 connections
  Thread calibration: mean lat.: 8338.285ms, rate sampling interval: 19202ms
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    16.20s     6.17s   29.64s    65.74%
    Req/Sec     5.00      0.00     5.00    100.00%
  Latency Distribution (HdrHistogram - Recorded Latency)
 50.000%   15.72s 
 75.000%   20.81s 
 90.000%   24.58s 
 99.000%   29.13s 
 99.900%   29.66s 
 99.990%   29.66s 
 99.999%   29.66s 
100.000%   29.66s 

  Detailed Percentile spectrum:
       Value   Percentile   TotalCount 1/(1-Percentile)

    4497.407     0.000000            1         1.00
    7561.215     0.100000           11         1.11
   11100.159     0.200000           22         1.25
   12582.911     0.300000           33         1.43
   14565.375     0.400000           44         1.67
   15720.447     0.500000           54         2.00
   16416.767     0.550000           60         2.22
   17301.503     0.600000           65         2.50
   18464.767     0.650000           71         2.86
   19185.663     0.700000           76         3.33
   20807.679     0.750000           81         4.00
   21479.423     0.775000           84         4.44
   22347.775     0.800000           87         5.00
   22527.999     0.825000           90         5.71
   23216.127     0.850000           93         6.67
   23478.271     0.875000           95         8.00
   23805.951     0.887500           96         8.89
   24723.455     0.900000           98        10.00
   25067.519     0.912500           99        11.43
   25395.199     0.925000          101        13.33
   26525.695     0.937500          102        16.00
   26525.695     0.943750          102        17.78
   26705.919     0.950000          103        20.00
   28065.791     0.956250          104        22.86
   28065.791     0.962500          104        26.67
   28377.087     0.968750          105        32.00
   28377.087     0.971875          105        35.56
   28475.391     0.975000          106        40.00
   28475.391     0.978125          106        45.71
   28475.391     0.981250          106        53.33
   29130.751     0.984375          107        64.00
   29130.751     0.985938          107        71.11
   29130.751     0.987500          107        80.00
   29130.751     0.989062          107        91.43
   29130.751     0.990625          107       106.67
   29655.039     0.992188          108       128.00
   29655.039     1.000000          108          inf
#[Mean    =    16199.756, StdDeviation   =     6170.105]
#[Max     =    29638.656, Total count    =          108]
#[Buckets =           27, SubBuckets     =         2048]
----------------------------------------------------------
  130 requests in 30.02s, 13.44MB read
  Socket errors: connect 0, read 0, write 0, timeout 1192
Requests/sec:      4.33
Transfer/sec:    458.47KB

有人知道我应该如何提取请求/秒(在底部)和平均延迟作为JSON参数发送吗?

预期输出为:"latency": 16.2, "requests_per_second": 4.33

1 个答案:

答案 0 :(得分:2)

您没有提供预期的输出,因此您的问题不清楚,但这是您想要的吗?

$ awk 'BEGIN{a["Latency"]; a["Requests/sec:"]} ($1 in a) && ($2 ~ /[0-9]/){print $1, $2}' file
Latency 16.20s
Requests/sec: 4.33

根据您向问题中添加预期输出进行了更新:

$ awk '
    BEGIN { map["Latency"]="latency"; map["Requests/sec:"]="requests_per_second" }
    ($1 in map) && ($2 ~ /[0-9]/) { printf "%s\"%s\": %s", ofs, map[$1], $2+0; ofs=", " }
    END { print "" }
' file
"latency": 16.2, "requests_per_second": 4.33