官术网_书友最值得收藏!

Setting parameters

In the previous example, we tested the method with only one value. Practically, when testing an enterprise application, we want to test it with different values to estimate the method's performance.

First of all, we can define a property for each parameter, set the Params attribute, and specify the value(s) for which we need that method to be tested. Then we can use that property in the code. BenchmarkRun automatically tests that method with all of the parameters and generates the report. Here is the complete code snippet of the TestBenchmark class:

public class TestBenchmark 
{ 
 
  [Params(10,20,30)] 
  public int Len { get; set; } 
         
  [Benchmark] 
  public  void Fibonacci() 
  { 
    int a = 0, b = 1, c = 0; 
    Console.Write("{0} {1}", a, b); 
 
    for (int i = 2; i < Len; i++) 
    { 
      c = a + b; 
      Console.Write(" {0}", c); 
      a = b; 
      b = c; 
    } 
  } 
 
  [Benchmark] 
  public  void FibonacciRecursive() 
  { 
    Fibonacci_Recursive(0, 1, 1, Len); 
  } 
 
  private void Fibonacci_Recursive(int a, int b, int counter, int len) 
  { 
    if (counter <= len) 
    { 
      Console.Write("{0} ", a); 
      Fibonacci_Recursive(b, a + b, counter + 1, len); 
    } 
  } 
}

After running Benchmark, the following report is generated:

主站蜘蛛池模板: 平山县| 伊宁市| 苍山县| 双柏县| 阳信县| 宜都市| 孟州市| 留坝县| 栾城县| 云安县| 阳西县| 沾益县| 桦川县| 桂平市| 浦北县| 长泰县| 资兴市| 凤凰县| 旺苍县| 赣榆县| 庆云县| 沅江市| 阳曲县| 商城县| 杭州市| 哈巴河县| 汤原县| 兴山县| 屏东县| 龙南县| 西青区| 古交市| 自贡市| 丹棱县| 东兴市| 扶绥县| 高台县| 阜新市| 肇东市| 来凤县| 昌乐县|