Skip to main content

Posts

Showing posts from January, 2013

Python lists : performance or resource usage

Lists are native part of the Python language and this part makes programming easy and speedy. But every Moon has a dark side and I would like to add some light to it. Problem of the list is heavy resource's usage. Everyone should keep in mind this during coding. Simple example from python tutorial: myfile = open("myfile.txt") myfile.readlines() Python opens file and creates a list from each line in it. Simple script below provides some information about executing speed and memory usage: #!/usr/bin/python import datetime import resource currenttime = datetime.datetime.now() print "="*20 print "Creating a file " print "="*20 myfile = open("textfile.txt", "w") simplerange = xrange(10000000) try: for i in simplerange: myfile.write(unicode(datetime.datetime.now())) myfile.write('\n') finally: myfile.close() timespend = datetime.datetime.now()- currenttime print timespend print "=&

Small python script for monitoring MySQL performance

I have few services which use MySQL as database server. I would like to have information about load in PNG  image  or  in Cacti app. MySQL   has  performance information at 'SHOW STATUS' command. Values  which  are monitored :   threads_running, threads_connected, thread_cached, slow_queries  Of course,  it is really easy to add more variables. Connection to MySQL is accomplished by MySQLdb  module. Typical example of usage is below : import MySQLdb mydb = MySQLdb.connect(host = 'hostname', user = 'username', password = 'secret', database = 'mysatabase' ) mycursor = mydb.cursor() mycursor.execute('SQL command') sqlresult = cur.fetchall() Storing data in rrd file is aviable via rrdtools package. This one is present in debian and Centos OS. example of creating file is below: import rrdtool rrdtool.create("myfile.rrd" , "DS:value1:data