Package Ganga :: Package Utility :: Package external :: Package ARDAMDClient :: Module diskutilsTest
[hide private]
[frames] | no frames]

Source Code for Module Ganga.Utility.external.ARDAMDClient.diskutilsTest

  1  ##!/usr/bin/env python 
  2  ################################################################################ 
  3  # Ganga Project. http://cern.ch/ganga 
  4  # 
  5  # $Id: diskutilsTest.py,v 1.1 2008-07-17 16:41:02 moscicki Exp $ 
  6  ################################################################################ 
  7   
  8  import sys, os 
  9  import re 
 10  import time 
 11  import tempfile 
 12   
 13  import diskutilsTest 
 14  _thisDir = os.path.dirname(diskutilsTest.__file__) 
 15  if not _thisDir: 
 16      _thisDir = os.getcwd() 
 17   
 18  from diskutils import * 
 19   
 20  #DEBUG = False 
 21  DEBUG = True 
 22   
 23  ################################################################################     
 24  # memory testing 
25 -def getmem():
26 """Gives memory used by the calling process in kb""" 27 ss = os.popen('pmap %d | tail -1'%os.getpid(), 'r').read() 28 if ss: 29 m = re.search(r'([0-9]*)K', ss) 30 if m: 31 return int(m.group(1))
32
33 -def _startText(ff, txt):
34 ff.write(txt) 35 t1 = time.time() 36 ff.write('operation started at %s \n' % time.ctime(t1)) 37 m1 = getmem() 38 return t1, m1
39
40 -def _endText(ff, t1, m1):
41 t2 = time.time() 42 m2 = getmem() 43 dt = t2 - t1 44 dm = m2 - m1 45 ff.write('operation finished at %s \n' % time.ctime(t2)) 46 ff.write('time used: %f seconds \n' % dt) 47 ff.write('-s->%f<-s-\n\n\n'% dt) 48 ff.write('memory used: %f Kb \n' % dm) 49 ff.write('-s->%f<-s-\n\n\n'% dm) 50 return dt, dm
51
52 -def runTest(NTEST, LEN, rootDir, output_dir):
53 if DEBUG: 54 print 'from runTest: rootDir %s, output_dir %s'%(rootDir, output_dir) 55 nn = tempfile.mktemp(suffix = '.test') 56 nn = os.path.join(output_dir, os.path.basename(nn)) 57 ff = file(nn, 'w') 58 lock = RLock() 59 try: 60 jj = [] 61 for i in range(NTEST): 62 j = [] 63 for k in range(10): 64 j.append(str(i) + '-' + str(k)) 65 j.append(LEN*'x'+str(i)) 66 jj.append(j) 67 68 t1, m1 = _startText(ff, 'registering %d jobs...' % NTEST) 69 if lock.acquire(): 70 try: 71 write(jj, 'aaaa') 72 finally: 73 lock.release() 74 dt, dm = _endText(ff, t1, m1) 75 if DEBUG: 76 print 'registering %d jobs...' % NTEST 77 print "--->command status", "OK" 78 print "(dt, dm):", (dt, dm), "\n" 79 80 81 t1, m1 = _startText(ff, 'retrieving info about ALL jobs') 82 if lock.acquire(): 83 try: 84 rjj = readLast('aaaa') 85 finally: 86 lock.release() 87 dt, dm = _endText(ff, t1, m1) 88 if DEBUG: 89 print 'retrieving info about ALL jobs' 90 #print "--->checkout jobs", len(rjj), map(lambda j: j[0], rjj) 91 print "(dt, dm):", (dt, dm), "\n" 92 93 94 t1, m1 = _startText(ff, 'registering %d jobs 10 times...' % NTEST) 95 for i in xrange(10): 96 if lock.acquire(): 97 try: 98 for j in jj: 99 j[-1] += '_'+str(i) 100 write(jj, 'aaaa'+str(i)) 101 finally: 102 lock.release() 103 dt, dm = _endText(ff, t1, m1) 104 if DEBUG: 105 print 'registering %d jobs 10 times...' % NTEST 106 print "--->command status", "OK" 107 print "(dt, dm):", (dt, dm), "\n" 108 109 110 t1, m1 = _startText(ff, 'retrieving info about ALL jobs 10 times') 111 for i in xrange(10): 112 if lock.acquire(): 113 try: 114 rjj = readLast('aaaa'+str(i), rjj) 115 finally: 116 lock.release() 117 dt, dm = _endText(ff, t1, m1) 118 if DEBUG: 119 print 'retrieving info about ALL jobs 10 times' 120 #print "--->checkout jobs", len(rjj), map(lambda j: j[0], rjj) 121 print "(dt, dm):", (dt, dm), "\n" 122 123 pid = os.getpid() 124 for j in jj: 125 j[0] = str(pid) 126 127 t1, m1 = _startText(ff, 'commiting %d jobs...' % NTEST) 128 if lock.acquire(): 129 try: 130 rjj = readLast('aaaa', rjj) 131 write(jj, 'aaaa') 132 finally: 133 lock.release() 134 dt, dm = _endText(ff, t1, m1) 135 if DEBUG: 136 print 'commiting %d jobs...' % NTEST 137 print "--->command status", "OK" 138 print "(dt, dm):", (dt, dm), "\n" 139 140 t1, m1 = _startText(ff, 'retrieving info about ALL jobs') 141 if lock.acquire(): 142 try: 143 rjj = readLast('aaaa', rjj) 144 finally: 145 lock.release() 146 dt, dm = _endText(ff, t1, m1) 147 if DEBUG: 148 print 'retrieving info about ALL jobs' 149 #print "--->checkout jobs", len(rjj), map(lambda j: j[0], rjj) 150 print "(dt, dm):", (dt, dm), "\n" 151 for j in rjj: 152 assert(rjj[0][0] == j[0]) 153 154 finally: 155 ff.close()
156 157 ################################################################################
158 -def NormPath(path):
159 if sys.platform == 'win32': 160 directory, file = os.path.split(path) 161 drive, tail = os.path.splitdrive(directory) 162 path_elem = tail.split(os.sep) 163 path = drive + os.sep 164 for i in range(len(path_elem)): 165 elem = path_elem[i] 166 if elem: 167 sub_elem = elem.split() 168 elem = sub_elem[0] 169 if len(elem) > 8 or len(sub_elem) > 1: 170 elem = elem[:6] + '~1' 171 path = os.path.join(path, elem) 172 173 path = os.path.join(path, file) 174 175 return path
176 177 ################################################################################ 178 if __name__ == '__main__': 179 NTEST = int(raw_input('Enter a number of job to test --->')) 180 NUSERS = int(raw_input('Enter a number of '"processes"' to test --->')) 181 LEN = int(raw_input('Enter a job length --->')) 182 OUTPUT = raw_input('Enter a name of output dir --->') 183 dname = 'users_' + str(NUSERS) + '__jobs_' + str(NTEST) 184 output_dir = os.path.join(os.getcwd(), OUTPUT, dname) 185 if not os.path.exists(output_dir): 186 os.makedirs(output_dir) 187 print "output dir is ", output_dir 188 rootDir = os.path.join(os.getcwd(), 'testdir', 'GangaTest', 'user') 189 190 python_path = NormPath(sys.executable) 191 i = 0 192 while i < NUSERS: 193 cmd = "import sys\nsys.path.append(\'%s\')\nfrom diskutilsTest import runTest\nrunTest(%d, %d, \'%s\',\'%s\')" % (_thisDir, NTEST, LEN, rootDir, output_dir) 194 if DEBUG: 195 print cmd 196 197 pid = os.spawnl(os.P_NOWAIT, python_path, python_path, "-c", cmd) 198 if DEBUG: 199 print "new user process started %d" % pid 200 i+=1 201