Venciity

UnitTestsPerformance

Jan 17th, 2015
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.37 KB | None | 0 0
  1. using System;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using PhonebookSystem;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace Phonebook.Tests
  8. {
  9.     [TestClass]
  10.     public class UnitTestsPerformance
  11.     {
  12.         [TestMethod]
  13.         [Timeout(3000)]
  14.         public void TestPerformance()
  15.         {
  16.             StringBuilder input = new StringBuilder();
  17.  
  18.             int addCommandsCount = 1500;
  19.             for (int i = 0; i < addCommandsCount / 2; i++)
  20.             {
  21.                 input.AppendLine("AddPhone(Nakov, +359887333" + i + ", +359887333999)");
  22.                 input.AppendLine("AddPhone(Nakov" + i + ", +359887333999, +359887" + i + ")");
  23.             }
  24.  
  25.             int changePhoneCommandsCount = 100;
  26.             for (int i = 0; i < changePhoneCommandsCount / 3; i++)
  27.             {
  28.                 input.AppendLine("ChangePhone(+359887333" + (i + 100) + ", +359887333" + (i + 200) +")");
  29.                 input.AppendLine("ChangePhone(+359887333999" + ", +359887333777)");
  30.                 input.AppendLine("ChangePhone(+359887333777" + ", +359887333999)");
  31.                 input.AppendLine("ChangePhone(+359887" + (i + 500) + ", +359887333999)");
  32.             }
  33.  
  34.             int listCommandsCount = 5000;
  35.             for (int i = 0; i < listCommandsCount; i++)
  36.             {
  37.                 input.AppendLine("List(" + i + ", " + (1 + (i % 20)) + ")");
  38.             }
  39.  
  40.             input.AppendLine("End");
  41.  
  42.             // Forcefully invoke the static constructor to ensure the program state is clean
  43.             typeof(PhonebookSystem.Phonebook).TypeInitializer.Invoke(null, null);
  44.            
  45.             // Redirect the console input / output and invoke the Main() method
  46.             Console.SetIn(new StringReader(input.ToString()));
  47.             StringWriter consoleOutput = new StringWriter();
  48.             Console.SetOut(consoleOutput);
  49.             PhonebookSystem.Phonebook.Main();
  50.  
  51.             // Assert is not needed, just check the performance
  52.             // Assert.AreEqual(expected, actual);
  53.         }
  54.  
  55.         [TestMethod]
  56.         [Timeout(1000)]
  57.         public void TestPerformanceConvertPhoneToCannonicalForm()
  58.         {
  59.             // Prepare input commands
  60.             StringBuilder input = new StringBuilder();
  61.             int addCommandsCount = 1000;
  62.             for (int i = 0; i < addCommandsCount; i++)
  63.             {
  64.                 input.AppendLine("AddPhone(Nakov, 02 / 981 45 66)");
  65.                 input.AppendLine("AddPhone(Nakov, +359 899 99 22 22)");
  66.                 input.AppendLine("AddPhone(Nakov, (062) 62 62 62)");
  67.                 input.AppendLine("AddPhone(Nakov, (+359) 899 777-555)");
  68.             }
  69.  
  70.             input.AppendLine("List(0, 1)");
  71.             input.AppendLine("End");
  72.  
  73.             // Prepare the expected result
  74.             StringBuilder expectedOutput = new StringBuilder();
  75.             expectedOutput.AppendLine("Phone entry created");
  76.             for (int i = 0; i < 4 * addCommandsCount - 1; i++)
  77.             {
  78.                 expectedOutput.AppendLine("Phone entry merged");
  79.             }
  80.             expectedOutput.AppendLine("[Nakov: +35929814566, +35962626262, +359899777555, +359899992222]");
  81.  
  82.             // Forcefully invoke the static constructor to ensure the program state is clean
  83.             typeof(PhonebookSystem.Phonebook).TypeInitializer.Invoke(null, null);
  84.  
  85.             // Redirect the console input / output and invoke the Main() method
  86.             Console.SetIn(new StringReader(input.ToString()));
  87.             StringWriter consoleOutput = new StringWriter();
  88.             Console.SetOut(consoleOutput);
  89.             PhonebookSystem.Phonebook.Main();
  90.  
  91.             // Assert that the program execution result is correct
  92.             string expected = expectedOutput.ToString();
  93.             string actual = consoleOutput.ToString();
  94.             Assert.AreEqual(expected.Length, actual.Length);
  95.         }
  96.  
  97.         [TestMethod]
  98.         [Timeout(1000)]
  99.         public void TestPerformanceChangePhone()
  100.         {
  101.             // This test is expected to fail due to timeout. It just shows that
  102.             // a performance bottleneck exists in the ChangePhone() method.
  103.  
  104.             StringBuilder input = new StringBuilder();
  105.             int addCommandsCount = 2000;
  106.             for (int i = 0; i < addCommandsCount; i++)
  107.             {
  108.                 input.AppendLine("AddPhone(Nakov" + i + ", +359887333999)");
  109.             }
  110.  
  111.             int changePhoneCommandsCount = 500;
  112.             for (int i = 0; i < changePhoneCommandsCount / 2; i++)
  113.             {
  114.                 input.AppendLine("ChangePhone(+359887333999" + ", +359887333777)");
  115.                 input.AppendLine("ChangePhone(+359887333777" + ", +359887333999)");
  116.             }
  117.  
  118.             input.AppendLine("End");
  119.  
  120.             // Forcefully invoke the static constructor to ensure the program state is clean
  121.             typeof(PhonebookSystem.Phonebook).TypeInitializer.Invoke(null, null);
  122.  
  123.             // Redirect the console input / output and invoke the Main() method
  124.             Console.SetIn(new StringReader(input.ToString()));
  125.             StringWriter consoleOutput = new StringWriter();
  126.             Console.SetOut(consoleOutput);
  127.             PhonebookSystem.Phonebook.Main();
  128.  
  129.             // Assert is not needed, just check the performance
  130.             // Assert.AreEqual(expected, actual);
  131.         }
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment