Thursday, September 16, 2010

C# XPATH vs LINQ

Use Xmlreader for parsing xml document.
Create an xml file as show below, but with 10000 contact elements called as “address_xw_n.xml” and make it available in D driver (d:\).
<AddressBook>
  <Contact>
    <Name>Person1</Name>
    <Address1><![CDATA[Number101]]></Address1>
    <Address2><![CDATA[Address501]]></Address2>
    <Zip>50001</Zip>
    <Phone><![CDATA[999991]]></Phone>
 </Contact>
</AddressBook>
The below code demonstrate the time take to query the above created xml file using xpath and LINQ for the Phone element value “999999996“.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Diagnostics;
using System.Xml.Linq;
namespace QueryXML
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch objSW = null;
            String strNormalFilePath;
            strNormalFilePath = @"d:\address_xw_n.xml";
            XmlDocument xmlNDoc;
            xmlNDoc = new XmlDocument();
            XDocument xNDoc;
            objSW = new Stopwatch();

            objSW.Reset();
            objSW.Start();
            //load the xml
            xmlNDoc.Load(strNormalFilePath);
            //xml query using xpath - Normal format
            objSW.Reset();
            objSW.Start();
            XmlNode xmlQNNode = xmlNDoc.SelectSingleNode(
                "/AddressBook/Contact/Phone[contains(text(),'999999996')]");
            //Console.WriteLine(xmlQNNode.Name +
            //  "-" + xmlQNNode.ChildNodes[0].Value);
            objSW.Stop();
            Console.WriteLine("XML query using xpath - Normal format");
            Console.WriteLine("TimeElapsed (Stopwatch float):{0}ms",
                objSW.Elapsed.TotalMilliseconds);
            Console.WriteLine("TimeElapsed (Stopwatch rounded):{0}ms",
                objSW.ElapsedMilliseconds);

            //xml query using LINQ - Normal format
            //Let us not include the conversion of XmlDocument to XDocument
            xNDoc = XDocument.Parse(xmlNDoc.OuterXml);
            objSW.Reset();
            objSW.Start();
            var linqNQuery = from p in
                                 xNDoc.Root.Elements("Contact").Elements("Phone")
                             where p.Value.Contains("999999996") select p;
            /*foreach (XElement e in linqNQuery)
            {
                Console.WriteLine(e.Name+"-"+e.Value);
            }*/
            objSW.Stop();
            Console.WriteLine("XML query using LINQ - Normal format");
            Console.WriteLine("TimeElapsed (Stopwatch float):{0}ms",
                objSW.Elapsed.TotalMilliseconds);
            Console.WriteLine("TimeElapsed (Stopwatch rounded):{0}ms",
                objSW.ElapsedMilliseconds);

            Console.ReadLine();
        }
    }
}

Output:
XML query using xpath - Normal format
TimeElapsed (Stopwatch float):20.0936ms
TimeElapsed (Stopwatch rounded):20ms
XML query using LINQ - Normal format
TimeElapsed (Stopwatch float):0.5082ms
TimeElapsed (Stopwatch rounded):0ms

Rule: Use LINQ for querying xml content.

Wednesday, September 15, 2010

MySQL performance optimization

Let us see some of the most simplest mysql optimizations and best practices,

Rue: Use MEDIUMINT for storing user Id for intranet applications. You can store -8388608 to 8388607 signed values or 0 to 16777215 unsigned values.

Rue: You can consider using BIGINT UNSIGNED for storing user Id for internet based applications. You can store -9223372036854775808 to 9223372036854775807 signed values or 0 to 18446744073709551615 unsigned values.

Rue: Always use UNSIGNED numeric types unless you want to store negative numbers.

Rue: Always have an primary key id column which is one of the INT types and UNSIGNED.

Rue: Use ENUM type over VARCHAR for storing predefined string values, e.g. "inactive", "inactive", "male", "female" etc.

Rue: Use DATE type instead of DATETIME if you want to store only date.

Rue: Use VARCHAR for variable string values.

Rue: Use INT UNSIGNED for IP4 values.

Rue: Using NOT NULL saves 1 bit per column.

Rue: Use binary type to store md5 value with 16 bytes instead of 32 bytes of varchar type.

Rue: Consider fixed format table structure (no varchar, no blob or no text columns) for tables with more write operations.

Rue: Use LIMIT in your SELECT, UPDATE statements if you already know your are looking for only one match, e.g. while checking for username and password match.

Rue: Avoid SELECT * FROM and specify only the interested column names.

Rue: Use index only when needed, indexes are good for reading and bad for storing data quickly. Identify the proportion of read and write operation on every single table in your database.

Rue: Consider storing image data outside the database and store only the reference of the image in the database. You can reconsider this approach if you want to support replication of servers.

For more information please refer the following sites,
http://dev.mysql.com/doc/refman/5.1/en/storage-engines.html
http://dev.mysql.com/doc/refman/5.1/en/optimization.html
http://www.slideshare.net/ronaldbradford/top-20-design-tips-for-mysql-data-architects-presentation
http://net.tutsplus.com/tutorials/other/top-20-mysql-best-practices/

Thursday, September 2, 2010

Improving the performance of web page | web application

The first step in improving the performance of your web page is to measure the current performance of the web page.

If you are using Google Chrome then try Speed Tracer. If you are using Firefox then try
Page Speed.

You can also try online performance testing at webpagetest.

Let us see some simple rules to improve the performance of the web page,

Rule: Remove broken links (<a>).

Rule: Combine the external javascript links into 2 or three links to reduce the HTTP request.

Rule: Use inline javascript for fewer lines of script.

Rule: To make the home page load faster have only the scripts needed for the home page either as external link or inline.

You can also try to defer the loading of javascript.

Rule: Try to defer the loading of javascript if possible.

Refer Browserscope for knowing the we browsers parallel loading capabilities of javascripts and other resources.

Rule: Combine the external css into 2 or three files to reduce the HTTP request.

Rule: Use inline css for fewer lines of css.

Rule: To make the home page load faster have only the css needed for the home page either as external link or inline.

Rule: Compress and compact the html, css and javascript resources to reduce the number of bytes sent over the network.

Rule: Minimize DNS lookups to reduce the resolution requests. Use URL paths e.g. host your site on www.xyz.com/abc instead of abc.xyz.com. Serve the startup javascript and other resources from the same host.

Rule: Minimize redirection from one URL to another.

Rule: Use image compressor. This would reduce number of bytes sent over the network.

Rule: Make the styles (css) load before the scripts (javascript). This would enables better parallelization of downloads and speeds up browser rendering.

Refer Browserscope for knowing the we browsers parallel loading capabilities of javascripts and other resources.

Rule: Parallelize the loading of resources.

Rule: Have the inline style and external style sheets in the "head" section.

Rule: Remove the unused css, javascript and html. This would eliminate unwanted bytes sent over the network.

Rule: Specify the size (height and width) of the image in the img tag.

For more details please refer Web Performance Best Practices and Let's make the web faster
.
Now try applying the above rules to your web page and measure the performance.

Watch the below two videos for more on improving web page | web application performance.

Speed Tracer



Page Speed