'JMeter - Capture Columns from 2 csv files and write it to a 3rd file

I have 2 csv files

File 1:

         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

File 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
16:27:45    16:28:51     66 secs
19:38:12    19:39:12     60 secs

From File 1, Except the 1st row capture whole of the data. From File 2, extract only the 2nd column from last row. Write these data into File 3. In File 3, subtract column 3 from column 2 (These are time values) and display the results (time in secs) in column 4. Overall, File 3 should look like

File 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

Thanks for your support.

Regards, Ajith



Solution 1:[1]

You can do this using any suitable JSR223 Test Element with Groovy language.

Example code:

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')
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dmitri T