summaryrefslogtreecommitdiff
path: root/tests/src/JIT/Performance/CodeQuality/Math/Functions/Functions.cs
blob: aff91ba844ca322ad7a1ee87361e118234efa40f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Xunit.Performance;

[assembly: OptimizeForBenchmarks]

namespace Functions
{
    public static class Program
    {
#if DEBUG
        private const int defaultIterations = 1;
#else
        private const int defaultIterations = 1000;
#endif

        private static readonly IDictionary<string, Action> TestList = new Dictionary<string, Action>() {
            ["absdouble"] = MathTests.AbsDoubleTest,
            ["abssingle"] = MathTests.AbsSingleTest,
            ["acosdouble"] = MathTests.AcosDoubleTest,
            ["acossingle"] = MathTests.AcosSingleTest,
            ["asindouble"] = MathTests.AsinDoubleTest,
            ["asinsingle"] = MathTests.AsinSingleTest,
            ["atandouble"] = MathTests.AtanDoubleTest,
            ["atansingle"] = MathTests.AtanSingleTest,
            ["atan2double"] = MathTests.Atan2DoubleTest,
            ["atan2single"] = MathTests.Atan2SingleTest,
            ["ceilingdouble"] = MathTests.CeilingDoubleTest,
            ["ceilingsingle"] = MathTests.CeilingSingleTest,
            ["cosdouble"] = MathTests.CosDoubleTest,
            ["cossingle"] = MathTests.CosSingleTest,
            ["coshdouble"] = MathTests.CoshDoubleTest,
            ["coshsingle"] = MathTests.CoshSingleTest,
            ["expdouble"] = MathTests.ExpDoubleTest,
            ["expsingle"] = MathTests.ExpSingleTest,
            ["floordouble"] = MathTests.FloorDoubleTest,
            ["floorsingle"] = MathTests.FloorSingleTest,
            ["logdouble"] = MathTests.LogDoubleTest,
            ["logsingle"] = MathTests.LogSingleTest,
            ["log10double"] = MathTests.Log10DoubleTest,
            ["log10single"] = MathTests.Log10SingleTest,
            ["powdouble"] = MathTests.PowDoubleTest,
            ["powsingle"] = MathTests.PowSingleTest,
            ["rounddouble"] = MathTests.RoundDoubleTest,
            ["roundsingle"] = MathTests.RoundSingleTest,
            ["sindouble"] = MathTests.SinDoubleTest,
            ["sinsingle"] = MathTests.SinSingleTest,
            ["sinhdouble"] = MathTests.SinhDoubleTest,
            ["sinhsingle"] = MathTests.SinhSingleTest,
            ["sqrtdouble"] = MathTests.SqrtDoubleTest,
            ["sqrtsingle"] = MathTests.SqrtSingleTest,
            ["tandouble"] = MathTests.TanDoubleTest,
            ["tansingle"] = MathTests.TanSingleTest,
            ["tanhdouble"] = MathTests.TanhDoubleTest,
            ["tanhsingle"] = MathTests.TanhSingleTest
        };

        private static int Main(string[] args)
        {
            var isPassing = true; var iterations = defaultIterations;
            ICollection<string> testsToRun = new HashSet<string>();

            try
            {
                for (int index = 0; index < args.Length; index++)
                {
                    if (args[index].ToLowerInvariant() == "-bench")
                    {
                        index++;

                        if ((index >= args.Length) || !int.TryParse(args[index], out iterations))
                        {
                            iterations = defaultIterations;
                        }
                    }
                    else if (args[index].ToLowerInvariant() == "all")
                    {
                        testsToRun = TestList.Keys;
                        break;
                    }
                    else
                    {
                        var testName = args[index].ToLowerInvariant();

                        if (!TestList.ContainsKey(testName))
                        {
                            PrintUsage();
                            break;
                        }

                        testsToRun.Add(testName);
                    }
                }

                if (testsToRun.Count == 0)
                {
                    testsToRun = TestList.Keys;
                }

                foreach (var testToRun in testsToRun)
                {
                    Console.WriteLine($"Running {testToRun} test...");
                    Test(iterations, TestList[testToRun]);
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine($"    Error: {exception.Message}");
                isPassing = false;
            }

            return isPassing ? 100 : -1;
        }

        private static void PrintUsage()
        {
            Console.WriteLine(@"Usage:
Functions [name] [-bench #]

  [name]: The name of the function to test. Defaults to 'all'.
    all");

            foreach (var testName in TestList.Keys)
            {
                Console.WriteLine($"  {testName}");
            }

            Console.WriteLine($@"
  [-bench #]: The number of iterations. Defaults to {defaultIterations}");
        }

        private static void Test(int iterations, Action action)
        {
            // ****************************************************************

            Console.WriteLine("  Warming up...");

            var startTimestamp = Stopwatch.GetTimestamp();

            action();

            var totalElapsedTime = (Stopwatch.GetTimestamp() - startTimestamp);
            var totalElapsedTimeInSeconds = (totalElapsedTime / (double)(Stopwatch.Frequency));

            Console.WriteLine($"    Total Time: {totalElapsedTimeInSeconds}");

            // ****************************************************************

            Console.WriteLine($"  Executing {iterations} iterations...");

            totalElapsedTime = 0L;

            for (var iteration = 0; iteration < iterations; iteration++)
            {
                startTimestamp = Stopwatch.GetTimestamp();

                action();

                totalElapsedTime += (Stopwatch.GetTimestamp() - startTimestamp);
            }

            totalElapsedTimeInSeconds = (totalElapsedTime / (double)(Stopwatch.Frequency));

            Console.WriteLine($"    Total Time: {totalElapsedTimeInSeconds} seconds");
            Console.WriteLine($"    Average Time: {totalElapsedTimeInSeconds / iterations} seconds");

            // ****************************************************************
        }
    }
}