2
\$\begingroup\$

I use JMeter for load testing and want to report error on CI pipeline when application though put is less than 1/sec. JMeter logs summary of results as -

2017年01月17日 01:57:00 INFO - jmeter.reporters.Summariser: summary + 103 in 00:00:30 = 3.4/s Avg: 2937 Min: 48 Max: 7962 Err: 0 (0.00%) Active: 2 Started: 10 Finished: 8 
2017年01月17日 01:57:00 INFO - jmeter.reporters.Summariser: summary = 12218 in 01:00:03 = 3.4/s Avg: 2942 Min: 20 Max: 27317 Err: 1 (0.01%)
2017年01月17日 01:57:00 INFO - jmeter.reporters.Summariser: summary + 1 in 00:00:00 = 66.7/s Avg: 3227 Min: 3227 Max: 3227 Err: 0 (0.00%) Active: 0 Started: 10 Finished: 10 
2017年01月17日 01:57:00 INFO - jmeter.reporters.Summariser: summary = 12219 in 01:00:03 = 3.4/s Avg: 2942 Min: 20 Max: 27317 Err: 1 (0.01%) 

Herein I would capture the last line and if value of throughput is less than 1 (which is 3.4/s in the current example) then it would be reported as error. Here is the script I wrote to capture value of throughput -

grep "meter.reporters.Summariser: summary" test.log | tail -1 | awk '{print 12ドル}' | cut -d "." -f1

and it gets me 3 which is ok. How can I improve this script?

asked Jan 17, 2017 at 10:03
\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

You are using here four different tools: grep, tail, awk and cut to do the following:

  • select lines
  • get the last one
  • print a given field of that one
  • extract the integer part of that field

This is clear and it works. However, using four processes is a bit of overkill, since all of these can be performed one-handedly via awk:

awk '/meter.reporters.Summariser: summary/ {rec=12ドル} END{printf "%d\n", rec}' file
  • /meter.reporters.Summariser: summary/ {rec=12ドル}
    When reading from the file, /pattern/ will match those lines containing the "pattern". If this happens, the action within {} is performed. In this case, it consists in storing the 12th field.
    But wait, weren't we looking for the last line in which this occurs? Yes, and this is exactly what we are doing by overwriting rec every time the pattern happens: last time it is written, it will store the definitive result.

  • END{printf "%d\n", rec}
    Once all lines have been processed, red will have the desired value on the form 3.4/s. We just want the 3 to appear, so we could do some string triming similar to the one you did with cut. However, we can also use awk's printf() to use Format-Control Letters to just print the integer part of it. This will also print 0 if rec is empty:

    $ awk 'BEGIN {printf "%s\n", "3.4/s"}'
    3.4/s
    $ awk 'BEGIN {printf "%d\n", "3.4/s"}'
    3
    $ awk 'BEGIN {printf "%d\n", ""}'
    0
    

Of course, you could also add the threshold logic in the script.

answered Jan 17, 2017 at 11:24
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.