Search This Blog


Monday, October 24, 2016

The 4 factors which slowdown data trasfer and how to identify them
Every IT Operations team faces complaints indicating that the network is slowing down or poorly delivering applications. Their very first focus is on checking the network performance (latency, packet loss, etc.). 
Nevertheless, the network is not the sole driver of data transfer speed and of the end user experience.
Many other factors directly impact how fast application queries and responses will flow through the network. If one wants to troubleshoot performance degradations, this check list of the factors which can badly impact the transfer speed, will come in very handy. 
Let’s start with what may be network-led:
Network latency refers to the time needed to send a packet from the source to the destination. This time varies depending:
  • on the physical distance,
  • the number of network devices which have to be crossed (also referred to as number of hops)
  • and to a lesser extent, to the performance of each of the devices.
The relationship between latency and transfer rates depends on the protocol which carries the data. To keep the focus on the most common ones: for a UDP flow, latency may not have an impact. As for TCP applications, generally the most commonly used protocol, it will have a drastic impact.
Network congestion refers to the saturation of a path used by packets to flow between the source and the destination. The element on the path can be either an active device (e.g. Router or Switch) or a physical link (e.g. cable).
When the maximum capacity of the element is reached, the packet cannot be transferred in a timely manner as it is either put in a queue (e.g. in a router) or dropped if a no queue system is available to retain them. It may even become impossible to setup new sessions.
The consequence will then vary, depending on the level of delay generated by the congestion:
  • Packets are delayed for a short period of time.
    • The latency will increase.
    • Some retransmissions will occur (for TCP flows) as the acknowledgment packets are not received fast enough by the sender.
    • Duplicate acknowledgment packets will also be received.
  • Packets are lost or dropped (packet loss).
    • The retransmission increases significantly: as packets are not acknowledged, they will then be massively resent.

  • Disconnections: sessions are dropped as too many packets are lost:
    • You might see TTL exceeded, session time-outs.
    • TCP sessions not being terminated properly. 
Although the overall network path is free of any congestion (lack of bandwidth or system resources), some devices apply policies:
  • Prioritization: some traffic is either more strategic (critical applications) or more performance sensitive (real time applications, VoIP, video conferencing) and gets allocated a higher priority than the rest of the applications using a given network path. In case the maximum capacity on the network path is reached, lower priority flows will start experiencing retransmission, packet loss or disconnection depending on how long and important the congestion is.
  • Filtering / encryption: there may be many kinds of filtering’s in place to scan viruses, to prevent users to reach non-recommended sites, to prevent threats on web servers, etc. Filtering has an impact on data transfer: depending on how much processing time it requires. This might have an impact on the latency between the client and the server. If the processing time becomes excessive, it can generate retransmission and packet loss.
  • Routing / load balancing: some devices distribute the load across a group of servers / devices or route the traffic to the most adequate path from a performance and / or an economic standpoint. The devices may also be overloaded or misconfigured which could lead to retransmission, packet loss or disconnection issues.
While troubleshooting slow transfer rates, it is important to list the devices on the path between clients and servers. You can then identify at which point in time and for which flow: retransmissions, duplicate acknowledgments, packet loss, TTL expired and session time-out or incomplete TCP start can be observed.
It is probably the last item you will consider if you are focused on network performance. But these systems also have limited resources which can lead to a congestion situation and slowdown the data transfer rates.
If a server lacks hardware resources, such as RAM, CPU, I/O, it will process user queries slower.
At a given moment, a client or a server reaching a congestion point will slow down the transfer using standard TCP mechanisms.
Here is how you can identify that situation:
  • 0 window events: one of the parties is asking to reduce the throughput. You can interpret this indicator as a sign of lack of resources and investigates on the host to identify which resource is not sufficiently available.
  • RST – Resets events: one of the parties disconnects the session abruptly. Keep in mind that some applications may use RST as a standard way to terminate a session, even if it is not a best practice! 
 It is easy to gather this information from your network traffic and to quickly pinpoint where your data transfer slowdowns are coming from. 

Saturday, October 22, 2016

Convert Number into words using recursion

Recursion:  When a function calls itself again and again the process called recursion. The function which is being call is known as recursive function, and the process is called recursion.

Recently, I've a requirement in my project to print amount (number) into words like screenshot given below.

So I've implemented the following code. Below function is applicable till 999999999.
private static string ConvertIntoWords(long number)
           if (number == 0)
               return "zero";
           if (number < 0)
               return "minus " + ConvertIntoWords(Math.Abs(number));
           string words = "";
           if ((number / 10000000) > 0)
               words += ConvertIntoWords(number / 10000000) + " crores ";
               number %= 10000000;
           if ((number / 100000) > 0)
               words += ConvertIntoWords(number / 100000) + " lacs ";
               number %= 100000;
           if ((number / 1000) > 0)
               words += ConvertIntoWords(number / 1000) + " thousand ";
               number %= 1000;
           if ((number / 100) > 0)
               words += ConvertIntoWords(number / 100) + " hundred ";
               number %= 100;
           if (number > 0)
               var unitsMap = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
               var tensMap = new[] { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
               if (number < 20)
                   words += unitsMap[number];
                   words += tensMap[number / 10];
                   if ((number % 10) > 0)
                       words += " "+unitsMap[number % 10];
                       //words += "-" + unitsMap[number % 10];
           return words;
The above function recursively calls itself  and I've taken two arrays unitsMap and tensMap to find corresponding word to a digit.

Calling above function in C# Console Application:

First check whether entered string is valid number or not. If string is a valid number make a call to ConvertIntoWords() function to get the words representation of number otherwise ask user to enter a valid number.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NumberToWords
   class Program
       static void Main(string[] args)
           Console.WriteLine("Please enter number");
           long num;
           if(long.TryParse(Console.ReadLine(), out num)) //whether number is valid or not
               Console.WriteLine("You entered : " + ConvertIntoWords(num));
               Console.WriteLine("Please enter valid number");

Example Flow:

Import data from excel to Sql server database using MVC code first approach

What is Import Feature:

Import feature lets users to save the data from excel sheets into databases. This is useful when  we have a requirement of entering bulk data into a database and there may be chances of less internet connectivity, which results into data loss. To prevent this data loss we prefer to upload data using import from excel feature.

The other requirement of this feature is that most of the Data Entry Operator have expertise in excel sheets, that will result faster data entry to any software.

There are three basic steps for implementing this functionality.
  1. Get the filled excel sheet from user machine.
  2. Validate data according to software data model.
  3. Save validated data to database.


Now let us move to implement these steps using coding.

Very first step is to find a third party library for reading the content of excel sheets. The following is the list to reference libraries

  1. DoddleReport

  2. Aspose.Cells for .NET

We can find more reference libraries here

Here I’m going to explain how can we import data into MVC models using EPPlus reference library. Here I’m using database approach first approach. Steps are given below.

Step 1. Create database ExcelImportDB. Create table Users.
            Sql script for Step1

create database ExcelImportDB
use ExcelImportDB
create table Users2 (SNo int PRIMARY KEY NOT NULL, Name varchar(500), Age int)


Step 2.  Create a new MVC Project ExcelImport

Step 3. Add reference EPPlus reference library by executing following command
            Install-Package EPPlus

Step 4.  Add new entity model using below steps.
  • Right click Models folder under Solution Explorer. Click Add new Item. The following window will open

  • Name your entity as ExcelImportEntities. Click on add button, the following window will appear

  • Click on Next , the following window will appear


  • Click on New Connection, the following window wiil appear

  • Enter your sql server credentials and Click on test connection, the following window will appear

  • Click OK in test connection message and click OK in Connection Properties Window. The following window will appear
  • This will save your connection string in web.config as ExcelImportDBEntities. Click on Next, the following window will appear

  • Click on Finish. Now your Database connected with your application. Don’t forget to Build Solution in this step.

Step 5. Go to Index.cshtml page and write following code to create a web page upload that will let users to upload excel sheets in server.

   ViewBag.Title = "Index";

@using(Html.BeginForm("Upload", "Home", FormMethod.Post, new {enctype = "multipart/form-data"}))

               <input type="file" name="UploadedFile" />
           <td colspan="2">
               <input type="submit" name="Submit" value="Submit" />

Step 6. Go to HomeContollers, write the following action method.

public ActionResult Upload(FormCollection formCollection)
           var usersList = new List<User>();
           if (Request != null)
               HttpPostedFileBase file = Request.Files["UploadedFile"];
               if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName))
                   string fileName = file.FileName;
                   string fileContentType = file.ContentType;
                   byte[] fileBytes = new byte[file.ContentLength];
                   var data = file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength));
                   using (var package = new ExcelPackage(file.InputStream))
                       var currentSheet = package.Workbook.Worksheets;
                       var workSheet = currentSheet.First();
                       var noOfCol = workSheet.Dimension.End.Column;
                       var noOfRow = workSheet.Dimension.End.Row;
                       for (int rowIterator = 2; rowIterator <= noOfRow; rowIterator++)
                           var user = new User();
                           user.SNo = Convert.ToInt32(workSheet.Cells[rowIterator, 1].Value);
                           user.Name = workSheet.Cells[rowIterator, 2].Value.ToString();
                           user.Age = Convert.ToInt32(workSheet.Cells[rowIterator, 3].Value);
           using (ExcelImportDBEntities excelImportDBEntities = new ExcelImportDBEntities())
               foreach (var item in usersList)
               return View("Index");

Step 7. Build your solution and Run. The following web page will open.


Step 8.  Choose your excel sheet from which you want to upload data and click submit. My excel sheet screenshot is given below.

As soon you click on Submit button the data of excel sheet will be imported to your sql server.  Attaching herewith screenshot of sql server database after importing data.


Now you have successfully imported data from excel sheet into sql server database usin MVC database first approach.