@@ -508,6 +508,106 @@ M.split_list = function(tbl, index, inclusive)
508508 return out
509509end
510510
511+ --- Default transformers for statistics
512+ local transform = {
513+ time = function (time )
514+ time = tonumber (time )
515+
516+ if time >= 60 then
517+ time = string.format (" %.2f" , time / 60 )
518+
519+ return time .. " min"
520+ end
521+
522+ local units = { " s" , " ms" , " µs" , " ns" }
523+ local unit = 1
524+
525+ while time < 1 and unit <= # units do
526+ time = time * 1000
527+ unit = unit + 1
528+ end
529+
530+ time = string.format (" %.2f" , time )
531+
532+ return time .. " " .. units [unit ]
533+ end ,
534+
535+ byte = function (bytes )
536+ bytes = tonumber (bytes )
537+
538+ local units = { " B" , " KiB" , " MiB" , " GiB" , " TiB" , " PiB" , " EiB" , " ZiB" , " YiB" }
539+ local unit = 1
540+
541+ while bytes >= 1024 and unit <= # units do
542+ bytes = bytes / 1024
543+ unit = unit + 1
544+ end
545+
546+ bytes = string.format (" %.2f" , bytes )
547+
548+ return bytes .. " " .. units [unit ]
549+ end ,
550+ }
551+
552+ --- Parse statistics line to a table with key, value pairs
553+ ---
554+ --- @param statistics_line string The statistics line from body
555+ ---
556+ --- @return string[] statistics
557+ local get_parsed_statistics = function (statistics_line )
558+ local out = {}
559+
560+ for _ , statistics_pair in ipairs (M .split (statistics_line , " &" )) do
561+ local value = M .split (statistics_pair , " =" , 1 )
562+
563+ if # value == 1 then
564+ table.insert (out , value [1 ])
565+ else
566+ out [value [1 ]] = value [2 ]
567+ end
568+ end
569+
570+ return out
571+ end
572+
573+ --- Parse and transform statistics line to a table of strings to be output.
574+ --- Returns the body without statistics line and a table of statistics lines.
575+ ---
576+ --- @param body string Response body
577+ ---
578+ --- @return string body , string[] statistics
579+ M .parse_statistics = function (body )
580+ local _ , _ , statistics = string.find (body , " [%c%s]+([^%c]*)$" )
581+ local config_statistics = config .get (" result" ).show_statistics
582+
583+ body = string.gsub (body , " [%c%s]+([^%c]*)$" , " " )
584+ local out = {}
585+
586+ statistics = get_parsed_statistics (statistics )
587+
588+ for _ , tbl in ipairs (config_statistics ) do
589+ if type (tbl ) == " string" then
590+ tbl = { tbl }
591+ end
592+
593+ local value = statistics [tbl [1 ]]
594+
595+ if tbl .type then
596+ if type (tbl .type ) == " string" then
597+ value = transform [tbl .type ](value )
598+ end
599+
600+ if type (tbl .type ) == " function" then
601+ value = tbl .type (value )
602+ end
603+ end
604+
605+ table.insert (out , (tbl .title or (tbl [1 ] .. " " )) .. value )
606+ end
607+
608+ return body , out
609+ end
610+
511611-- http_status returns the status code and the meaning, e.g. 200 OK
512612-- see https://httpstatuses.com/ for reference
513613-- @param code The request status code
0 commit comments