2013-01-10 107 views
2

我对R相对比较陌生,而且这是我第一次尝试使用它来实际分析一些数据。问题是这样的:我有一个包含日志的请求数量的CSV文件送达以下形式给定系统:时间序列每秒钟,每小时,每天的请求

# Unix timestamp, number of requests 
1354810257,241624 
1354810258,244759 
1354810259,245307 
1354810260,248961 

在文件中包含相对于周期间的信息的时刻。现在我需要获取一张图表,显示系统每秒钟,每小时和每天的请求数量。

+0

请求数实际上是一个单调增加的序列。 – nopper

+0

如果这是真的,那么应该使用开始和结束值之间的差异来按秒,小时和日期聚合。 @nopper需要为测试提供一个更好的示例,并需要阐明其数据的基本含义。 –

+0

整个CSV文件是从监控节点集群的Graphite服务器中提取的。这里的请求数表示集群本身处理的项目数。假设它们是HTTP请求的数量,而群集是HTTP服务器。我需要的东西类似于http://stackoverflow.com/questions/5034513/how-to-graph-requests-per-second-from-web-log-file-using-r,唯一的区别是我需要统计每天,每小时和每秒,以了解系统的性能。 – nopper

回答

1

我用Python和matplotlib解决了它。代码与此类似:

import csv 
from pylab import * 
from itertools import groupby 

def by_hour(value): 
    return value[0] // 3600 

def plot_data_for(data, map_, reduce_): 
    keys = [] 
    values = [] 
    for k,v in groupby(data, key=map_): 
     keys.append(k) 
     values.append(reduce_(v)) 
    return (keys, values) 

times = [] 
requests = [] 
reader = csv.reader(open("results.csv")) 

for row in reader: 
    times.append(int(row[0])) 
    requests.append(int(row[1])) 

increments = map(lambda x: x[1] - x[0], zip(requests, requests[1:] + [requests[-1]])) 
plot(*plot_data_for(zip(times, increments), by_hour, lambda values: sum(map(lambda x: x[1], values))))