#!/usr/bin/python
import shutil
import os, sys
from os import *
from sys import *
import time

OPEN_ARGS= O_RDWR | O_CREAT

class TestFiles:
            
        def __init__(self, result):
                #We direclty open the result file
                self.fichier=file(result, "w")
                #We will perform a measure each 100 files
                self.base=100 
                #number of files when starting the test
                self.n=0
                #path to use to create files.
                self.path='mnt/max_file_test/'
                #self.path='mnt/'


        def create_files(self, i):
                j=0
                filename=""
                
                while j<self.base:
                    #I play a little with decimal numerotation for naming ;)
                    n=self.base*i+j
                    filename=self.path+str(n)
                    #Open with OPEN_ARGS
                    fd=open(filename,O_RDWR | O_CREAT)
                    fsync(fd)
                    close(fd)   
                    j+=1
                #save the number of files reached
                self.n=n
                return 0
                

#########################################################
# The real test                
#########################################################                
#The idea is to generate 
#1/ as many empty files as possible
#2/ measure the time to create each files
#3/ To speed up the process we do not perform a measure between all files creation 
#   but each 100files (default  self.base  value).
#########################################################       
        def start(self, max):
            i=0
            while self.base*i<max:
                if(1):
                    #mesure time to create 100 files 
                    touch_t0=time.time()
                    self.create_files(i)
                    touch_t1=time.time()
                #Save results in a gnuplot compatible format.
                self.fichier.write("Time to create "+str(self.n)+" files: "+str(touch_t1-touch_t0)+" seconds")
                self.fichier.write("\n")
                #flush results... 
                self.fichier.flush()
                i+=1
            self.fichier.close()
            print "Number of files created "+str(self.n)
            print "-----------------------------------------"



        def clean(self,max):
        #Ignore max for now : destroy all files
            i=0
            while i<max:
                f=self.path+'/'+str(i)
                remove(f)
                i+=1


 
def usage():
        print "test maximum mount available for NFSv4"
        print "-h, --help   :this message"
        print "max=<value>  :maximum exported filesystem to test"
        print "--clean      :clean tests only"
        print "--test       :test only"
        print " "
        print "send bugs to Vincent ROQUETA: vincent.roqueta@ext.bull.net"



def main():
        global fichier
        args=sys.argv[1:]
        max=1024
        test=True
        clean=True
        for a in args:
            if a in ("-h", "--help"):
                usage()
                sys.exit(1)
            if a[:3]=="max":
                v=a.split("=")
                max=int(v[1])
                continue
            if a=="--clean":
                test=False
                continue
            if a=="--test":
                clean=False
                continue

        t=TestFiles("test_files_orw")

        if(test):
            t.start(max)
            print "test completed"
        if(clean):
            t.clean(max)
            print "Cleanup completed"
                



main()

