Is This Code Efficient?

Results 1 to 3 of 3

Thread: Is This Code Efficient?

  1. #1
    Join Date
    Dec 1969

    Default Is This Code Efficient?

    I&#039;m using asp to ping a dynamically generated list of IP addresses. Heres some rough pseudocode:<BR><BR>Open database, select hosts to ping<BR>Start Loop through recordset<BR> for each host in recordset call IsHostAlive Function<BR> in IsHostAlive Function<BR> create filesystem object and WScript.Shell object<BR> Ping selected host and save results to text file<BR> Open text file and return true or false based on contents <BR> close text file and filesystem object<BR>Loop<BR><BR>As you can see, I&#039;m creating the filesystemobject, and the shell instance, and i&#039;m opening, reading from and closing the textfile for every field in the recordset. This seems like a lot of overhead. But is it really, or will this not place much of a load on the server? Do I need to make any changes?<BR><BR>here is the actual code:<BR>
    <BR>&#060;%<BR>OPTION EXPLICIT <BR>&#039;MAKE STYLESHEET FOR ERROR MESSSAGES <BR>&#039;make it so have to use dim<BR><BR>DIM objConn, strSQL, objRS<BR>&#039;OPEN DATABASE WITH HOST ADDRESSES IN IT<BR>Set objConn = Server.CreateObject("ADODB.Connection")  <BR>objConn.Open "TheLibrary"<BR>&#039;SELECT ALL HOSTS FROM TABLE<BR>strSQL = "SELECT Host FROM tblHosts"<BR>&#039;POPULATE RECORDSET<BR>Set objRS = objConn.Execute(strSQL)<BR><BR>&#039;FIRST MAKE SURE RECORDSET DIDNT RETURN EMPTY<BR>IF (objRS.EOF) THEN<BR>   Response.Write ("&#060;p class=""ErrorMessage""&#062; Error. Recordset Returned Empty.&#060;/p&#062;")<BR>   Response.End<BR>END IF<BR>&#039;LOOP THROUGH RECORDSET. CALL ISHOSTALIVE FUNCTION FOR EACH HOST ADDRESS<BR>DO WHILE NOT objRS.EOF<BR>   IF IsHostAlive(objRS("Host")) Then<BR>      Response.Write ("Host " & objRS("Host") & " is alive.&#060;br/ &#062;")<BR>   ELSE<BR>      Response.Write ("Host " & objRS("Host") & " is not responding.&#060;br/ &#062;")<BR>   END IF<BR>   objRS.MoveNext<BR>LOOP<BR>&#039;CLOSE CONNECTIONS AND SET TO NOTHING<BR>objConn.Close<BR>SET objConn = NOTHING<BR><BR><BR><BR>FUNCTION IsHostAlive(strHostAddress)<BR>&#039;FUNCTION TO RUN PING AND TEST IF HOSTS ARE ALIVE	<BR>	DIM RetCode, objFileSys, WShShell<BR>	DIM objTextFile, strTextBuffer, strFileName<BR>	<BR>	&#039;CREATE FILESYTEMOBJECT INSTANCE<BR>	SET objFileSys = Server.CreateObject("Scripting.FileSystemObject")<BR>	&#039;GET TEMPORARY FILENAME<BR>	strFileName = objFileSys.GetTempName<BR>	&#039;CREATE SHELL INSTANCE<BR>	SET WShShell = Server.CreateObject("WScript.Shell")<BR>	&#039;RUN BAT FILE WITH PROVIDED PARAMETER strHostAddress<BR>    RetCode = WShShell.Run("C:InetpubwwwrootTheLibraryDoPing.bat " & strHostAddress & " " & strFileName, 1, True)<BR>	&#039;CHECK AND SEE IF THERE WAS AN ERROR RUNNING IT<BR>&#039;	IF RetCode = 0 THEN<BR>      &#039;THERE WERE NO ERRORS IF GOT HERE<BR>&#039;	ELSE<BR>	  &#039;PROVIDE ERROR MESSAGE IF COMMAND DIDNT EXECUTE PROPERLY<BR>&#039;      Response.Write ("&#060;p class=""ErrorMessage""&#062;Errors occured while trying to run Ping.&#060;/p&#062;")<BR>&#039;      Response.End<BR>&#039;	END IF<BR>	&#039;OPEN TEMPORARY FILE<BR>	SET objTextFile = objFileSys.OpenTextFile("C:InetpubwwwrootTheLibrary\" & strFileName & ".txt", 1)<BR>	&#039;READ IN RESULTS OF PING<BR>	strTextBuffer = objTextFile.ReadAll<BR>	<BR>	&#039;CHECK RESULTS FOR HOST UNREACHABLE OR 100 PERCENT LOSS	OR COULD NOT FIND HOST<BR>	IF (InStr(strTextBuffer, "host unreachable")&#062;0) OR (InStr(strTextBuffer, "100% loss") &#062; 0) OR (InStr(strTextBuffer, "could not find host") &#062; 0)THEN<BR>		IsHostAlive = FALSE<BR>    ELSE<BR>        &#039;RETURN TRUE IF GOT A REPLY<BR>		IsHostAlive = TRUE<BR>    END IF<BR>	&#039;CLOSE TEMPORARY TEXT FILE<BR>	objTextFile.Close<BR>	&#039;DELETE TEMPORARY TEXT FILE<BR>	objFileSys.DeleteFile "C:InetpubwwwrootTheLibrary\" & strFileName & ".txt"<BR>	&#039;SET OBJECTS TO NOTHING<BR>	SET objTextFile = NOTHING<BR>	SET objFileSys  = NOTHING<BR><BR>END FUNCTION &#039;END CHECKHOST<BR><BR>%&#062;<BR>

  2. #2
    Join Date
    Dec 1969

    Default Not particularly.

    From the fact that you can create a shell object, I&#039;m gathering that you&#039;re not on a commercial shared server somewhere, in which case, it would be far more efficient to install a component like ASPPing (which is free) and use it instead. That would be less of a hack than the batch file, and if should allow you to do away with the FSO calls entirely. <BR><BR><BR><BR>

  3. #3
    Join Date
    Dec 1969

    Default Hard to be less efficient...

    ...I would hazard to guess.<BR><BR>For example, you&#039;d be *MUCH* better off passing the *list* of host addresses to your shell-based code and having it do all the pings before returning with a list of (say?) the dead ones.<BR><BR>Instantiating the Shell object is the real performance killer in this. Yeah, it&#039;s pointless to create the FSO more than once per page, but compared to the overhead of the Shell that&#039;s almost nothing.<BR><BR>This code is *CRYING* out for a custom component. Really.<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts