Changeset 857

Show
Ignore:
Timestamp:
12/11/09 05:47:53 (4 years ago)
Author:
atorf
Message:

NXCProfiler: First working version with very simple coverage report…

Location:
branches/atorf/personal playground/NXCProfiler/Analyzer
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • branches/atorf/personal playground/NXCProfiler/Analyzer/AnalyzeNXCProfile.py

    r856 r857  
    4141Vars.verbose = False 
    4242 
     43 
     44Consts.CODENOTPROFILED = 0 
     45Consts.CODEDIDNTRUN    = 1 
     46Consts.CODEPROFILED    = 2 
    4347 
    4448verbosePrint = None  # this is gonne be a method! 
     
    136140#def  
    137141 
     142def writeStringToFile(filename, str): 
     143    try: 
     144        f = file(filename, "w") 
     145        f.write(str) 
     146    except (IOError, OSError): 
     147        print "Could not open output file for writing or write to it" 
     148        f.close() 
     149        sys.exit(1) 
     150    #end try 
     151 
     152    f.close() 
     153#end def 
    138154 
    139155 
     
    159175            elif tmp.startswith("PROFILER_ENDSECTION"): 
    160176                val = int(tmp.partition("(")[2].partition(")")[0]) 
    161                 out.append((i, "beginsection", val)) 
     177                out.append((i, "endsection", val)) 
    162178                verbosePrint('   . found "endsection(%d)" in line %d' % (val, i)) 
    163179            #end if 
     
    169185#end def 
    170186 
     187def findProfilerCommandLine(lines, cmd): 
     188    lineno = -1 
     189    for p in lines: 
     190        if p[1] == cmd: 
     191            lineno = p[0] 
     192            break 
     193        #end if 
     194    #end if 
     195    if lineno == -1: 
     196        print "Profiler command %s not found in source file." % cmd 
     197        sys.exit(1) 
     198    #end if 
     199     
     200    return lineno 
     201#end def 
     202 
     203def generateCoverageLines(sourcelines, proflines, stats): 
     204    coverage = [Consts.CODENOTPROFILED for tmp in sourcelines]; 
     205    # for all profiler commands 
     206    for (i, p) in enumerate(proflines): 
     207        # look for profiled sections 
     208        if p[1] == 'beginsection': 
     209            # find out if code was executed 
     210            verbosePrint('   . checking section %s, ExecutionCount = %d' % (p[2], stats.ExecutionCount[p[2]])) 
     211            if stats.ExecutionCount[p[2]] > 0: 
     212                val = Consts.CODEPROFILED 
     213            else: 
     214                val = Consts.CODEDIDNTRUN 
     215            #end if 
     216             
     217            #NOTE BAD 
     218            endline = -1 
     219            for nextp in proflines: 
     220                if nextp[1] == 'endsection': 
     221                    if nextp[2] == p[2]: 
     222                        endline = nextp[0] 
     223                        break 
     224            if endline == -1: 
     225                print "Warning: Didn't find matching profiler command endsection for a certain startsection!" 
     226                         
     227            #end if 
     228            # set all lines to the end... 
     229            for j in range(p[0], endline+1): 
     230                coverage[j] = val 
     231            #end for 
     232        #end if 
     233    return coverage 
    171234 
    172235def generateHTMLHeader(sourcefile): 
     
    188251 
    189252 
    190 def writeStringToFile(filename, str): 
    191     try: 
    192         f = file(filename, "w") 
    193         f.write(str) 
    194     except (IOError, OSError): 
    195         print "Could not open output file for writing or write to it" 
    196         f.close() 
    197         sys.exit(1) 
    198     #end try 
    199  
    200     f.close() 
    201 #end def 
     253 
     254 
     255def generateHTMLCodeTable(sourcelines, proflines, stats, coverage): 
     256    parts = []; 
     257     
     258    startline = findProfilerCommandLine(proflines, "start") 
     259    stopline = findProfilerCommandLine(proflines, "stop") 
     260     
     261     
     262    parts.append(HTMLSnippets.getHTMLCodeStart()) 
     263     
     264    for (i, line) in enumerate(sourcelines): 
     265        escapedCode = line.rstrip().replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace('"', '&quot;'); 
     266        escapedCode = escapedCode.replace('\t', '    ') 
     267        escapedCode = escapedCode.replace(' ', '&nbsp;') 
     268        if i == startline or i == stopline: 
     269            tmp = '<span class="profilercmd">' +  escapedCode + "</span><br>\n" 
     270        else: 
     271            if coverage[i] == Consts.CODENOTPROFILED: 
     272                tmp = '<span class="codenotprofiled">' +  escapedCode + "</span><br>\n" 
     273            elif coverage[i] == Consts.CODEDIDNTRUN: 
     274                tmp = '<span class="codedidntrun">' +  escapedCode + "</span><br>\n" 
     275            else: 
     276                tmp = '<span class="codeprofiled">' +  escapedCode + "</span><br>\n" 
     277            #end if 
     278        #end if 
     279        parts.append(tmp) 
     280    #end for 
     281 
     282    parts.append("</td></tr>") 
     283     
     284    return ''.join(parts) 
    202285 
    203286 
     
    206289def doTheAnalysis(statsfile, sourcefile, outfile): 
    207290    stats       = parseProfileFile(statsfile) 
    208     sourceLines = readSourceFile(sourcefile) 
    209     proflines   = findProfilerCommands(sourceLines) 
     291    sourcelines = readSourceFile(sourcefile) 
     292    proflines   = findProfilerCommands(sourcelines) 
     293    coverage    = generateCoverageLines(sourcelines, proflines, stats) 
    210294     
    211295    verbosePrint(" - Generating HTML output file") 
     
    214298    HTMLparts.append(generateHTMLFileSummary(sourcefile, statsfile, stats)) 
    215299    HTMLparts.append(generateHTMLRuntimeSummary(stats)) 
     300    HTMLparts.append(generateHTMLCodeTable(sourcelines, proflines, stats, coverage)) 
    216301    HTMLparts.append(HTMLSnippets.getHTMLFooter()) 
    217302     
    218303     
    219304    writeStringToFile(outfile, ''.join(HTMLparts)) 
    220     verbosePrint("%s written." % outfile) 
     305    verbosePrint(" - %s written." % outfile) 
     306     
    221307     
    222308#end def 
  • branches/atorf/personal playground/NXCProfiler/Analyzer/HTMLSnippets.py

    r856 r857  
    2020        <style type="text/css"> 
    2121            body { font-family:'Verdana'; } 
    22             th { font-size:14pt; font-style:bold; } 
     22            code { font-size:10pt; } 
     23             
     24            span { font-size:10pt; font-family:'Courier New' } 
     25            .codedidntrun { color:#A0A020; } 
     26            .codenotprofiled { color:#000000; } 
     27            .codeprofiled { color:#000060; } 
     28            .profilercmd { font-weight:bolder; color:#500000; } 
     29            .comment { color:#A9D668; } 
     30             
     31            th { font-size:12pt; font-weight:bold; } 
    2332            .summaryth { background-color:#A9D668} 
    2433            .codeth { background-color:#7DA2D1} 
     
    2938            td { font-size:11pt; } 
    3039            .summarytd { background-color:#DEFFBD} 
    31             .codedidntruntd { background-color:#CBDDFF} 
    32             .codeprofiledtd { background-color:#CBDDFF} 
    33             .codenotprofiledtd { background-color:#CBDDFF} 
     40            .codedidntruntd { background-color:#EFEFEF} 
     41            .codeprofiledtd { background-color:#EFEFEF} 
     42            .codenotprofiledtd { background-color:#EFEFEF} 
    3443             
    3544            .standardtd { background-color:#CBDDFF} 
     
    7887            <td class="summarytd"> 
    7988                <ul>                 
    80                   <li><code>Total program runtime: %###.2f seconds</code> 
    81                   <li><code>Profiled part runtime: %###.2f seconds</code> 
     89                  <li><code>Total program runtime: %###0.2f seconds</code> 
     90                  <li><code>Profiled part runtime: %###0.2f seconds</code> 
    8291                </ul> 
    8392            </td> 
    8493        </tr> 
     94    </table> 
    8595    ''' 
    8696#end def 
     97 
     98 
     99def getHTMLCodeStart(): 
     100    return '''<table border="0" cellpadding="20" cellspacing="8" width="750"> 
     101        <tr> 
     102            <th class="codeth">Profiled source code</th> 
     103        </tr> 
     104       <tr> 
     105            <td class="codenotprofiledtd"> 
     106             
     107        ''' 
  • branches/atorf/personal playground/NXCProfiler/Analyzer/sample.nxc

    r855 r857  
    217217 
    218218// --- Done 
    219      
     219 
     220    /* DIRTY DIRTY HACK TEST 
     221       this code was not profiled 
     222       this is a quick hack... bad bad... 
     223       PROFILER_BEGINSECTION(6);  
     224       // something 
     225       // something 
     226       // this should never ever be executed 
     227       // different color highlighting? 
     228       PROFILER_ENDSECTION(6);  
     229    */ 
    220230 
    221231}//end task