test.py 7.44 KB
Newer Older
German Leon's avatar
German Leon committed
1
#!/usr/bin/python
German Leon's avatar
German Leon committed
2
3
4
5
6
7
8
9
10
import sys, os, subprocess, random, time, datetime
import pexpect, re
import os.path
from os import path
import fnmatch
import serial
import config
from config import *

German Leon's avatar
German Leon committed
11
12
13
14
if inject:
  from config_inject import *


German Leon's avatar
German Leon committed
15
16
17
def configurate_serie():
	#os.system("sudo stop ttyS0")
	Puerto=0
German Leon's avatar
German Leon committed
18
	Puerto="/dev/ttyS0"
German Leon's avatar
German Leon committed
19
20
21
22
23
24
25
26
27
28
29
	try:
	   serie = serial.Serial(Puerto, 115200)
	   serie.timeout=1;
   
	except serial.SerialException:
	   #-- Error al abrir el puerto serie
	   sys.stderr.write("Error al abrir puerto (%s)\n" % str(Puerto))
	return serie

#mantener vivos los watdog softwre y hardware
def KeepWatchdog():
German Leon's avatar
German Leon committed
30
31
32
33
   cab=os.environ.get("MYHOME")
   if (cab==None):
        cab=os.environ.get("HOME")
   os.system(cab+"/bin/refresh_watchdog > watchsoft.test")
German Leon's avatar
German Leon committed
34
35
   os.system("touch watchsoft.test")

German Leon's avatar
German Leon committed
36
37
38
39
40
41
42
# Comprime los ficheros con extension .tar
def gzipFiles():
  listOfFiles = os.listdir('results')
  for entry in listOfFiles:
      if fnmatch.fnmatch(entry, "*.tar") or fnmatch.fnmatch(entry, "*.log"):
           os.system("gzip -f results/" + entry)

German Leon's avatar
German Leon committed
43
def writeOutput(serie, output, elapsedTime, outputFileName, fileName):
German Leon's avatar
German Leon committed
44
45
      if verbose:
        print("\n***Elapsed time: %s ms. ***\n" % (elapsedTime))
German Leon's avatar
German Leon committed
46
47
      serie.write("\n***Elapsed time: %s ms. ***\n" % (elapsedTime))

German Leon's avatar
German Leon committed
48
#      if verbose:
German Leon's avatar
German Leon committed
49
#        print("%s " % (output) ) 
German Leon's avatar
German Leon committed
50
#      serie.write("%s " % (output) )
German Leon's avatar
German Leon committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

      output += "\n\n***Elapsed time: " + `elapsedTime` + " ms. ***\n\n"
      outputFile = open(outputFileName, "w")
      outputFile.write(output)
      outputFile.flush()
      outputFile.close()
#      os.system("tar -rvf results/" + fileName + ".tar " + outputFileName)
#      os.system("rm " + outputFileName)

def runBenchmark(fiIni, fileName, tIni):
  
  serie=configurate_serie()
  
  numTimeouts = 0

  if (totalFiNumber == -1): # bucle infinito
      forever = True
  else:
      forever = False

German Leon's avatar
German Leon committed
71
72
  if log:
    stdLogFile = open("results/"+fileName+"_"+tIni+".log", "w")
German Leon's avatar
German Leon committed
73
74
75
76
77
78
79
80

  fiNumber = fiIni - 1
  while forever or ( (fiNumber+1) < totalFiNumber+1):

    fiNumber = fiNumber + 1

    KeepWatchdog()

German Leon's avatar
German Leon committed
81
82
83
84
85
86
87
88
#    if (fiNumber > 100):
#        tout = 90
#    else:
#        tout = timeout

    if inject:
      generateFiPointFromGroupedLines()

German Leon's avatar
German Leon committed
89
90
91
92
93
94
95
96
97
98
99
    try:

      x = datetime.datetime.now()
      outputFileName =  "results/"+fileName + x.strftime("_%H%M%S.err")
      ti = x.strftime("%m/%d/%Y %H:%M:%S")

      # Measure execution time
      startTime = time.time()

#      os.system("sudo /home/ubuntu/bin/gpufreq-info")
#      os.system("cpufreq-info | grep 'current CPU' | cut -d's' -f2 | cut -d'(' -f1")
German Leon's avatar
German Leon committed
100
101
#      print("./"+outputExeFile+" "+inputParameters)
      p = pexpect.spawn("./"+outputExeFile+" "+inputParameters, timeout=timeout)
German Leon's avatar
German Leon committed
102
103
104
105
106
107
108
109
110
111

      p.expect(pexpect.EOF)

      # Process elspased time
      elapsedTime = 1000*(time.time() - startTime)

      p.close()

      if (p.exitstatus == 0):
        if verbose:
German Leon's avatar
German Leon committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
          print("(%d) %s; PASS in %.2f ms." % (fiNumber, ti, elapsedTime) )
          #print(p.before)
          sys.stdout.flush()
        if log:
          stdLogFile.write("(%d) %s; PASS in %.2f ms.\n" % (fiNumber, ti, elapsedTime) )
#          stdLogFile.write("%s\n" % (p.before) )
          stdLogFile.flush()
#        serie.write("(%d) %s; PASS in %.2f ms.\n\r" % (fiNumber, ti, elapsedTime) )
#        serie.write("%s\n" % (p.before) )
#        serie.flush()
      elif (p.exitstatus == 4): 
        if verbose:
          print("(%d) %s; CRASH in %.2f ms." % (fiNumber, ti, elapsedTime) )
          sys.stdout.flush()
        if log:
          stdLogFile.write("(%d) %s; CRASH in %.2f ms.; %s\n" % (fiNumber, ti, elapsedTime, outputFileName) )
#          stdLogFile.write("%s\n" % (p.before) )
          stdLogFile.flush()
#        serie.write("(%d) %s; CRASH in %.2f ms.; %s\n\r" % (fiNumber, ti, elapsedTime, outputFileName) )
#        serie.flush()
        if log:
          writeOutput(serie, p.before, elapsedTime, outputFileName, fileName+"_"+tIni)
German Leon's avatar
German Leon committed
134
135
      elif (p.exitstatus == 1): 
        if verbose:
German Leon's avatar
German Leon committed
136
137
138
139
140
141
142
143
144
145
146
          print("(%d) %s; SDC in %.2f ms." % (fiNumber, ti, elapsedTime) )
#          print(p.before)
          sys.stdout.flush()
        if log:
          stdLogFile.write("(%d) %s; SDC in %.2f ms.; %s\n" % (fiNumber, ti, elapsedTime, outputFileName) )
#          stdLogFile.write("%s\n" % (p.before) )
          stdLogFile.flush()
#        serie.write("(%d) %s; SDC in %.2f ms.; %s\n\r" % (fiNumber, ti, elapsedTime, outputFileName) )
#        serie.flush()
        if log:
          writeOutput(serie, p.before, elapsedTime, outputFileName, fileName+"_"+tIni)
German Leon's avatar
German Leon committed
147
148
149
      else:
        if verbose:
          print("(%d) %s; Exit status: %d in %.2f ms." % (fiNumber, ti, p.exitstatus, elapsedTime) )
German Leon's avatar
German Leon committed
150
151
152
153
154
155
156
157
158
          sys.stdout.flush()
        if log:
          stdLogFile.write("(%d) %s; Exit status: %d in %.2f ms.\n" % (fiNumber, ti, p.exitstatus, elapsedTime) )
#          stdLogFile.write("%s\n" % (p.before) )
          stdLogFile.flush()
#        serie.write("(%d) %s; Exit status: %d in %.2f ms.\n\r" % (fiNumber, ti, p.exitstatus, elapsedTime) )
#        serie.flush()
        if log:
          writeOutput(serie, p.before, elapsedTime, outputFileName, fileName+"_"+tIni)
German Leon's avatar
German Leon committed
159
160
161
162
163
164
165
166
167
168
169


      numTimeouts = 0

    except KeyboardInterrupt:
      print("KeyboardInterrupt")
      sys.stdout.flush()
      lastFile = open("lastFiNumber", "w")
      lastFile.write("%d" % fiNumber)
      lastFile.flush()
      lastFile.close()
German Leon's avatar
German Leon committed
170
171
172
      if log:
        stdLogFile.flush()
        stdLogFile.close()
German Leon's avatar
German Leon committed
173
174
175
176
177
178
179
180
181
182
183
      sys.exit(2)

    except pexpect.EOF, e:
      print("Fin de archivo")

    except pexpect.TIMEOUT, e:

      terminated = p.terminate(force=True)
      elapsedTime = time.time() - startTime

      if verbose:
German Leon's avatar
German Leon committed
184
185
186
187
188
189
190
191
192
193
        print("(%d) %s; Timeout after %.2f ms." % (fiNumber, ti, elapsedTime) )
        sys.stdout.flush()
      if log:
        stdLogFile.write("(%d) %s; Timeout after %.2f ms.\n" % (fiNumber, ti, elapsedTime) )
        stdLogFile.write("%s\n" % (p.before) )
        stdLogFile.flush() 
      serie.write("(%d) %s; Timeout after %.2f ms.\n\r" % (fiNumber, ti, elapsedTime) )
      serie.flush()

#      sys.stdout.flush()
German Leon's avatar
German Leon committed
194
195
196
197
198
199
200
201
202
203
204
205
#      stdLogFile.close()

      numTimeouts += 1
      # Si se dan 3 timeouts seguidos, suponemoe que no se recuperara y rearrancamos la Jetson

      lastFile = open("lastFiNumber", "w")

      if (numTimeouts == 3): 
        fiNumber = fiNumber - 2 # contamos los tres ultimos timeouts consecutivos como un solo error no recuperable
        lastFile.write("%d" % fiNumber)
        lastFile.flush() 
        lastFile.close()
German Leon's avatar
German Leon committed
206
        time.sleep(1)
German Leon's avatar
German Leon committed
207
208
209
210
211
212
213
214
215
216
217
218
219
        sys.exit(3)
      else:
        lastFile.write("%d" % fiNumber)
        lastFile.flush() 
        lastFile.close()

    finally:

      lastFile = open("lastFiNumber", "w")
      lastFile.write("%d" % fiNumber)
      lastFile.flush() 
      lastFile.close()

German Leon's avatar
German Leon committed
220
#      time.sleep(1)
German Leon's avatar
German Leon committed
221
222

  # end while
German Leon's avatar
German Leon committed
223
224
  if log:
    stdLogFile.close()
German Leon's avatar
German Leon committed
225
226
227
228

#end runBenchmark

def main(argv):
German Leon's avatar
German Leon committed
229
  
German Leon's avatar
German Leon committed
230
231
232
233
  fileName = sys.argv[1]
  fiIni = int( sys.argv[2] )
  tIni = sys.argv[3]

German Leon's avatar
German Leon committed
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#  if ( os.path.exists('lastFiNumber') ):
#    lastFile = open("lastFiNumber", "r")
#    fiIni = int( lastFile.read() ) + 1
#    lastFile.close()
#  else:
#    fiIni = 1

#  gzipFiles()  

  if inject:
    groupProfileLines()

#  x = datetime.datetime.now()
#  fileName =  bmName + x.strftime("_%H%M%S")

German Leon's avatar
German Leon committed
249
250
251
  runBenchmark(fiIni, fileName, tIni)


German Leon's avatar
German Leon committed
252
253
254
255
#  os.system("rm lastFiNumber")
#  os.system("mv /home/ubuntu/startup_script.sh /home/ubuntu/startup_script_noactivo.sh")


German Leon's avatar
German Leon committed
256
257
258
259
##############################################################################
#main()
if __name__ == "__main__":
  main(sys.argv)