赞
踩
为了进行压力测试需要做几份大数据文件,根据表定义不同生成的文件的列数和类型也不同,分为
RandomName:随机长度的字符串最大长度通过参数指定
RandomIndex:打乱顺序的自增长序列
RandomInt:随机整数值最大值通过参数指定
RandomUF:随机浮点数最大值通过参数指定
RandomDay,RandomTime:随机日期和日期时间,起始和截止通过参数指定.
RandomChoice:随机指定CodeList值
详细代码如下:
1 importtime2 importbinascii3 importos4 importsys5 from random importshuffle6 from numpy importrandom7
8 #Code Value
9 COD3 = ['AED', 'AFN', 'ALL', 'AMD', 'ANG', 'AOA']10
11 classRandomObject:12 intCount = 10000
13 __listRand =[]14 __genRandFun =object15
16 def __init__(self, fun):17 self.__genRandFun =fun18
19 def __str__(self):20 if len(self.__listRand) ==0:21 self.__listRand = self.__genRandFun()22 return self.__listRand.pop()23
24 def __int__(self):25 if len(self.__listRand) ==0:26 self.__listRand = self.__genRandFun()27 return self.__listRand.pop()28
29 def __float__(self):30 if len(self.__listRand) ==0:31 self.__listRand = self.__genRandFun()32 return self.__listRand.pop()33
34
35 classRandomName(RandomObject):36 def __init__(self, intLen=1):37 self.__intLen =intLen38 RandomObject.__init__(self, self.__genRandomValue)39
40 def __genRandomValue(self):41 randList =[]42 for rand in random.randint(1, self.__intLen + 1, RandomObject.intCount):43 randList.append(binascii.hexlify(os.urandom(rand)).decode()[0:rand])44 returnrandList45
46
47 classRandomChoice(RandomObject):48 def __init__(self, list):49 self.__list =list50 RandomObject.__init__(self, self.__genRandomValue)51
52 def __genRandomValue(self):53 return random.choice(self.__list, RandomObject.intCount).tolist()54
55
56 classRandomInt(RandomObject):57 __intLen =058
59 def __init__(self, intLen):60 self.__intLen =intLen61 RandomObject.__init__(self, self.__genRandomValue)62
63 def __genRandomValue(self):64 return random.randint(1, self.__intLen + 1, RandomObject.intCount).tolist()65
66
67 classRandomUF(RandomObject):68 def __init__(self, intLen):69 self.__intLen =intLen70 RandomObject.__init__(self, self.__genRandomValue)71
72 def __genRandomValue(self):73 return random.uniform(1, self.__intLen + 1, RandomObject.intCount).tolist()74
75
76 classRandomDay(RandomObject):77 def __init__(self, startDay=20000101, endDay=20200108):78 self.__startDay =startDay79 self.__endDay =endDay80 RandomObject.__init__(self, self.__genRandomValue)81
82 def __genRandomValue(self):83 randList =[]84 mDate = random.randint(self.__startDay, self.__endDay, RandomObject.intCount)85 for i in range(0, len(mDate) - 1):86 y, m = divmod(mDate[i], 10000)87 m, d = divmod(m, 100)88 randList.append("%04d%02d%02d" % (y, (m % 12) + 1, (d % 28) + 1))89 returnrandList90
91
92 classRandomTime(RandomObject):93 __startDay =094 __endDay =095
96 def __init__(self, startDay=20000101, endDay=20200108):97 self.__startDay =startDay98 self.__endDay =endDay99 RandomObject.__init__(self, self.__genRandomValue)100
101 def __genRandomValue(self):102 randList =[]103 mHrs = random.randint(0, 23, RandomObject.intCount)104 mMints = random.randint(0, 59, RandomObject.intCount)105 mSends = random.randint(0, 59, RandomObject.intCount)106
107 mDate = random.randint(self.__startDay, self.__endDay, RandomObject.intCount)108 for i in range(0, len(mDate) - 1):109 y, m = divmod(mDate[i], 10000)110 m, d = divmod(m, 100)111 randList.append(112 "%04d/%02d/%02d" % (y, (m % 12) + 1, (d % 28) + 1) + "%02d:%02d:%02d" %(113 mHrs[i], mMints[i], mSends[i]))114 returnrandList115
116
117 classRandomIndex(RandomObject):118 def __init__(self, startCount, ranCount):119 self.__startCount =int(startCount)120 self.__ranCount =int(ranCount)121 RandomObject.__init__(self, self.__genRandomIndex)122
123 def __genRandomIndex(self):124 ranList = range(self.__startCount, self.__startCount + self.__ranCount)125 ranList =list(ranList)126 shuffle(ranList)127 returnranList128
129
130 def genDataBase1(fileName='default.csv', iStart=1, dataCount=10):131 #Inital Random Class
132 randIdx =RandomIndex(iStart, dataCount)133 randNm19 = RandomName(19)134 randDay_20120601_20170629 = RandomDay(20120601, 20170629)135 randUF999999999999999 = RandomUF(999)136 randINT = RandomInt(2147483647)137 randChr_COD3 =RandomChoice(COD3)138 randTime_20160101_20171231 = RandomTime(20160101, 20171231)139
140 outp = open(fileName, 'w')141 iStart =int(iStart)142 dataCount =int(dataCount)143 i = iStart - 1
144 while i < dataCount + iStart - 1:145 #Format String
146 m_format = "'%05i','%s','%i','%0.5f','%s','%s','%s'\n"
147 #Value Expression
148 m_value =(149 randIdx, randNm19, randINT, randUF999999999999999, randDay_20120601_20170629, randTime_20160101_20171231,150 randChr_COD3)151 mLine = m_format %m_value152 outp.write(mLine)153 i += 1
154 outp.close()155
156
157 if __name__ == "__main__":158 random.seed()159 start =time.time()160 if len(sys.argv) == 4:161 genDataBase1(sys.argv[1], sys.argv[2], sys.argv[3])162 else:163 genDataBase1()164 end =time.time()165 print('use time:%d' % (end - start))
其中#Codelist #Intial Class #Format #Expression List可根据表定义不同自行更改
生成测试数据样式如下:
'00007','348a19be800a10b','1346619294','688.98200','20140214','2016/04/13 19:48:38','AOA'
'00002','f0','739566893','521.15793','20120708','2016/01/07 14:43:36','AFN'
'00009','ff09deb3d7368d2c509','1066446464','35.11855','20131124','2017/02/09 12:33:06','ALL'
'00005','e','1609587893','852.05709','20160303','2016/08/15 13:02:22','ALL'
'00008','b8ea04f21b414','1369157176','122.26536','20120107','2016/02/11 17:57:54','AFN'
'00003','e5bb989508afca6e4b5','1249235396','695.97509','20140610','2016/11/23 01:12:15','AFN'
'00006','5bbcefa3','143724217','751.23029','20140606','2016/06/02 01:43:51','AMD'
'00004','5aa1e19d3cfedb2','1802189343','174.93201','20150616','2016/09/07 10:34:18','ANG'
'00010','a9e40e481535fa04','752374554','716.81998','20120123','2017/01/09 22:22:51','AOA'
'00001','eafbb5ed8','1437209638','637.82241','20150204','2016/11/13 00:34:35','ANG'
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。