Rapid Load Test data generation using python
I have a requirement that I need to create a set of SQL queries to insert a load of test data into a test database for a load test. The problem I have is that some of the data I need to dynamically change and some of the data has incremental patterns that I need to create in certain columns.
One approach that I have used is to create an SQL template command and swap in values inbetween placeholders. I have a rules engine - which is essentially a switch statement that I can use to fill in depending on the position of the placeholder. Its not too complex, and its not too clever. I need something simple to debug when generating large volumes of data.
At the moment all the values are hardcoded, but its a python script - its not going to take a lot to modify the script itself to create the data. I could have used external configuration files, or pass in a load of parametres but then I need to create more code to deal with this.
So the code looks like
class SQLGenerator():
def countSwappedValues(self):
def __init__(self):
self.SQLCommand = "INSERT INTO TABLE_FOOBAR VALUES ({0},{1});"
def run(self):
for columnPos in range (0,self.countSwappedValues()):
value = self.ruleEngine(columnPos,12)
self.swapValues(value,columnPos)
print (self.SQLCommand)
def ruleEngine(self,columnPos,number):
return {
0: self.generateLeadingZeros(5,number),
1: "Account"+self.generateLeadingZeros(5,number),
}[columnPos]
leftBracket = self.SQLCommand.count('{')
rightBracket = self.SQLCommand.count('}')
if (leftBracket == rightBracket):
return leftBracket
else:
return 0 def swapValues(self, value,index):
replaceStr = "{"+str(index)+"}"
self.SQLCommand= self.SQLCommand.replace(replaceStr,value) def generateLeadingZeros(self, noLeadingZeros, number):
result = ""
for x in range (0,noLeadingZeros - len(str(number))):
result = result + "0" result = result + str(number)
return results = SQLGenerator()
s.run()What I need to do - is wrap around a loop for the number of items I need to generate, substitute more complex SQL and change the rules engine for that SQL and output each generated SQL line to a file. Where I print out the SQL is where I would write to file.
Its not an amazing all conquering solution, and there is scope for extending this to run from external configuration but since its fairly lightweight - it should be quick to use and debug. Python is a good tool for rapidly solving some issues such as data generation.