Separate large perf benchmarks into their own legs (#15231)
[platform/upstream/coreclr.git] / tests / src / JIT / Performance / CodeQuality / Benchstones / BenchF / Lorenz / Lorenz.cs
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 //
5 // This program solves the "lorenz" equations using Runge-Kutta 4
6
7 using Microsoft.Xunit.Performance;
8 using System;
9 using System.Runtime.CompilerServices;
10 using Xunit;
11
12 [assembly: OptimizeForBenchmarks]
13
14 namespace Benchstone.BenchF
15 {
16 public static class Lorenz
17 {
18 #if DEBUG
19     public const int Iterations = 1;
20 #else
21     public const int Iterations = 8000000;
22 #endif
23
24     private static double s_t = 0.0;
25     private static double s_x = 5.0;
26     private static double s_y = 2.0;
27     private static double s_z = 27.0;
28
29     private static int s_nsteps = Iterations;
30     private static double s_h = -1.0;
31     private static int s_printDerivative = -1;
32
33     [MethodImpl(MethodImplOptions.NoInlining)]
34     private static bool Bench()
35     {
36         double k1, k2, k3, k4;
37         double l1, l2, l3, l4;
38         double m1, m2, m3, m4;
39         double hdiv2, hdiv6;
40         int i;
41
42         if (s_h < 0.0)
43         {
44             s_h = 20.0 / (double)s_nsteps;
45         }
46         if (s_printDerivative < 0)
47         {
48             s_printDerivative = s_nsteps;
49         }
50
51         hdiv2 = s_h / 2.0;
52         hdiv6 = s_h / 6.0;
53
54         for (i = 0; i < s_nsteps; ++i)
55         {
56             double t_arg, x_arg, y_arg, z_arg;
57
58             k1 = F(s_t, s_x, s_y, s_z);
59             l1 = G(s_t, s_x, s_y, s_z);
60             m1 = H(s_t, s_x, s_y, s_z);
61
62             t_arg = s_t + hdiv2;
63             x_arg = s_x + hdiv2 * k1;
64             y_arg = s_y + hdiv2 * l1;
65             z_arg = s_z + hdiv2 * m1;
66
67             k2 = F(t_arg, x_arg, y_arg, z_arg);
68             l2 = G(t_arg, x_arg, y_arg, z_arg);
69             m2 = H(t_arg, x_arg, y_arg, z_arg);
70
71             x_arg = s_x + hdiv2 * k2;
72             y_arg = s_y + hdiv2 * l2;
73             z_arg = s_z + hdiv2 * m2;
74
75             k3 = F(t_arg, x_arg, y_arg, z_arg);
76             l3 = G(t_arg, x_arg, y_arg, z_arg);
77             m3 = H(t_arg, x_arg, y_arg, z_arg);
78
79             t_arg = s_t + s_h;
80             x_arg = s_x + s_h * k3;
81             y_arg = s_y + s_h * l3;
82             z_arg = s_z + s_h * m3;
83
84             k4 = F(t_arg, x_arg, y_arg, z_arg);
85             l4 = G(t_arg, x_arg, y_arg, z_arg);
86             m4 = H(t_arg, x_arg, y_arg, z_arg);
87
88             s_x = s_x + hdiv6 * (k1 + 2.0 * k2 + 2.0 * k3 + k4);
89             s_y = s_y + hdiv6 * (l1 + 2.0 * l2 + 2.0 * l3 + l4);
90             s_z = s_z + hdiv6 * (m1 + 2.0 * m2 + 2.0 * m3 + m4);
91             s_t = t_arg;
92         }
93
94         return true;
95     }
96
97     private static double F(double t, double x, double y, double z)
98     {
99         return (10.0 * (y - x));
100     }
101
102     private static double G(double t, double x, double y, double z)
103     {
104         return (x * (28.0 - z) - y);
105     }
106
107     private static double H(double t, double x, double y, double z)
108     {
109         return (x * y - (8.0 * z) / 3.0);
110     }
111
112     [Benchmark]
113     public static void Test()
114     {
115         foreach (var iteration in Benchmark.Iterations)
116         {
117             using (iteration.StartMeasurement())
118             {
119                 Bench();
120             }
121         }
122     }
123
124     private static bool TestBase()
125     {
126         bool result = Bench();
127         return result;
128     }
129
130     public static int Main()
131     {
132         bool result = TestBase();
133         return (result ? 100 : -1);
134     }
135 }
136 }