2012-11-29 54 views
3

我使用jmeter生成一些负载测试结果,并输出格式良好的csv文件,但现在我需要使用ruby进行一些编号处理。一个例子开始CSV文件:读取.csv文件并在ruby中进行简单统计

threadName,grpThreads,allThreads,URL,Latency,SampleCount,ErrorCount 
Thread Group 1-1,1,1,urlXX,240,1,0 
Thread Group 1-1,1,1,urlYY,463,1,0 
Thread Group 1-2,1,1,urlXX,200,1,0 
Thread Group 1-3,1,1,urlXX,212,1,0 
Thread Group 1-2,1,1,urlYY,454,1,0 
. 
. 
. 
Thread Group 1-N,1,1,urlXX,210,1,0 

现在,统计我需要阅读每个线程组的第一行中,添加延迟领域了,然后用我的线程组的数量划分,只获得平均延迟。然后迭代到每个线程组的第二行,等等..

我在想,也许我需要为每个线程组编写一些临时排序的csv文件(url的命令总是相同的在一个线程组中),然后使用这些作为输入,添加第一行,做数学,添加第二行,直到没有更多的行。

但由于线程组的数量变化,我一直没能写红宝石,使其能够围绕弯曲......任何代码示例将非常感激:)

+0

是否所有的线程组具有相同数量的行? – Faiz

+0

是的,他们这样做。每个线程组都通过相同的URL。通常测试集具有10-80个URL,但对于单次运行,它们都具有相同的值。我想使用URL作为哈希,可能比使用线程组名称更好。 其中一些运行我有10000个线程组,因此我正在寻找一种能够自动保持计算直到最后一个线程组的东西。 – tlatti

回答

1

[更新] - 这是你想要的吗,我想知道?

这怎么样 - 这可能是低效的,但它做你想做的?

CSV = File.readlines("data.csv") 
CSV.shift # minus the header. 

# Hash where key is grp name; value is list of HASHES with keys {:grp, :lat} 
hash = CSV. 
    map {|l| # Turn every line into a HASH of grp name and it's lats. 
    fs = l.split(","); {:grp => fs[0], :lat => fs[4]} 
    }. 
    group_by{|o| o[:grp]} 

# The largest number of lines we have in any group 
max_lines = hash.max_by{|gname, l| l.size}.size 

# AVGS is a list of averages. 
# AVGS[0] is the average lat. for all the first lines, 
# AVGS[1] is the average lat. for all second lines, etc. 
AVGS = 
(0..(max_lines-1)).map{|lno| # line no 
    total = # total latency for the i'th line... 
    hash.map {|gname, l| 
     if l[lno] then l[lno][:lat].to_i 
     else 0 end 
    } 
    total.reduce{|a,b| a+b}/(hash.size) 
} 

# So we have 'L' Averages - where L is the maximum number of 
# lines in any group. You could do anything with this list 
# of numbers... find the average again? 
puts AVGS.inspect 

应该返回类似:

[217/*avg for 1st-liners*/, 305 /*avg for 2nd liners*/] 
+0

这开始看起来像它,那里有好主意,我想我会稍后尝试用csv.foreach做同样的事情。 我的最终文件有1000-10000个线程组,我不知道为什么这只打印前2个?这里有.to_i ... – tlatti

+0

hash.max_by - > hash.values.max :) 我还在数学上打了一些小打印,将输出打印在一个不错的列中,更容易粘贴到电子表格:) – tlatti

相关问题