<commit_before>proc ReadCPUTimeTable { } {
    global CPUTimeTable
    global env

    if { [catch {set VTK_HISTORY_PATH $env(VTK_HISTORY_PATH)}] != 0} return

    source CPUTimeTable.tcl
    SetCPUTimeTestArchAndKit
    return
}

proc SetCPUTimeTestArchAndKit { } {
    global CPUTimeTestKit
    global CPUTimeTestArch
    global env

    if { [catch {set VTK_HISTORY_PATH $env(VTK_HISTORY_PATH)}] != 0} return

    ## Find the real path - cd there, then do a pwd to get the
    ## actual path and not the link.
    set currentDir [pwd]
    cd $VTK_HISTORY_PATH
    set testDir [pwd]
    cd $currentDir

    ## Keep track of the parts of the path used to construct the
    ## path for previous days
    set CPUTimeTestKit [file tail $testDir]
    set CPUTimeTestArch [file tail [file dirname $testDir]]

}

proc ComputeLimits { theList } {
    global env

    if { [catch {set VTK_HISTORY_PATH $env(VTK_HISTORY_PATH)}] != 0} return

    ## Compute the mean - use the most recent 20 samples
    set sum 0
    set validCount 0
    foreach n $theList {
	if { $n != -1 } {
	    set sum [expr $sum + $n]
	    incr validCount
	}
	if { $validCount > 20 } break
    }

    if { $validCount > 0 } {
	set mean [expr $sum / $validCount]
    } else {
	return [list 0 0]
    }

    ## Compute the standard deviation - use the most recent 20 samples
    set stddev 0
    set validCount 0
    foreach n $theList {
	if { $n != -1 } {
	    set stddev [expr $stddev + (($n - $mean) * ($n - $mean))]
	    incr validCount
	}
	if { $validCount > 20 } break
    }
    if { $validCount > 1 && $stddev > 0 } {
        set stddev [expr sqrt( $stddev / ($validCount-1) )]
    } else {
        set stddev 0
    }

    ## take the larger of the standard deviation or 0.01 times the mean
    if { [expr 0.01 * $mean] > $stddev } {
        set stddev [expr 0.01 * $mean]
    }

    ## make sure this is at least 0.01
    if { $stddev < 0.01 } {
	set stddev 0.01
    }

    ## set the limit to be 3 standard deviations around the mean - this is
    ## guaranteed to get at least 3% above and 3% below and allow at least
    ## a range of 0.06 seconds.
    set lowLimit  [expr $mean - 3*$stddev]
    set highLimit [expr $mean + 3*$stddev]

    return [list $lowLimit $highLimit]
}


proc CheckTime { theTest {currentTime -1}} {
    global CPUTimeTable
    global env
    global CPUTimeTestKit
    global CPUTimeTestArch

    if { [catch {set VTK_HISTORY_PATH $env(VTK_HISTORY_PATH)}] != 0} return

    SetCPUTimeTestArchAndKit

    ## Get the name of the test
    set theTest [file rootname $theTest]

    ## Get the list of times for this test out of the time table
    set timelist ""
    catch { set timelist $CPUTimeTable(${theTest}_${CPUTimeTestArch}_${CPUTimeTestKit}) }

    ## remove ending -1's
    set i [expr [llength $timelist] - 1]
    set done 0
    while { $i >= 0 && $done == 0 } {
	if { [lindex $timelist $i] == "-1" } {
	    set timelist [lrange $timelist 0 [expr $i - 1]]
	    incr i -1
	} else {
	    set done 1
	}
    }
    
    ## If there is nothing in the list, then this is a new test
    if { $timelist == "" } { return "Warning: New Test" }

    ## If a current time was not passed in, use the most recent time in
    ## the list as the current time, and then remove this element from the
    ## list
    if { $currentTime == -1 } {
	set currentTime [lindex $timelist 0]
	set timelist [lrange $timelist 1 end]
    }

    ## If there is nothing in the list, then this is a new test
    if { $timelist == "" } { return "Warning: New Test" }

    ## Find the limits
    set limits [ComputeLimits $timelist]
    set low  [lindex $limits 0]
    set high [lindex $limits 1]

    set retCode ""

    ## If we have less than 5 time samples, this is a recently added test.
    ## If we can't compute a low and a high, then this must be a new test
    ## (this condition should not occur?). Otherwise, test if it is
    ## consistently slower or faster, or just slower or faster for today
    if { [llength $timelist] < 5 } {
	set retCode "Warning: Recently Added Test"
    } elseif { $low == 0 && $high == 0 } {
	set retCode "Warning: New Test"
    } elseif { $currentTime < $low } {
	set tmplist [lrange $timelist 1 end]
	set limits [ComputeLimits $tmplist]
	set low  [lindex $limits 0]
	if { [lindex $timelist 0] < $low } {
	    set retCode "Warning: Consistently Faster CPU Time"
	} else {
	    set retCode "Warning: Faster CPU Time"
	}
    } elseif { $currentTime > $high } {
	set tmplist [lrange $timelist 1 end]
	set limits [ComputeLimits $tmplist]
	set high [lindex $limits 1]
	if { [lindex $timelist 0] > $high } {
	    set retCode "Warning: Consistently Slower CPU Time"
	} else {
	    set retCode "Warning: Slower CPU Time"
	}
    } 

    ## If we haven't set a return code yet, then look back for 4 days
    ## to see if anything significant has occurred. For an event to
    ## be reported as a recent increase or decrease in time, it must
    ## be sustained for 2 days (a single spike will be ignored except
    ## for the day on which it occurred).
    set count 0
    set total_count 0
    set limit [expr [llength $timelist] - 2]
    set testTime $currentTime
    while { $retCode == "" && $count < 4 && $total_count < $limit} {
	set prevTestTime $testTime
	set testTime [lindex $timelist 0]
        if { $testTime != -1 } {

            set timelist [lrange $timelist 1 end]

            set limits [ComputeLimits $timelist]<commit_msg>Fixed bug when determining "constistently" time.<commit_after>proc ReadCPUTimeTable { } {
    global CPUTimeTable
    global env

    if { [catch {set VTK_HISTORY_PATH $env(VTK_HISTORY_PATH)}] != 0} return

    source CPUTimeTable.tcl
    SetCPUTimeTestArchAndKit
    return
}

proc SetCPUTimeTestArchAndKit { } {
    global CPUTimeTestKit
    global CPUTimeTestArch
    global env

    if { [catch {set VTK_HISTORY_PATH $env(VTK_HISTORY_PATH)}] != 0} return

    ## Find the real path - cd there, then do a pwd to get the
    ## actual path and not the link.
    set currentDir [pwd]
    cd $VTK_HISTORY_PATH
    set testDir [pwd]
    cd $currentDir

    ## Keep track of the parts of the path used to construct the
    ## path for previous days
    set CPUTimeTestKit [file tail $testDir]
    set CPUTimeTestArch [file tail [file dirname $testDir]]

}

proc ComputeLimits { theList } {
    global env

    if { [catch {set VTK_HISTORY_PATH $env(VTK_HISTORY_PATH)}] != 0} return

    ## Compute the mean - use the most recent 20 samples
    set sum 0
    set validCount 0
    foreach n $theList {
	if { $n != -1 } {
	    set sum [expr $sum + $n]
	    incr validCount
	}
	if { $validCount > 20 } break
    }

    if { $validCount > 0 } {
	set mean [expr $sum / $validCount]
    } else {
	return [list 0 0]
    }

    ## Compute the standard deviation - use the most recent 20 samples
    set stddev 0
    set validCount 0
    foreach n $theList {
	if { $n != -1 } {
	    set stddev [expr $stddev + (($n - $mean) * ($n - $mean))]
	    incr validCount
	}
	if { $validCount > 20 } break
    }
    if { $validCount > 1 && $stddev > 0 } {
        set stddev [expr sqrt( $stddev / ($validCount-1) )]
    } else {
        set stddev 0
    }

    ## take the larger of the standard deviation or 0.01 times the mean
    if { [expr 0.01 * $mean] > $stddev } {
        set stddev [expr 0.01 * $mean]
    }

    ## make sure this is at least 0.01
    if { $stddev < 0.01 } {
	set stddev 0.01
    }

    ## set the limit to be 3 standard deviations around the mean - this is
    ## guaranteed to get at least 3% above and 3% below and allow at least
    ## a range of 0.06 seconds.
    set lowLimit  [expr $mean - 3*$stddev]
    set highLimit [expr $mean + 3*$stddev]

    return [list $lowLimit $highLimit]
}


proc CheckTime { theTest {currentTime -1}} {
    global CPUTimeTable
    global env
    global CPUTimeTestKit
    global CPUTimeTestArch

    if { [catch {set VTK_HISTORY_PATH $env(VTK_HISTORY_PATH)}] != 0} return

    SetCPUTimeTestArchAndKit

    ## Get the name of the test
    set theTest [file rootname $theTest]

    ## Get the list of times for this test out of the time table
    set timelist ""
    catch { set timelist $CPUTimeTable(${theTest}_${CPUTimeTestArch}_${CPUTimeTestKit}) }

    ## remove ending -1's
    set i [expr [llength $timelist] - 1]
    set done 0
    while { $i >= 0 && $done == 0 } {
	if { [lindex $timelist $i] == "-1" } {
	    set timelist [lrange $timelist 0 [expr $i - 1]]
	    incr i -1
	} else {
	    set done 1
	}
    }
    
    ## If there is nothing in the list, then this is a new test
    if { $timelist == "" } { return "Warning: New Test" }

    ## If a current time was not passed in, use the most recent time in
    ## the list as the current time, and then remove this element from the
    ## list
    if { $currentTime == -1 } {
	set currentTime [lindex $timelist 0]
	set timelist [lrange $timelist 1 end]
    }

    ## If there is nothing in the list, then this is a new test
    if { $timelist == "" } { return "Warning: New Test" }

    ## Find the limits
    set limits [ComputeLimits $timelist]
    set low  [lindex $limits 0]
    set high [lindex $limits 1]

    set retCode ""

    ## If we have less than 5 time samples, this is a recently added test.
    ## If we can't compute a low and a high, then this must be a new test
    ## (this condition should not occur?). Otherwise, test if it is
    ## consistently slower or faster, or just slower or faster for today
    if { [llength $timelist] < 5 } {
	set retCode "Warning: Recently Added Test"
    } elseif { $low == 0 && $high == 0 } {
	set retCode "Warning: New Test"
    } else {
	set tmplist [lrange $timelist 1 end]
	set limits [ComputeLimits $tmplist]
	set newlow  [lindex $limits 0]
	set newhigh [lindex $limits 1]
	if { $currentTime < $newlow && [lindex $timelist 0] < $newlow } {
	    set retCode "Warning: Consistently Faster CPU Time"
	} elseif { $currentTime > $newhigh && [lindex $timelist 0] > $newhigh } {
	    set retCode "Warning: Consistently Slower CPU Time"
	} elseif { $currentTime < $low } {
	    set retCode "Warning: Faster CPU Time"
	} elseif { $currentTime > $high } {
	    set retCode "Warning: Slower CPU Time"
	}
    } 

    ## If we haven't set a return code yet, then look back for 4 days
    ## to see if anything significant has occurred. For an event to
    ## be reported as a recent increase or decrease in time, it must
    ## be sustained for 2 days (a single spike will be ignored except
    ## for the day on which it occurred).
    set count 0
    set total_count 0
    set limit [expr [llength $timelist] - 2]
    set testTime $currentTime
    while { $retCode == "" && $count < 4 && $total_count < $limit} {
	set prevTestTime $testTime
	set testTime [lindex $timelist 0]
        if { $testTime != -1 } {

            set timelist [lrange $timelist 1 end]

            set limits [ComputeLimits $timelist]
{'fraction_non_alphanumeric': 0.10030283564283748, 'fraction_numerical': 0.015692392401578416, 'mean_word_length': 2.5955130320026396, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1203200', 'n_tokens_mistral': 3838, 'n_tokens_neox': 3560, 'n_words': 1413}