Software tips

Random programming tips and tricks

Improve system performance using batch scripts

clock April 7, 2008 01:32 by author notjordan

*Note: The batch scripts below were created for Windows XP.
Many people will usually end up using one computer to handle most everything, so you can imagine how much software gets installed and how sluggish it can get. With everything that starts automatically my laptop easily pushes 800MB of RAM usage after startup.

Many processes that are enabled by default are not used all the time; in fact I rarely use many of the ones on my laptop. One way to trim down the load on a computer without having to completely remove software is to setup scripts to manually start/stop processes as needed.

Step 1: Identify categories to group rarely used processes under
Categories for my computer:

  • IIS, ASP.NET development
  • PHP, apache development
  • Cold fusion development
  • Proxy services (Tor/Privoxy)

 

Step 2: Find executables and processes that are started automatically for each category
This step is a bit of trial and error; focus on a few at a time because finding dependencies for a large number of processes would not be fun.
-Set each relevant service to “Manual” startup.
-Remove each relevant executable from automatically starting up.

Step 3: Create a batch script for each category
Place commands to start/stop each process and executable from step 2 into a batch file.
The following are provided as an example, they may need modification for other installs.

IIS.bat:

@echo off
set x="%1"
set y="%2"
IF %x%=="start" (
net start "SQL Server (SQLEXPRESS)"
net start "SQL Server Browser"
net start "SQL Server FullText Search (SQLEXPRESS)"
net start "SQL Server VSS Writer"
net start "Simple Mail Transfer Protocol (SMTP)"
net start "World Wide Web Publishing"
net start "IIS Admin"
net start ".NET Runtime Optimization Service v2.0.50727_X86"
) ELSE IF %x%=="stop" (
net stop "SQL Server (SQLEXPRESS)"
net stop "SQL Server Browser"
net stop "SQL Server FullText Search (SQLEXPRESS)"
net stop "SQL Server VSS Writer"
net stop "Simple Mail Transfer Protocol (SMTP)"
net stop "World Wide Web Publishing"
net stop "IIS Admin"
net stop ".NET Runtime Optimization Service v2.0.50727_X86"
) ELSE (
echo Valid Options:
echo start
echo stop
);
IF %y%=="nopause" (
echo Done with apache.bat
) ELSE (
pause
)

apache.bat:

@echo off
set x="%1"
set y="%2"
IF %x%=="start" (
start "httpd" httpd.exe
start "ApacheMonitor" /Dc:\"Program Files"\"Apache Software Foundation"\Apache2.2\bin /B ApacheMonitor.exe
net start "mysql"
) ELSE IF %x%=="stop" (
tskill ApacheMonitor
tskill httpd
net stop "mysql"
) ELSE (
echo Valid Options:
echo start
echo stop
);
IF %y%=="nopause" (
echo Done with apache.bat
) ELSE (
pause
)

cf.bat:

@echo off
set x="%1"
set y="%2"
IF %x%=="start" (
net start "ColdFusion 8 .NET Service"
net start "ColdFusion 8 Application Server"
net start "ColdFusion 8 ODBC Agent"
net start "ColdFusion 8 ODBC Server"
) ELSE IF %x%=="stop" (
net stop "ColdFusion 8 .NET Service"
net stop "ColdFusion 8 Application Server"
net stop "ColdFusion 8 ODBC Agent"
net stop "ColdFusion 8 ODBC Server"
) ELSE (
echo Valid Options:
echo start
echo stop
);
IF %y%=="nopause" (
echo Done with coldfusion
) ELSE (
pause
)

tor.bat:

@echo off
set x="%1"
set y="%2"
IF %x%=="start" (
start "vidalia" /Dc:\"Program Files"\vidalia /B vidalia.exe
start "privoxy" /Dc:\"Program Files"\privoxy /B privoxy.exe
start "pg2" /Dc:\"Program Files"\PeerGuardian2 /B pg2.exe
) ELSE IF %x%=="stop" (
tskill privoxy
tskill vidalia
tskill tor
tskill pg2
) ELSE (
echo Valid Options:
echo start
echo stop
);
IF %y%=="nopause" (
echo Done with apache.bat
) ELSE (
pause
)

Step 4: Create scripts to start and stop all at once
Add a reference to each of the above batch files into a master batch that can be used for starting/stopping all at once.
startall.bat:

@echo off
start /B apache start nopause
start /B tor start nopause
start /B iis start nopause
start /B cf start nopause
pause

killall.bat:

@echo off
start /B apache stop nopause
start /B tor stop nopause
start /B iis stop nopause
start /B cf stop nopause
pause

Place these batch files into a folder that is within your PATH so they can be executed using Start->Run. I prefer keeping these and other random command tools in a central location that has been added to my PATH variable.
C:\cmdtools

Start->Run commands to START services:

iis start
apache start
cf start
tor start
startall

Start->Run commands to STOP services:

iis stop
apache stop
cf stop
tor stop
killall

The difference is quite dramatic on my laptop; RAM usage drops 418MB, with 19 fewer processes running at startup.

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Performance Tip-Use StringBuilder

clock November 10, 2007 05:22 by author notjordan

String concatenation is not efficient in .NET because Strings are immutable. Changes made to a String cause a new copy of the String to be created, resulting in unnecessary overhead for each operation and extra garbage waiting to be collected. This can result in very noticeable performance loss especially when looping.
Make use of the StringBuilder class when concatenating a String instead of adding to a String object. There is some overhead when creating a StringBuilder, but this overhead is offset after about five operations. Make a habit of using the StringBuilder class when five or more operations are expected to be done on the String.

Below is a short speed test to see how dramatic of a difference there can be when looping.

Code-behind:

Dim dateStart As DateTime
Dim dateEnd As DateTime
Dim timeDiff As TimeSpan

'Run for regular string concat-------------
Dim strRegular As String = ""
dateStart = Date.Now
For i As Integer = 0 To 15000
strRegular = strRegular + i.ToString
Next
dateEnd = Date.Now
timeDiff = dateEnd - dateStart
Me.LitSRegular.Text = timeDiff.ToString
'------------------------------------------
'Run for stringbuilder---------------------
Dim strbTest As New StringBuilder()
dateStart = Date.Now
For i As Integer = 0 To 15000
strbTest.Append(i.ToString)
Next
dateEnd = Date.Now
timeDiff = dateEnd - dateStart
Me.LitSBuilder.Text = timeDiff.ToString
'------------------------------------------

Front-end:

Regular string concatenation: <asp:Literal ID="LitSRegular" runat="server"></asp:Literal>
<br />
StringBuilder: <asp:Literal ID="LitSBuilder" runat="server"></asp:Literal>

Running the above code will provide similar results to the following:
Regular string concatenation: 00:00:01.8281250
StringBuilder: 00:00:00.0156250

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Avoid using QueueUserWorkItem in ASP.NET

clock October 22, 2007 18:42 by author notjordan

When you have a method to run that can process independent of the page being displayed it is tempting to kick the method off asynchronously with QueueUserWorkItem to not slow down the page as shown below:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
QueueUserWorkItem(AddressOf DoHeavyProcessing, Me.Request.QueryString("i"))
End Sub

Private Sub DoHeavyProcessing(ByVal param As Object)
'do stuff
End Sub

While this is a simple way to do the extra processing without slowing the page load, doing this in ASP.NET does have very bad side-effects. QueueUserWorkItem will queue the method in the same thread pool that ASP.NET uses, limiting the ability of the web server, unhandled exceptions will also cause unexpected results as well.
Avoid this even if your site gets little traffic.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Disable Session State

clock October 5, 2007 22:55 by author notjordan

Not every web application needs to use Session state. Disable Session state for sites that do not make use of it. The performance improvement may be very minimum, but cutting down extra processing for every request is always good.

Session state can be disabled in the web.config file:
<configuration>
   <system.web>
     <sessionState mode="Off" />
   </system.web>
</configuration>

Session state can also be disabled on individual pages by modifying the "EnableSessionState" page directive on each page. 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


ASP.NET 2.0 ViewState Info

clock October 5, 2007 04:56 by author notjordan

ViewState is ASP.NET's way of storing the state of a page to retain values between requests. ViewState is enabled by default for every control, which will add a lot of overhead to requests. Therefore it should be disabled whenever it is not absolutely necessary by setting the “EnableViewState=false” on each control.
ViewState is not limited to storing the state of controls, it can also be used to store and retrieve other values. Call Me.ViewState.Add(“name”, “value”) to store a value in ViewState to be used later on the page.

ViewState Security:
The information stored in the ViewState field can be easily read with a ViewState viewer because it is not encrypted by default. If you plan to store sensitive information in ViewState then you will need to encrypt it. Note that this will add extra overhead on the server, so only use when necessary. Encryption can be enabled for the entire site by setting “<pages viewStateEncryptionMode="Always">” in the web.config, or on individual pages by adding “ViewStateEncryptionMode=Always" in each page directive.

ViewState and Custom Controls:
Developers consuming a custom control have the option of disabling ViewState on that control. Therefore if ViewState is required for the control to work then you should use ControlState instead since this cannot be disabled by the consuming page.

*note: If the ViewState string is too long for a single hidden field (MaxPageStateFieldLength property), it will be split into as many multiple files as needed.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Search

Categories


Tags



Blogroll

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

Sign in