forked from DapperLib/Dapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
122 lines (115 loc) · 4.18 KB
/
Program.cs
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
using BenchmarkDotNet.Running;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using static System.Console;
namespace Dapper.Tests.Performance
{
public static class Program
{
public static void Main(string[] args)
{
#if DEBUG
WriteLineColor("Warning: DEBUG configuration; performance may be impacted!", ConsoleColor.Red);
WriteLine();
#endif
WriteLine("Welcome to Dapper's ORM performance benchmark suite, based on BenchmarkDotNet.");
Write(" If you find a problem, please report it at: ");
WriteLineColor("https://github.com/StackExchange/Dapper", ConsoleColor.Blue);
WriteLine(" Or if you're up to it, please submit a pull request! We welcome new additions.");
WriteLine();
if (args.Length == 0)
{
WriteLine("Optional arguments:");
WriteColor(" --all", ConsoleColor.Blue);
WriteLine(": run all benchmarks");
WriteColor(" --legacy", ConsoleColor.Blue);
WriteLine(": run the legacy benchmark suite/format", ConsoleColor.Gray);
WriteLine();
}
WriteLine("Using ConnectionString: " + BenchmarkBase.ConnectionString);
EnsureDBSetup();
WriteLine("Database setup complete.");
if (args.Any(a => a == "--all"))
{
WriteLine("Iterations: " + BenchmarkBase.Iterations);
var benchmarks = new List<Benchmark>();
var benchTypes = Assembly.GetEntryAssembly().DefinedTypes.Where(t => t.IsSubclassOf(typeof(BenchmarkBase)));
WriteLineColor("Running full benchmarks suite", ConsoleColor.Green);
foreach (var b in benchTypes)
{
benchmarks.AddRange(BenchmarkConverter.TypeToBenchmarks(b));
}
BenchmarkRunner.Run(benchmarks.ToArray(), null);
}
else if (args.Any(a => a == "--legacy"))
{
var test = new LegacyTests();
const int iterations = 500;
WriteLineColor($"Running legacy benchmarks: {iterations} iterations that load up a Post entity.", ConsoleColor.Green);
test.RunAsync(iterations).GetAwaiter().GetResult();
WriteLine();
WriteLineColor("Run complete.", ConsoleColor.Green);
}
else
{
WriteLine("Iterations: " + BenchmarkBase.Iterations);
BenchmarkSwitcher.FromAssembly(typeof(Program).GetTypeInfo().Assembly).Run(args);
}
}
private static void EnsureDBSetup()
{
using (var cnn = new SqlConnection(BenchmarkBase.ConnectionString))
{
cnn.Open();
var cmd = cnn.CreateCommand();
cmd.CommandText = @"
If (Object_Id('Posts') Is Null)
Begin
Create Table Posts
(
Id int identity primary key,
[Text] varchar(max) not null,
CreationDate datetime not null,
LastChangeDate datetime not null,
Counter1 int,
Counter2 int,
Counter3 int,
Counter4 int,
Counter5 int,
Counter6 int,
Counter7 int,
Counter8 int,
Counter9 int
);
Set NoCount On;
Declare @i int = 0;
While @i <= 5001
Begin
Insert Posts ([Text],CreationDate, LastChangeDate) values (replicate('x', 2000), GETDATE(), GETDATE());
Set @i = @i + 1;
End
End
";
cmd.Connection = cnn;
cmd.ExecuteNonQuery();
}
}
public static void WriteLineColor(string message, ConsoleColor color)
{
var orig = ForegroundColor;
ForegroundColor = color;
WriteLine(message);
ForegroundColor = orig;
}
public static void WriteColor(string message, ConsoleColor color)
{
var orig = ForegroundColor;
ForegroundColor = color;
Write(message);
ForegroundColor = orig;
}
}
}