为了测试下面的脚本,我生成了一些测试数据,即 16 个文件,每个文件有 12 条记录,随机出现的t
值不完全等于 的倍数1000
,使用以下帮助脚本
from random import random, seed
seed(1234)
for n in range(1, 17):
with open('f%02d'%n, 'w') as f:
for t in range(1000,12001,1000):
print(t+(1 if random()<0.1 else 0),int(random()*10000), sep=', ', file=f)
例如,文件f07
包含
1000, 3812
2000, 9600
3000, 9210
4000, 6852
5000, 8387
6001, 2603
7000, 8578
8000, 1137
9000, 2138
10000, 5875
11001, 8774
12000, 7768
以下脚本用于heapq.merge
对来自已排序文件的数据进行排序,数据由一个简单的生成器过滤,该生成器拆分单个记录并在记录所属的文件中添加信息,以便我们可以编写统一的记录,可能带有无效条目.
用于debug = False
在数据上使用脚本。
'''Usage: python3 merge.py filename1, filename2, ..., filenameout'''
from heapq import merge
from sys import argv, stdout
def reader(fobj, n):
for line in fobj:
yield [x.strip() for x in line.split(',')]+[n]
# change to debug = False to run on your data
debug = True
argv = ['']+['f%02d'%n for n in range(1, 17)]+[''] if debug else argv
out = stdout if debug else open(argv[-1], 'w')
readers = [reader(open(f), n) for n, f in enumerate(argv[1:-1])]
N = len(readers)
# header is "Interval,IOPS01,IOPS02,...,IOPSnn" modify as you like
print('Interval',*('IOPS%02d'%(n+1) for n in range(N)), sep=',', file=out)
# initialize for the loop
t0 = ""
# the loop
for t, iops, n in merge(*readers, key=lambda x:int(x[0])):
if t != t0:
if t0: print(t0, *iops_l, sep=',', file=out)
t0 = t
iops_l = ['····' if debug else '']*N
iops_l[n] = '%4s'%iops if debug else iops
# the last record to output
print(t0, *iops_l, sep=',', file=out)
f01
在, ...上运行程序f16
会产生
Interval,IOPS01,IOPS02,IOPS03,IOPS04,IOPS05,IOPS06,IOPS07,IOPS08,IOPS09,IOPS10,IOPS11,IOPS12,IOPS13,IOPS14,IOPS15,IOPS16
1000,4407, 889,9569,2956, 477,5845,3812,3079,····,2571,4994,8863,6394,5339,8280,9311
1001,····,····,····,····,····,····,····,····,1400,····,····,····,····,····,····,····
2000,····,2695,3053,1856, 379,8128,9600,····,5880, 283,9501, 709,1964,5371,7242,3195
2001,9109,····,····,····,····,····,····,3949,····,····,····,····,····,····,····,····
3000,5822,6446,····,1194, 876,4995,9210,6827,7535,····,5323,8030,6204,3628,3730,5248
3001,····,····,2767,····,····,····,····,····,····,9906,····,····,····,····,····,····
4000, 839,3552,1773,8294,6993,5490,6852,4005,2986,6665,1455,9396,5018,7121,6750,9895
5000,2368,9335,9547,8177,9450,1309,8387,2812,3305,5156,2432,3797,9101,4568,8340,····
5001,····,····,····,····,····,····,····,····,····,····,····,····,····,····,····,1974
6000,····,5301,8338,6410,····,9253,····,6004,2937,5152,5064, 524, 495,8768,7844,3358
6001,7887,····,····,····,2574,····,2603,····,····,····,····,····,····,····,····,····
7000,6232,····,····,7026,····, 697,8578,5188,8142,····,7734, 801,4680,5435,5463,6949
7001,····,5081,3861,····,4263,····,····,····,····,7595,····,····,····,····,····,····
8000,1485,····,3417,1571,3966, 17,1137,4574,5340,6980,3265,9292,3717,1917,7696, 293
8001,····,1437,····,····,····,····,····,····,····,····,····,····,····,····,····,····
9000,1144,3773,4759,2693,8873,6382,2138,6598,3396,2195,5617,6769,7784,8677,6985,3160
10000,····,····,4708,8404,8185,1482,5875,7255,3210,6531,5991,6930, 87,9459,9566,····
10001,4867,5875,····,····,····,····,····,····,····,····,····,····,····,····,····, 556
11000, 645,5573,8815,7935,8586,4327,····,1037,7596,6827,3717,8642,9853,8470,3549,4239
11001,····,····,····,····,····,····,8774,····,····,····,····,····,····,····,····,····
12000,4658,9373,7810, 707,7024,2071,7768,7940,5482,5641,7628,3714,5999,7547,1883,9796
在我用 替换debug = True
后debug = False
,程序可以像这样运行
$ python3 merge.py f01 f02 f03 f04 f05 f06 f07 f08 f09 f10 f11 f12 f13 f14 f15 f16 OUT
$ cat OUT
Interval,IOPS01,IOPS02,IOPS03,IOPS04,IOPS05,IOPS06,IOPS07,IOPS08,IOPS09,IOPS10,IOPS11,IOPS12,IOPS13,IOPS14,IOPS15,IOPS16
1000,4407, 889,9569,2956, 477,5845,3812,3079,,2571,4994,8863,6394,5339,8280,9311
1001,,,,,,,,,1400,,,,,,,
2000,,2695,3053,1856, 379,8128,9600,,5880, 283,9501, 709,1964,5371,7242,3195
2001,9109,,,,,,,3949,,,,,,,,
3000,5822,6446,,1194, 876,4995,9210,6827,7535,,5323,8030,6204,3628,3730,5248
3001,,,2767,,,,,,,9906,,,,,,
4000, 839,3552,1773,8294,6993,5490,6852,4005,2986,6665,1455,9396,5018,7121,6750,9895
5000,2368,9335,9547,8177,9450,1309,8387,2812,3305,5156,2432,3797,9101,4568,8340,
5001,,,,,,,,,,,,,,,,1974
6000,,5301,8338,6410,,9253,,6004,2937,5152,5064, 524, 495,8768,7844,3358
6001,7887,,,,2574,,2603,,,,,,,,,
7000,6232,,,7026,, 697,8578,5188,8142,,7734, 801,4680,5435,5463,6949
7001,,5081,3861,,4263,,,,,7595,,,,,,
8000,1485,,3417,1571,3966, 17,1137,4574,5340,6980,3265,9292,3717,1917,7696, 293
8001,,1437,,,,,,,,,,,,,,
9000,1144,3773,4759,2693,8873,6382,2138,6598,3396,2195,5617,6769,7784,8677,6985,3160
10000,,,4708,8404,8185,1482,5875,7255,3210,6531,5991,6930, 87,9459,9566,
10001,4867,5875,,,,,,,,,,,,,, 556
11000, 645,5573,8815,7935,8586,4327,,1037,7596,6827,3717,8642,9853,8470,3549,4239
11001,,,,,,,8774,,,,,,,,,
12000,4658,9373,7810, 707,7024,2071,7768,7940,5482,5641,7628,3714,5999,7547,1883,9796
$