Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tarwn's ASP Benchmark Script Error and Update 1

Status
Not open for further replies.

Tarwn

Programmer
Mar 20, 2001
5,787
US
Hey guys, for anyone that downloaded my little benchmark scripts, I just found an error that could be a problem. The script was originally keeping track of the time for every run but when I instantianted the array I accidentally used the number of tests to instantiate both dimensions instead of the number of tests and the number of runs.
This error does not effect the results any and will only show up when you have fewer methods to test then 4. A corrected version can be found below.

I also added more detailed instructions and restructured it a little better so that modification would be easier.

-T


Code:
<%
'----------------------------------------------------------
' Tarwn's Basic Benchmark Script
'----------------------------------------------------------
' Directions:
'
' 1) Line 66, Line 68: 
'		Decide how many runs you want and how many 
'		tests per run. Make sure you have enough 
'		tests to get around the 16 millisecond 
'		round-off issue. Each set of tests (a run)
'		will be averaged for the results
' 2) Line 71:
'		Define a name for the benchmark results file
' 3) Line 73:
'		Define a name for the benchmark - this will
'		be displayed on the results
' 4) Line 75:
'		Fill in how many tests there will be (number
'		of method functions)
' 5) Line 85:
'		Define the titles for each method that will
'		be used in results display
' 6) Line 278 + num methods:
'		Define each method you will be testing using
'		the name Method# where # is the number of that
'		method
'----------------------------------------------------------
' Output:
'	You can receive the displayed results as either
'	formatted HTML or XML. This allows you to plug in
'	another application to pull the results in. If you
'	want XML simply include an attribute called SendXML
'	in either a Form Request or QueryString with any 
'	value. The presence of this attribute will make the
'	code output XML instead of the default HTML.
'----------------------------------------------------------
' ChangeLog:
' Tarwn - 01/21/2004 - Originally Created for testing simple
'						functions
' Tarwn - 08/16/2004 - Modified for text file output, XML 
'						output, etc
' Tarwn - 10/24/2004 - Array issue and notes corrected
'----------------------------------------------------------
' Copyright Info:
'	This script is free for re-use, modification, etc. for
'	non-commercial purposes. Contact Tarwn for permission
'	if you wish to use it in another fashion. 
'	Please feel free to redistribute the script provided
'	you list any changes you have made in the change log
'	section
'----------------------------------------------------------

Dim m_time()
Dim m_avg(), m_low(), m_high()
Dim m_title()
Dim s_time
Dim num_test_type
Dim benchmark_results_file, benchmark_title

' used to keep track of total time for the whole benchmark
Dim run_start
run_start = timer

'------------------ Test Variables ------------------------
	Dim num_tests, num_runs
	'num tests of method per run
	num_tests = 10000
	'num runs to average (run 0 is ignored)
	num_runs = 4
	'relative path for results output - will automagically
	'	create numbered copies if file already exists
	benchmark_results_file = "BasicResults.txt"
	'title for this benchmark
	benchmark_title = "Tarwn's Basic Benchmark"
	'number of method tests we have
	num_test_type = 3
'----------------------------------------------------------

	ReDim m_time(num_test_type,num_runs)
	ReDim m_avg(num_test_type)
	ReDim m_low(num_test_type)
	ReDim m_high(num_test_type)
	ReDim m_title(num_test_type)

'------------------ Method Test Titles --------------------
	m_title(1) = "Some Method"
	m_title(2) = "Some Other Method"
	m_title(3) = "Yet Another Method"
'----------------------------------------------------------


'------------------ Test Functions ------------------------
' These are the functions to handle the actual 
' method tests

Function Method1()
	Dim a
	a = 1 + 1
End Function

Function Method2()
	Dim a
	a = 1
	a = a * 2
End Function

Function Method3()
	Dim a
	a = 1
	a = a + a
End Function
'----------------------------------------------------------

'================== Workhorse Code ========================
' Runs the test code and displays results

Dim run_counter, method_counter

'initialize low, high, total time values
For method_counter = 1 to num_test_type
	m_time(method_counter,0) = 0
	m_low(method_counter) = 100000
	m_high(method_counter) = 0
Next

'run the tests
Dim temp_time
For run_counter = 0 to num_runs
	For method_counter = 1 to num_test_type
		temp_time = Runtest(method_counter, num_tests)	'get test results
		If temp_time = -1 Then
			Response.Write "Error Occurred: mc=" & method_counter & " rc=" & run_counter
			Response.End
		End If

		'Throw out the first run, it was a warm up
		If run_counter > 0 Then
			m_time(method_counter,0) = m_time(method_counter,0) + temp_time	'add time to total time for this method
			m_time(method_counter,run_counter) = temp_time 'get this exact run time
			If temp_time < m_low(method_counter) Then m_low(method_counter) = temp_time	'if this is lowest, substitute
			If temp_time > m_high(method_counter) Then m_high(method_counter) = temp_time	'if this is highest, substitute
			If run_counter = num_runs Then m_avg(method_counter) = m_time(method_counter,0)/num_runs	'if last run then calc average
	
			temp_time = -1	'insurance on next loop
		End If
	Next
Next

'order the results
Dim o_results()
Dim i, j, t
ReDim o_results(num_test_type)
For i = 1 to num_test_type
	o_results(i) = i
Next

For i = num_test_type to 1 step -1
	For j = 1 to i
		If m_avg(o_results(i)) < m_avg(o_results(j)) Then
			t = o_results(j)
			o_results(j) = o_results(i)
			o_results(i) = t
		End If
	Next
Next

If Request("SendXML") <> "" Then
	Response.ContentType = "text/xml"
	Response.Write "<benchmark>"
	Response.Write "<name>" & benchmark_title & "</name>"
	Response.Write "<results runs=""" & num_runs & """ loops=""" & num_tests & """ run_date=""" & Now() & """>"

	For method_counter = 1 to num_test_type
		Response.Write "<test method=""" & o_results(method_counter) & """>"
		Response.Write "<title>" & m_title(o_results(method_counter)) & "</title>"
		Response.Write "<average>" & FormatNumber(m_avg(o_results(method_counter)),3) & "</average>"
		Response.Write "<low>" & FormatNumber(m_low(o_results(method_counter)),3) & "</low>"
		Response.Write "<high>" & FormatNumber(m_high(o_results(method_counter)),3) & "</high>"
		For run_counter = 1 to num_runs
			Response.Write "<result number=""" & num_runs & """>" & FormatNumber(m_time(o_results(method_counter),run_counter),3) & "</result>"
		Next
		Response.Write "</test>"
	Next
	Response.Write "</results></benchmark>"

	Response.End
Else
	%>
	<html>
	<head>
	<style>
	table{
		font-size: 9pt;
		font-family: verdana;
		width: 600px;
	}
	th{
		background-color: #eeeeee;
	}
	td{
		border-bottom: #dddddd;
	}
	</style>
	</head>
	<body>
	<table style="display:inline;"><tr><th colspan="5">Benchmark Results - Sorted By Average Time</th></tr>
	<tr><th>Method</th><th>Name</th><th>Average</th><th>Low</th><th>High</th></tr>
	<%
	For method_counter = 1 to num_test_type
		Response.Write "<tr><td>Method #" & o_results(method_counter) & "</td>"
		Response.Write "<td>" & m_title(o_results(method_counter)) & "</td>"
		Response.Write "<td>" & FormatNumber(m_avg(o_results(method_counter)),3) & "</td>"
		Response.Write "<td>" & FormatNumber(m_low(o_results(method_counter)),3) & "</td>"
		Response.Write "<td>" & FormatNumber(m_high(o_results(method_counter)),3) & "</td></tr>"
	Next
	%>
	</table><br><br>
	<table style="display:inline;"><tr><th colspan="5">Benchmark Results - UnSorted</th></tr>
	<tr><th>Method</th><th>Name</th><th>Average</th><th>Low</th><th>High</th></tr>
	<%
	For method_counter = 1 to num_test_type
		Response.Write "<tr><td>Method #" & method_counter & "</td>"
		Response.Write "<td>" & m_title(method_counter) & "</td>"
		Response.Write "<td>" & FormatNumber(m_avg(method_counter),3) & "</td>"
		Response.Write "<td>" & FormatNumber(m_low(method_counter),3) & "</td>"
		Response.Write "<td>" & FormatNumber(m_high(method_counter),3) & "</td></tr>"
	Next
	Response.Write "</table><br>"
	%>
	<br>
	<br>
	<table style="display:inline;"><tr><th colspan="<%=5 + num_runs%>">Full Benchmark Results - Sorted By Average Time</th></tr>
	<tr><th>Method</th><th>Name</th><th>Average</th><th>Low</th><th>High</th>
	<%
	For run_counter = 1 to num_runs
		Response.Write "<th>Run " & run_counter & "</th>"
	Next
	Response.Write "</tr>"

	For method_counter = 1 to num_test_type
		Response.Write "<tr><td>Method #" & o_results(method_counter) & "</td>"
		Response.Write "<td>" & m_title(o_results(method_counter)) & "</td>"
		Response.Write "<td>" & FormatNumber(m_avg(o_results(method_counter)),3) & "</td>"
		Response.Write "<td>" & FormatNumber(m_low(o_results(method_counter)),3) & "</td>"
		Response.Write "<td>" & FormatNumber(m_high(o_results(method_counter)),3) & "</td>"
		For run_counter = 1 to num_runs
			Response.Write "<td>" & FormatNumber(m_time(o_results(method_counter),run_counter),3) & "</td>"
		Next
		Response.Write "</tr>"
	Next
	Response.Write "</table>"
	Response.Write "<br>Times are averages from " & num_runs & " runs of " & num_tests & " loops for each method."
	Response.Write "<br>Total Test Run Time: " & timer - run_start & "s"

End If

'----- Write Results to File
Dim fso, fil, fil_ctr
Set fso = Server.CreateObject("Scripting.FileSystemObject")
fil_ctr = 1
If fso.FileExists(Server.MapPath(benchmark_results_file)) Then
	Do Until Not fso.FileExists(Server.MapPath(Replace(benchmark_results_file,".","(" & fil_ctr & ").")))
		fil_ctr = fil_ctr + 1
	Loop
	benchmark_results_file = Replace(benchmark_results_file,".","(" & fil_ctr & ").")
End If

Set fil = fso.CreateTextFile(Server.MapPath(benchmark_results_file))
fil.WriteLine benchmark_title
fil.WriteLine "Date: " & Now()
fil.WriteLine "Number of runs: " & num_runs
fil.WriteLine "Loops Per Run: " & num_tests
fil.WriteLine "Total Elapsed: " & timer-run_start & " seconds"
fil.WriteLine ""

fil.Write "Method	Name	Average	Low	High"
For run_counter = 1 to num_runs
	fil.Write "	Run " & run_counter
Next
fil.Write vbCrLf

For method_counter = 1 to num_test_type
	fil.Write "Method #" & o_results(method_counter) & vbTab & _
					m_title(o_results(method_counter)) & vbTab & _
					FormatNumber(m_avg(o_results(method_counter)),3) & vbTab & _
					FormatNumber(m_low(o_results(method_counter)),3) & vbTab & _
					FormatNumber(m_high(o_results(method_counter)),3)
	For run_counter = 1 to num_runs
		fil.Write vbTab  & FormatNumber(m_time(o_results(method_counter),run_counter),3)
	Next
	fil.Write vbCrLf
Next

fil.Write vbCrLf

fil.Close
Set fil = Nothing
Set fso = Nothing

'------------------ Clock Functions -------------
' Usually these would be in a language that has
' better millisecond resolution, but shouldn't
' be necessary in this case
Function StartClock()
	s_time = timer
End Function

Function StopClock()
	StopClock = timer - s_time
End Function

'------------------ RunTest Function ------------
' Allows us to make one call and run any test
Function RunTest(testNum, loopCount)
	Dim loop_ctr

	RunTest = 0

	If testNum > 0 And testNum <= num_test_type Then
		For loop_ctr = 1 to loopCount
			StartClock
			Execute("Method" & testNum & "()")
			RunTest = RunTest + StopClock
			Response.Clear
		Next
	Else
		RunTest = -1
	End If
End Function

%>

barcode_1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top