Sunday, May 22, 2011

CIFS performance measurement in Windows

windows has a very strange behavior when it comes to Copy/Move . and what is strange about it is the Progress bar and the information related to the progress bar. it is as far is could get from accuracy in MBps and time remaining , but its not their fault because estimating the time and MBps is very hard since their is so many factors related to those kind of measurements , so i decided to make small utility to get the average Timing and MBps , and i need those readings as a part of network performance benchmark on CIFS shares

check below for the code in C# , i hope that you will benefit from it, take into consideration i wrote the code quick and dirty :-)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace Copy_test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

      //this dictionary will hold the file names and the full path
        public Dictionary<string, string> selectFilesInfo = new Dictionary<string, string>();



        private void browseDSbutton2_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {

       
            openFileDialog1.Multiselect = true;
            openFileDialog1.Title = "source files to be copied";
            openFileDialog1.Filter = "All Files|*.*";
            openFileDialog1.FileName = "";

            if (openFileDialog1.ShowDialog() ==DialogResult.OK)
            {
             
                for(int i=0;i<openFileDialog1.FileNames.Length;i++)
                {
                 
                  // add filenames and filepath to dictionary
                   selectFilesInfo.Add(openFileDialog1.SafeFileNames[i],openFileDialog1.FileNames[i]);
                 
                    textBox2.Text += openFileDialog1.FileNames[i] + " ";
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
           // create a stopwatch instance which comes in handy
            Stopwatch sw = Stopwatch.StartNew();
           //stop the stop watch because we didn't start taking readings yet
            sw.Stop();
            long filesizes = 0;
            FileInfo fi;

            foreach (KeyValuePair<string, string> kvp in selectFilesInfo)
            {
                 //application core it will measure the timing for every copy process, take the summation of the file sizes
                fi = new FileInfo(kvp.Value);
                filesizes  = filesizes + fi.Length;
                sw.Start();
                File.Copy(kvp.Value,textBox1.Text + "\\" + kvp.Key,true);
                sw.Stop();
            }


            long time = sw.ElapsedMilliseconds/1000;
            filesizes = (filesizes / 1024)/1024;
         
            MessageBox.Show("Copying effictive time took : " + time.ToString() + " seconds to write " +
                filesizes + " MB in avarage speed "+ filesizes/time +"MBps");
        }
    }
}

No comments:

Post a Comment