Performancing Metrics

Performance blog: FTP Performance testing

Friday, May 2, 2014

FTP Performance testing


The below  C# code was developed to place  EDI X12(HIPPA) files directly into the FTP folder for batch testing.
 
  The below script will also help

·         To  Test FTP server by placing larger number of files into FTP server as expected in production

·         To minimize the randomness in the testing process and ensures repeatability



using System;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using System.Net;

using System.Text;

using System.Drawing;

using System.IO;

using System.Threading;

using System.Threading.Tasks;

using System.Collections.Generic;

 

 

namespace FTPScript

{

    [TestClass]

    public  class UnitTest1

    {

      

        [TestMethod]

        public void main()

        {

 

            string sDir="C:\\270";// Directory containing Test data files

            List<FileInfo> files = new List<FileInfo>();

            try {

                           foreach (string f in Directory.GetFiles(sDir)) {

                                  files.Add(new FileInfo(f));

                           }

            }

            catch (System.Exception excpt)

            {

                throw excpt; 

            }

           /* List files = new List();

            files.Add(new FileInfo("C:\\270\\270.txt"));

            files.Add(new FileInfo("C:\\270\\270_DOB.DAT"));*/

 

                   

            Parallel.ForEach(files, objFile =>

                                                                      {

                                                                          UploadFile(objFile);

                                                                      });

 

        }

 

        static bool UploadFile(FileInfo fileInfo)

            {

                FtpWebRequest request = null;

                try

                {

                    string ftpPath = ftp://XXX.XX.XX.XX/CC + fileInfo.Name;

                    request = (FtpWebRequest)WebRequest.Create(ftpPath);

                    request.Credentials = new NetworkCredential("XXXX\\XXX", "XXXXX");  // username , password

                    request.Method = WebRequestMethods.Ftp.UploadFile;

                    request.KeepAlive = false;

                    request.Timeout = 60000; // 1 minute time out

                    request.ServicePoint.ConnectionLimit = 15; // Connection limit 15 with the ftp., By default 2, -1 means infinite.

 

                    byte[] buffer = new byte[1024];

                    using (FileStream fs = new FileStream(fileInfo.FullName, FileMode.Open))

                    {

                        int dataLength = (int)fs.Length;

                        int bytesRead = 0;

                        int bytesDownloaded = 0;

                        using (Stream requestStream = request.GetRequestStream())

                        {

                            while (bytesRead < dataLength)

                            {

                                bytesDownloaded = fs.Read(buffer, 0, buffer.Length);

                                bytesRead = bytesRead + bytesDownloaded;

                                requestStream.Write(buffer, 0, bytesDownloaded);

                            }

                            requestStream.Close();

                        }

                    }

                    return true;

                }

                catch (Exception ex)

                {

                    throw ex;               

                }

                finally

                {

                    request = null;

                }

                //return false;

            }// UploadFile

    }

}



@2011, copyright Vamsidhar Tokala