Guest User

Untitled

a guest
Jul 18th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. void Main()
  2. {
  3. RunDirectly();
  4. RunIndirectly();
  5.  
  6. var sw = new Stopwatch();
  7.  
  8. sw.Start();
  9. for (int i = 0; i < 100; i++)
  10. {
  11. RunDirectly();
  12. }
  13. sw.Stop();
  14. Console.ForegroundColor = ConsoleColor.Green;
  15. Console.WriteLine($"100 Direct Executions: {sw.ElapsedMilliseconds}");
  16.  
  17. sw.Reset();
  18.  
  19. sw.Start();
  20. for (int i = 0; i < 100; i++)
  21. {
  22. RunIndirectly();
  23. }
  24. sw.Stop();
  25. Console.ForegroundColor = ConsoleColor.Blue;
  26. Console.WriteLine($"100 Indirect Executions: {sw.ElapsedMilliseconds}");
  27.  
  28. sw.Reset();
  29.  
  30. sw.Start();
  31. for (int i = 0; i < 100; i++)
  32. {
  33. RunIndirectly();
  34. }
  35. sw.Stop();
  36. Console.ForegroundColor = ConsoleColor.Blue;
  37. Console.WriteLine($"100 Indirect Executions: {sw.ElapsedMilliseconds}");
  38.  
  39. sw.Reset();
  40.  
  41. sw.Start();
  42. for (int i = 0; i < 100; i++)
  43. {
  44. RunDirectly();
  45. }
  46. sw.Stop();
  47. Console.ForegroundColor = ConsoleColor.Green;
  48. Console.WriteLine($"100 Direct Executions: {sw.ElapsedMilliseconds}");
  49.  
  50. sw.Reset();
  51.  
  52. sw.Start();
  53. for (int i = 0; i < 100; i++)
  54. {
  55. RunDirectly();
  56. }
  57. sw.Stop();
  58. Console.ForegroundColor = ConsoleColor.Green;
  59. Console.WriteLine($"100 Direct Executions: {sw.ElapsedMilliseconds}");
  60.  
  61. sw.Reset();
  62.  
  63. sw.Start();
  64. for (int i = 0; i < 100; i++)
  65. {
  66. RunIndirectly();
  67. }
  68. sw.Stop();
  69. Console.ForegroundColor = ConsoleColor.Blue;
  70. Console.WriteLine($"100 Indirect Executions: {sw.ElapsedMilliseconds}");
  71.  
  72. sw.Reset();
  73.  
  74. sw.Start();
  75. for (int i = 0; i < 100; i++)
  76. {
  77. RunIndirectly();
  78. }
  79. sw.Stop();
  80. Console.ForegroundColor = ConsoleColor.Blue;
  81. Console.WriteLine($"100 Indirect Executions: {sw.ElapsedMilliseconds}");
  82.  
  83. sw.Reset();
  84.  
  85. sw.Start();
  86. for (int i = 0; i < 100; i++)
  87. {
  88. RunDirectly();
  89. }
  90. sw.Stop();
  91. Console.ForegroundColor = ConsoleColor.Green;
  92. Console.WriteLine($"100 Direct Executions: {sw.ElapsedMilliseconds}");
  93.  
  94. sw.Reset();
  95.  
  96. }
  97.  
  98. private void RunDirectly()
  99. {
  100. var logger = new Logger();
  101.  
  102. try
  103. {
  104. SomeMethod();
  105. logger.Write("Completed Without Exception");
  106. return;
  107. }
  108. catch (Exception ex)
  109. {
  110. logger.Write("Exception During Crm Service Operation");
  111. logger.Write(ex);
  112. throw;
  113. }
  114. }
  115.  
  116. private void RunIndirectly()
  117. {
  118. var logger = new Logger();
  119.  
  120. Run(SomeMethod,logger);
  121.  
  122. }
  123.  
  124. private void SomeMethod()
  125. {
  126. SomeMethod(20);
  127. }
  128.  
  129. private void SomeMethod(int milliSeconds)
  130. {
  131. Thread.Sleep(milliSeconds);
  132. }
  133.  
  134.  
  135. private void Run(Action method, Logger _logger)
  136. {
  137. Run<object>(() =>
  138. {
  139. method();
  140. return null;
  141. }, _logger);
  142. }
  143.  
  144. // Define other methods and classes here
  145. private T Run<T>(Func<T> method, Logger _logger)
  146. {
  147. if (method == null)
  148. {
  149. _logger.Write($"Null Argument: {nameof(method)}");
  150. throw new ArgumentNullException(nameof(method));
  151. }
  152.  
  153. try
  154. {
  155. var result = method();
  156. return _logger.WriteAndReturn(result, "Completed Without Exception");
  157. }
  158. catch (Exception ex)
  159. {
  160. _logger.Write("Exception During Crm Service Operation");
  161. _logger.Write(ex);
  162. throw;
  163. }
  164.  
  165. }
  166.  
  167. public class Logger
  168. {
  169. public T WriteAndReturn<T>(T returnObject, string contents)
  170. {
  171. return returnObject;
  172. }
  173.  
  174. public void Write(string contents)
  175. {}
  176.  
  177. public void Write(Exception contents)
  178. {}
  179.  
  180.  
  181. }
Add Comment
Please, Sign In to add comment