Saturday, November 15, 2008

Performance of the application can be increased by executing multiple web methods at the same time if they are not dependent on each other.

For example,

Consider two web service methods as follows:

[WebMethod]

    public float CalculateIncomeTax(float basic)

    {

        //Complicated calculations which takes time

        Thread.Sleep(8000);      

 

        return basic/100;

    }

[WebMethod]

    public float CalculateServiceTax(float basic)

    {

        //Complicated calculations which takes time

        Thread.Sleep(5000);

 

        return basic/1000;

    }

The following code calls these methods.

Synchronous call:

private void buttonSynch_Click(object sender, EventArgs e)

        {

            //Calculates Income tax

            StartProcess(true);

            newWebService.CalculateIncomeTax(100000);

            StartProcess(false);

            //Calculates Service tax

            StartProcess(true);

            newWebService.CalculateServiceTax(100000);

            StartProcess(false);

        }

Time required for execution = Time required for ( CalculateIncomeTax) + Time required for (CalculateServiceTax)

=  Approx 8 Sec. + Approx 5 Sec.

=  13.963955 ( Actual time required)

= Approx 14 Sec.

 

Asynchronous call for one function:

private void buttonAsynch_Click(object sender, EventArgs e)

        {

            StartProcess(true);

            newWebService.CalculateIncomeTaxAsync(100000);

            StartProcess(true);

            newWebService.CalculateServiceTax(100000);

            StartProcess(false);

  }

Time required for execution = Max (Time required for ( CalculateIncomeTax),  Time required for (CalculateServiceTax) )

=  Max ( Approx 8 Sec. , Approx 5 Sec. )

=  8.429886 (Actual time required )

= Approx 8 Sec.

Method StartProcess is written to signal start and end of processing.

int countProcess = 0

void StartProcess( bool startProcess )

        {

            if (startProcess)

                m_countProcess++;

            else

                m_countProcess--;

            if( m_countProcess == 0 )

                labelProcessingTax.Text = "";

            else

                labelProcessingTax.Text = "Processing";

            labelProcessingTax.Refresh();

}

Event handler is written to handle Asynchronous call completion event for CalculateIncomeTax method.

newWebService.CalculateIncomeTaxCompleted += new WindowsApplication1.NewService.CalculateIncomeTaxCompletedEventHandler(newWebService_CalculateIncomeTaxCompleted);

void newWebService_CalculateIncomeTaxCompleted(object sender, WindowsApplication1.NewService.CalculateIncomeTaxCompletedEventArgs e)

        {

            if (e.Cancelled == true)

                return;

             //Output is e.Result, use it to display IncomeTax

             StartProcess(false);

        }

Web service proxy class is generated by Visual studio when web reference is added.

Proxy class contains two overloaded methods for each web method to facilitate asynchronous calls.

 

       I.            Single-invocation:

(Web method name)Async ( method parameters…)

 

     II.            Multiple-invocation:

(Web method name)Async ( method parameters…, , object userState)

          Userstate should be unique value (for example, a GUID or hash code).

Usage:

1.      Multiple asynchronous calls:

Multiple-invocation makes possible calling methods asynchronously multiple times without waiting for any pending asynchronous operations to finish.

Trying to call methods asynchronously with single-invocation syntax multiple times, before previous invocation is completed throws InvalidOperationException.

 

2.      Tracking Pending Operations:

userState objects passed as parameters can be stored in collection to keep track of all pending tasks.

 

3.      Canceling Pending Operations:

Any particular instance of asynchronous operation can be cancelled by specifying the userState parameter passed during invocation to CancelAsync function.

E.g. newWebService.CancelAsync(userState);

Asynchronous calls invoked by single-invocation syntax are not cancellable.

No comments: