Click here to Skip to main content
Click here to Skip to main content

Advanced Asynchoronous WebServices

, 20 Apr 2004
Rate this:
Please Sign up or sign in to vote.
Advanced asynchoronous WebServices.

Introduction

This application shows how to use asynchronous web services in your application and how you can use the callback methods in your application. I made this application to calculate the speedup for the case when you want to run long processes, in different machine versus single machine.

This machine displays which job is running, which task and latest task display in red color.

My Approach

I used .NET/ C# / SOAP and web services technique to implement this. The neat part of using asynchronous .NET web services is that they call the callback function when the function at the other machine is completed, which solves lots of complicacy from the project. Specially, when we need to dispatch 400 jobs, then I just check about which machine calls the callback, means that machine is free now, so I send the next task dispatch to that machine.

Asynchronous Web Service

When you create any web method, it creates an asynchronous method also for you, which has the “Begin” keyword before the web method name.

For example, I have SortMachine1 web method, so I call it like this:

BeginSortMachine1(parameter,CallBackFunction,0);

This is a call back function, when an asynchronous web service completes, this function gets called.

public void OnCBMachine1(IAsyncResult ar)
{
  try
  {
    if (false == bIsBtn400Clicked)
    {
      mylist1[0] = st.EndSortMachine1(ar);
      bMachineDone[0] = true; 
      eTime[0] = System.DateTime.Now;
      timeDiff[0] = eTime[0].Subtract(sTime[0]);
      lblMachine1Time.Text = timeDiff[0].TotalMilliseconds.ToString();
      lblMsg1.Text = "Finished Machine 1 Job.";

      if ( bMachineDone[1] == true && bMachineDone[2] == true && 
                                           bMachineDone[3] == true )
      {
        timeDiff[4] = eTime[0].Subtract(sTime[0]);
        lblTimeTaken.Text = timeDiff[4].TotalMilliseconds.ToString();
        double dTime = timeDiff[4].TotalMilliseconds / Int32.Parse(datasize4.Text);
        avg4.Text = dTime.ToString();
        task4.Text = (timeDiff[4].TotalMilliseconds / 4).ToString();
        lblInfo.Text = "Job Completed";
      }
    }
    else
    {
      mylist400[nCounterMachine1] = st.EndSortMachine1(ar);
      bMachineDone[0] = true;
      eTime[0] = System.DateTime.Now;
      timeDiff[0] = eTime[0].Subtract(sTime[0]);
      TotalTimeFor400Tasks += timeDiff[0].TotalMilliseconds;
      lblMachine1Time.Text = timeDiff[0].TotalMilliseconds.ToString();
      lblMsg1.Text = "Finished Machine 1 Job # " + (nCounterMachine1+1).ToString();

      lblTimeTaken.Text = TotalTimeFor400Tasks.ToString();
      double dTime = TotalTimeFor400Tasks / Int32.Parse(datasize400.Text);
      avg400.Text = dTime.ToString();
      task400.Text = (TotalTimeFor400Tasks / nCurrentJobIndex).ToString();
      if ( nCurrentJobIndex == 400)
      {
        lblInfo.Text = "Job Completed";
        dTime = TotalTimeFor400Tasks / Int32.Parse(datasize400.Text);
        avg400.Text = dTime.ToString();
        task400.Text = (TotalTimeFor400Tasks / nCurrentJobIndex).ToString();
      }
      else
        ServiceJobs();
    }
  }
  catch(Exception ex)
  {
    lblInfo.Text = ex.Message.ToString();
  }
}

Part of the Projects

I created the project in two parts.

Server: This is an XML web service, which has four web methods, that sorts the data using (O)n2 algorithm. Creating and XML web service is a very easy task in .NET. Tricky part is, I want to use these methods asynchronously. When you create a web service in .NET, it creates that method asynchronous and synchronous both. All you need to add is a Begin keyword, if you want to use that method as asynchronous. Also, you need to assign a callback function, which is called by web method, when web method is completed.

I create XML web services that are running on four different machines, and these web services contain the function for the sorting of data (On2) algorithm. This web service is called DataProcess and has four functions.

Client Application (Windows Based Application): This is the second application I made. This is a simple Windows based application in C# .NET. And I added web reference for the services I put on the different computer on my office LAN. Searching for all the available web services is very easy in .NET.

Screen shot of an application:

Clicking on Add Reference button will show up this dialog:

If you click on local machine, it searches in your machine and gives you a list of web services available on your computer.

It’s a Windows based application. It’s a simple application. It has 3 options:

  • Single thread : This means application runs on same machine as a full task and estimates the time.
  • 4 tasks : Since we have four machines, create 4 tasks, and assign the job to each machine and start the timer, and when all the jobs are finished then count that time. Subtract this time from start time, that is total time taken by this method.
  • 400 Tasks : This is a little tricky part. I solved this part in the following order. Create 400 tasks.
    1. Start the timer.
    2. Send first four tasks to four machines, and keep the counter of current job.
    3. We call all jobs asynchronously, so as soon as I get a callback function from machine, I send another job on line and increment the counter.
    4. Keep doing this until counter reaches 400 (means all jobs are dispatched).
    5. Now wait for last job to complete and as soon as you get that callback, mark that time.
    6. Take a difference of this time from the start time, that is your total time taken from this method.

Analysis

  • Single machine Test Analysis:

    All times are in milliseconds.

    Single Machine Test

    When I first read the problem, I assumed the performance for 400 tasks will be better than 4 task, even for single machine. But the results are completely opposite. Reason: When I run 400 tasks on the same machine, it takes up the whole CPU time, CPU usage is 100%, which dramatically slows down the whole system performance. This project is designed to run on 4 different machines, so you will not achieve the result by running on the same machine.

  • Multiple machine Test Analysis:

    In this case, we gets much better results for 400 tasks, but for 4 tasks, it's not much better improvement. I think there are two reasons for that.

    1. For four tasks, we create a connection object for each machine, so that we can call the web service, which introduces a slowdown for the first call.
    2. Each slave machine called the function only one time, so nothing is in cache at this point.

    Reason of getting better results in case of 400 tasks.

    1. We create a connection object only once and than keep calling the web service again and again, which enhances the performance.
    Tt  = Total time to execute job one as single program.
    Tp = Time of that part of job, that can be broken 
                             down into independent tasks.
    Speed Up =  S(k) = Tt / Tk = 7232.6224 / 2214.7874 = 3.265

    Theoretical speedup:

                     1
    S(k) = ---------------------
               (1-b) + b/k
    
    Here K = 4, lets assume b = 1, which is an ideal case, 
                               when everything is distributed.
    Then 
    S(k) = 4.
    
    If b = .96
    S(k) = 1/ (.04+ .24) = 1/ .28 = 3.57

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Shail_Srivastav
Web Developer
United States United States

Comments and Discussions

 
GeneralShowing error in <authentication mode="Windows" /> Pinmemberrajasingam29-Apr-08 0:52 
GeneralForm freezes on async webservice call PinmemberTalal Sultan6-Jun-06 21:44 
GeneralRe: Form freezes on async webservice call PinmemberShail_Srivastav8-Sep-06 4:58 
QuestionIt is not running on my machine??? PinsussMichellesfs21-Oct-05 2:20 
AnswerRe: It is not running on my machine??? PinmemberShail_Srivastav21-Oct-05 5:34 
GeneralPerfomance Question Pinmemberis0butane4-Apr-05 13:04 
GeneralGreat Article PinmemberJeffrey Scott Flesher13-Sep-04 19:58 
GeneralRe: Great Article PinmemberShail_Srivastav14-Sep-04 10:21 
GeneralHere's one PinmemberAndrew McCarter21-Apr-04 9:48 
GeneralRe: Here's one PinsussAnonymous22-Apr-04 6:12 
GeneralRe: Here's one PinmemberAndrew McCarter22-Apr-04 8:30 
GeneralRe: Here's one PinstaffNishant S29-Apr-04 2:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141220.1 | Last Updated 21 Apr 2004
Article Copyright 2004 by Shail_Srivastav
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid