CRM Performance Toolkit example code


This is part of this post: Bandwidth testing using CRM Performance Toolkit


class BandWidthUtilities
      {
          //all input parameters follow the NetCap command line help – see description by “netcap /?”
          public static bool StartCapture(
              string path,
              string sMbyte,
              string sCapFile,
              string sFilter,
              string sNIC,
              string sOffset,
              string sTrigStr
              )
          {
              try
              {
                  string strProgName = "NetCap";

                  //clear the existing same process(es) first
                  Process[] iProcesses = Process.GetProcessesByName(strProgName);
                  if (iProcesses.Length > 0)
                  {
                      foreach (Process iProcess in iProcesses)
                      {
                          iProcess.Kill();
                      }
                  }

                  //e.g. sOffset = '1e' or '1a', then sTrigStr = ‘01010101’
                  string strArgs = "/B:" + sMbyte + " /C:\"" + sCapFile + "\" /T P "
                      + sOffset + " " + sTrigStr + " /N:" + sNIC + " /F:" + sFilter;
                  if (StartProgram(strProgName, strArgs, path, true) >= 0)
                      return true;
                  else
                      return false;
              }
              catch (Exception e)
              {
                  WriteErrorLog(e.ToString());
                  return false;
              }
          }


          public static bool StopCapture(string sCommandName, string sParams)
          {
              //e.g. sCommandName = "Ping"; sParams = "-n 1  1.1.1.1";
              if (StartProgram(sCommandName, sParams, null, true) >= 0)
                  return true;
              else
                  return false;
          }

          public static void StopProgram(string programName)
          {
              Process[] processes = Process.GetProcessesByName(programName);
              foreach (Process process in processes)
                  process.Kill();
          }

          public static void StopProgram(int pid)
          {
              Process process = Process.GetProcessById(pid);
              process.Kill();
          }

          public static int StartProgram(string exe, string sArgs, string path, bool waitForExit)
          {
              try
              {
                  Process MyProcess = new Process();

                  MyProcess.StartInfo.CreateNoWindow = true;
                  MyProcess.StartInfo.UseShellExecute = false;
                  MyProcess.StartInfo.FileName = exe;
                  if (path != string.Empty)
                      MyProcess.StartInfo.WorkingDirectory = path;

                  if (sArgs.Trim().ToLower() != "null")
                      MyProcess.StartInfo.Arguments = sArgs;

                  if (MyProcess.Start() == true)
                  {
                      if (waitForExit)
                      {
                          MyProcess.WaitForExit();
                          return 0;
                      }
                      return MyProcess.Id;
                  }
              }
              catch (Exception e)
              {
                  //write to error log
                  WriteErrorLog("failed in StartProgram, exception: " + e.ToString());

              }
              return -1;
          }

          public static bool RunNetMonExpert(string CapFileFullName)
          {
              bool status = true;
              int pid = 0;
              string Exe = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) +  @"\Microsoft\VRTA\VRTA.EXE";
  
              string ProgArgs = null;

              try
              {
                  ProgArgs = "\"" + CapFileFullName + "\"";
                  if ((pid = (StartProgram(Exe, ProgArgs, string.Empty, false))) > 0)
                  {
                      Thread.Sleep(2000);
                      StopProgram(pid);
                  }
                  else
                      WriteErrorLog("Run VRTA failed! " + CapFileFullName);
              
              }
              catch (Exception e)
              {
                  status = false;
                  WriteErrorLog(e.ToString());
              }
              return status;
          }

          public static void ParseRTAFile(string fileName, string title)
          {
              string dir = Path.GetDirectoryName(fileName);


              if (dir == "")
                  dir = ".";

              Console.WriteLine("Dir:" + dir);

              string fName = dir + "\\" + Path.GetFileNameWithoutExtension(fileName) + ".html";
              StreamWriter htmlwriter = new StreamWriter(fName, false);

              htmlwriter.WriteLine("
<html><head><title>" + title + "</title></head><body>");
              htmlwriter.WriteLine("
<p>&nbsp;</p><p align=\"center\"><b><font size=\"6\">" + title + "</font></b></p>");
              htmlwriter.WriteLine("<p>&nbsp;<b><font size=\"3\"><a href=\"" + fileName + "\">Open Netcom capture File</a></font></b></p>");
              htmlwriter.WriteLine("<p>&nbsp;<b><font size=\"3\"><a href=\"" + Path.GetFileNameWithoutExtension(fileName) + "_rta.txt\">Open Capture file as text</a></font></b></p>");
              htmlwriter.WriteLine("<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#111111\" width=\"100%\">");
              htmlwriter.WriteLine("<tr>");
              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\"><b>Request URI</b></td>");
              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\"><b>Status Code</b></td>");
              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\"><b>Content Type</b></td>");
              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\"><b>Total Bytes</b></td>");
              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\"><b>Upstream Bytes</b></td>");
              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\"><b>Content Length</b></td>");
              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\"><b>Number of Roundtrips </b></td>");
              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\"><b>Relative Time</b></td>");
              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\"><b>Time to 1st Byte</b></td>");
              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\"><b>Kbs Down</b></td>");
              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\"><b>Duration</b></td>");
              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\"><b>Finish Status</b></td>");
              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\"><b>HTTP Method</b></td>");
              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\"><b>Expire Date</b></td>");
              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\"><b>Referrer Request</b></td>");
              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\"><b>URI Args</b></td>");
              htmlwriter.WriteLine("</tr>");

              StreamReader fileReader = new StreamReader(dir + "\\" + Path.GetFileNameWithoutExtension(fileName) + "_rta.txt");

              try
              {
                  string line1 = fileReader.ReadLine();

                  int Rts = 0;
                  int numRequests = 0;

                  string[] strs1 = null;
                  string[] strs2 = null;
                  string[] nodes = line1.Split(new char[] { '\t' });

                  line1 = fileReader.ReadLine();
                  string line2 = fileReader.ReadLine();

                  strs1 = line1.Split(new char[] { '\t' });

                  //lets make sure we are starting from the right frame

                  while (line1 != null)
                  {
                      strs1 = line1.Split(new char[] { '\t' });
                      if (line2 != null)
                          strs2 = line2.Split(new char[] { '\t' });
                      if (strs1[3].IndexOf("
Stop Capture") >= 0)
                      {
                          line1 = line2;
                          line2 = fileReader.ReadLine();
                          continue;
                      }

                      else
                      {
                          if (strs1[0].IndexOf("
TOTALS") >= 0)
                          {
                              htmlwriter.WriteLine("
<tr>");
                              htmlwriter.WriteLine("
<td width=\"25%\"><b> TOTALS: Number of Requests:" + numRequests + "</b></td>");
                              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\"><b>" + strs1[4] + "</b></td>");
                              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\"><b>" + strs1[5] + "</b></td>");
                              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\"><b>" + strs1[6] + "</b></td>");
                              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\"><b>" + strs1[7] + "</b></td>");
                              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\"><b>" + strs1[8] + "</b></td>");
                              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\"><b>" + Rts + "</b></td>");
                              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\"><b>" + strs1[1] + "</b></td>");
                              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\"><b>" + strs1[12] + "</b></td>");
                              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\"><b>" + strs1[13] + "</b></td>");
                              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\"><b>" + strs1[10] + "</b></td>");
                              htmlwriter.WriteLine("<td width=\"25%\"><b>" + strs1[16] + "</b></td>");
                              htmlwriter.WriteLine("<td width=\"25%\"><b>" + strs1[17] + "</b></td>");
                              htmlwriter.WriteLine("<td width=\"25%\"><b>" + strs1[20] + "</b></td>");
                              htmlwriter.WriteLine("<td width=\"25%\"><b>" + strs1[18] + "</b></td>");
                              htmlwriter.WriteLine("<td width=\"25%\"><b>" + strs1[25] + "</b></td>");
                              htmlwriter.WriteLine("</tr>");
                              line1 = line2;
                              line2 = fileReader.ReadLine();

                          }
                          else
                          {

                              htmlwriter.WriteLine("<tr>");
                              htmlwriter.WriteLine("<td width=\"25%\"> " + strs1[3] + "</td>");
                              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\">" + strs1[4] + "</td>");
                              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\">" + strs1[5] + "</td>");
                              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\">" + strs1[6] + "</td>");
                              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\">" + strs1[7] + "</td>");
                              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\">" + strs1[8] + "</td>");
                              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\">" + strs1[15] + "</td>");
                              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\">" + strs1[1] + "</td>");
                              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\">" + strs1[12] + "</td>");
                              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\">" + strs1[13] + "</td>");
                              htmlwriter.WriteLine("<td width=\"25%\" align=\"center\">" + strs1[10] + "</td>");
                              htmlwriter.WriteLine("<td width=\"25%\">" + strs1[16] + "</td>");
                              htmlwriter.WriteLine("<td width=\"25%\">" + strs1[17] + "</td>");
                              htmlwriter.WriteLine("<td width=\"25%\">" + strs1[20] + "</td>");
                              htmlwriter.WriteLine("<td width=\"25%\">" + strs1[18] + "</td>");
                              htmlwriter.WriteLine("<td width=\"25%\">" + strs1[25] + "</td>");
                              htmlwriter.WriteLine("</tr>");


                              numRequests++;
                              Rts = Rts + Convert.ToInt32(strs1[15]);
                              line1 = line2;
                              line2 = fileReader.ReadLine();
                          }
                      }
                  }
              }

              catch (Exception e)
              {
                  Console.WriteLine("Exception:" + e.ToString());

              }

              finally
              {

                  htmlwriter.WriteLine("</table</body></html>");
                  htmlwriter.Close();

                  fileReader.Close();
              }
          }

          public static void WriteErrorLog(string LogString)
          {
              try
              {
                  string ErrLogName = "error.log";
                  StreamWriter sw = new StreamWriter(ErrLogName, true);
                  sw.WriteLine(DateTime.Now.ToString() + " - " + LogString);
                  sw.Close();
              }
              catch (Exception e)
              {
                  Console.WriteLine(e.ToString());
              }
          }
      }   

Comments (0)

Skip to main content