JMeter - Groovy脚本执行错误的计算

9lowa7mx  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(117)

我有2个csv文件
档案一:

Name                   Start_Time
eml-alertservice               19:42:12
eml-redis                      19:42:12
eml-fluentd                    19:42:12
eml-kube-prometheus-stack      19:42:13
mwl-query-service              19:42:13
eml-grafana                    19:42:13
entity-management-service      19:42:14
snomed-service                 19:44:04
nuevo-dcm-services-wado        19:44:05
eis-common-postgres            19:45:43

档案二:

OS         K3s        Duration
18:10:06    18:10:45     39 secs
18:53:38    18:54:17     39 secs
20:03:19    20:03:58     39 secs
16:27:45    16:28:51     66 secs
19:38:12    19:39:12     60 secs

在文件1中,除第1行外,捕获全部数据。在文件2中,仅提取最后一行的第2列。将这些数据写入文件3。在文件3中,从第2列中减去第3列(这些是时间值),并在第4列中显示结果(时间,以秒为单位)。文件3的总体外观如下所示
档案三:

Name                 Start_Time            K3s         Duration
eml-alertservice               19:42:12           19:39:12      60 secs
eml-redis                      19:42:12           19:39:12      60 secs
eml-fluentd                    19:42:12           19:39:12      60 secs
eml-kube-prometheus-stack      19:42:13           19:39:12      61 secs
mwl-query-service              19:42:13           19:39:12      61 secs
eml-grafana                    19:42:13           19:39:12      61 secs
entity-management-service      19:42:14           19:39:12      62 secs
snomed-service                 19:44:04           19:39:12      292 secs
nuevo-dcm-services-wado        19:44:05           19:39:12      293 secs
eis-common-postgres            19:45:43           19:39:12      391 secs

下面的示例代码在大多数情况下都能正常工作:

def time = (new File('file2.csv').readLines().last() =~ /(\d{2}:\d{2}:\d{2})/)[1][1]

def file1 = new File('file1.csv').readLines()

def names = file1.drop(1).collect { line -> (line =~ /([a-z\-\_]+)/)[0][1] }
def times = file1.drop(1).collect { line -> (line =~ /(\d{2}:\d{2}:\d{2})/)[0][1] }

def file3 = new File('file3.csv')

file3 << 'Name' << '\t' << 'Start_Time' << '\t' << 'K3s' << '\t' << 'Duration' << System.getProperty('line.separator')

names.eachWithIndex { name, index ->

    def endTime = Date.parse('hh:mm:ss', times.get(index) as String)
    def startTime = Date.parse('hh:mm:ss', time as String)
    def delta = (endTime.getTime() - startTime.getTime()) / 1000 + ' secs'
    file3 << name << '\t' << times.get(index) << '\t' << time << '\t' << delta << System.getProperty('line.separator')
}

但在某种情况下,它的工作方式不同(显示意想不到的结果)。
文件1包含

Name                 Start_Time
auto-delete-service            11:57:18
workitem-management            12:02:25
eml-fluentd                    12:08:32
mwl-query-service              12:08:41
ian-scu-dcm-services           12:09:37
worklist-management            12:09:45
snomed-service                 12:09:45
eml-loki                       12:10:59
eml-api-gateway                12:11:20
eml-kibana                     12:12:28
eml-redis                      12:12:30
eisflowmanager                 12:12:42
eml-alertservice               12:12:49
transformer                    12:12:53
preferences-service            12:13:32
eml-device-registry            12:13:41
eml-elasticsearch              12:13:49
eml-kube-prometheus-stack      12:13:49
coreload-bootstrap-service     12:13:50
uid-dcm-services               12:13:50
eml-grafana                    12:14:06
eis-common-postgres            12:14:21
eisrabbitmq                    12:14:38

文件2具有

OS          K3s        Duration
18:10:06    18:10:45     39 secs
18:53:38    18:54:17     39 secs
20:03:19    20:03:58     39 secs
17:56:39    17:57:40     61 secs
18:25:38    18:26:18     40 secs
18:00:12    18:01:07     55 secs
19:22:47    19:23:42     55 secs
11:53:33    11:54:28     55 secs
18:19:01    18:19:56     55 secs
12:14:12    12:15:07     55 secs
12:30:19    12:31:17     58 secs
14:20:51    14:21:46     55 secs
15:45:02    15:45:57     55 secs
16:52:38    16:53:36     58 secs
10:32:45    10:33:42     57 secs
11:56:15    11:57:13     58 secs

文件3的结果为

Name                   Start_Time    K3s      Duration
auto-delete-service           11:57:18   11:57:13   5 secs
workitem-management           12:02:25   11:57:13   -42888 secs
eml-fluentd                   12:08:32   11:57:13   -42521 secs
mwl-query-service             12:08:41   11:57:13   -42512 secs
ian-scu-dcm-services          12:09:37   11:57:13   -42456 secs
worklist-management           12:09:45   11:57:13   -42448 secs
snomed-service                12:09:45   11:57:13   -42448 secs
eml-loki                      12:10:59   11:57:13   -42374 secs
eml-api-gateway               12:11:20   11:57:13   -42353 secs
eml-kibana                    12:12:28   11:57:13   -42285 secs
eml-redis                     12:12:30   11:57:13   -42283 secs
eisflowmanager                12:12:42   11:57:13   -42271 secs
eml-alertservice              12:12:49   11:57:13   -42264 secs
transformer                   12:12:53   11:57:13   -42260 secs
preferences-service           12:13:32   11:57:13   -42221 secs
eml-device-registry           12:13:41   11:57:13   -42212 secs
eml-elasticsearch             12:13:49   11:57:13   -42204 secs
eml-kube-prometheus-stack     12:13:49   11:57:13   -42204 secs
coreload-bootstrap-service    12:13:50   11:57:13   -42203 secs
uid-dcm-services              12:13:50   11:57:13   -42203 secs
eml-grafana                   12:14:06   11:57:13   -42187 secs
eis-common-postgres           12:14:21   11:57:13   -42172 secs
eisrabbitmq                   12:14:38   11:57:13   -42155 secs

在上面的结果中,某个地方的计算出错了。当第二列和第三列的值分别为12和11小时时,这个bug被注意到了。它对其他值也很好用。
请帮助我修复这个bug。谢谢你的支持。
此致,阿吉斯

aamkag61

aamkag611#

在脚本中将hh更改为HH
根据SimpleDateFormat文档:

  • h-上午/下午的小时数(1-12)
  • H-一天中的小时(0-23)

有关JMeter中的Groovy脚本的更多信息:Apache Groovy: What Is Groovy Used For?

相关问题