Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 3a9c11a

Browse files
committed
Add Dapper Post perf tests
1 parent a9a53c1 commit 3a9c11a

File tree

2 files changed

+188
-0
lines changed

2 files changed

+188
-0
lines changed

tests/ServiceStack.OrmLite.Tests/ServiceStack.OrmLite.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@
255255
<Compile Include="UseCase\FieldFromInterfaceImplementationUseCase.cs" />
256256
<Compile Include="UseCase\NestedComplexTypeUseCase.cs" />
257257
<Compile Include="UseCase\ParentChildCyclicalExample.cs" />
258+
<Compile Include="UseCase\PostPerfTests.cs" />
258259
<Compile Include="UseCase\SchemaUseCase.cs" />
259260
<Compile Include="UseCase\ServiceStack_OrmLite_UseCase.cs" />
260261
<Compile Include="UseCase\ShardingUseCase.cs" />
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using System.Reflection;
6+
using NUnit.Framework;
7+
using ServiceStack.OrmLite.Dapper;
8+
9+
namespace ServiceStack.OrmLite.Tests.UseCase
10+
{
11+
class Post
12+
{
13+
public int Id { get; set; }
14+
public string Text { get; set; }
15+
public DateTime CreationDate { get; set; }
16+
public DateTime LastChangeDate { get; set; }
17+
public int? Counter1 { get; set; }
18+
public int? Counter2 { get; set; }
19+
public int? Counter3 { get; set; }
20+
public int? Counter4 { get; set; }
21+
public int? Counter5 { get; set; }
22+
public int? Counter6 { get; set; }
23+
public int? Counter7 { get; set; }
24+
public int? Counter8 { get; set; }
25+
public int? Counter9 { get; set; }
26+
27+
}
28+
29+
public class PostPerfTests : OrmLiteTestBase
30+
{
31+
public PostPerfTests()
32+
{
33+
Dialect = Dialect.SqlServer2012;
34+
}
35+
36+
private void EnsureDBSetup()
37+
{
38+
using (var cnn = OpenDbConnection().ToDbConnection())
39+
{
40+
var cmd = cnn.CreateCommand();
41+
cmd.CommandText = @"
42+
if (OBJECT_ID('Post') is null)
43+
begin
44+
create table Post
45+
(
46+
Id int identity primary key,
47+
[Text] varchar(max) not null,
48+
CreationDate datetime not null,
49+
LastChangeDate datetime not null,
50+
Counter1 int,
51+
Counter2 int,
52+
Counter3 int,
53+
Counter4 int,
54+
Counter5 int,
55+
Counter6 int,
56+
Counter7 int,
57+
Counter8 int,
58+
Counter9 int
59+
)
60+
61+
set nocount on
62+
63+
declare @i int
64+
declare @c int
65+
66+
declare @id int
67+
68+
set @i = 0
69+
70+
while @i < 5000
71+
begin
72+
73+
insert Post ([Text],CreationDate, LastChangeDate) values (replicate('x', 2000), GETDATE(), GETDATE())
74+
set @id = @@IDENTITY
75+
76+
set @i = @i + 1
77+
end
78+
end
79+
";
80+
cmd.Connection = cnn;
81+
cmd.ExecuteNonQuery();
82+
}
83+
}
84+
85+
class Test
86+
{
87+
public static Test Create(Action<int> iteration, string name)
88+
{
89+
return new Test { Iteration = iteration, Name = name };
90+
}
91+
92+
public Action<int> Iteration { get; set; }
93+
public string Name { get; set; }
94+
public Stopwatch Watch { get; set; }
95+
}
96+
97+
class Tester : List<Test>
98+
{
99+
public void Add(Action<int> iteration, string name)
100+
{
101+
Add(Test.Create(iteration, name));
102+
}
103+
104+
public void Run(int iterations)
105+
{
106+
// warmup
107+
foreach (var test in this)
108+
{
109+
test.Iteration(iterations + 1);
110+
test.Watch = new Stopwatch();
111+
test.Watch.Reset();
112+
}
113+
114+
var rand = new Random();
115+
for (int i = 1; i <= iterations; i++)
116+
{
117+
foreach (var test in this.OrderBy(ignore => rand.Next()))
118+
{
119+
test.Watch.Start();
120+
test.Iteration(i);
121+
test.Watch.Stop();
122+
}
123+
}
124+
125+
foreach (var test in this.OrderBy(t => t.Watch.ElapsedMilliseconds))
126+
{
127+
Console.WriteLine(test.Name + " took " + test.Watch.ElapsedMilliseconds + "ms");
128+
}
129+
}
130+
}
131+
132+
[Test]
133+
public void Run_single_select_Dapper()
134+
{
135+
var tester = new Tester();
136+
137+
var db = OpenDbConnection();
138+
tester.Add(id => db.Query<Post>("select * from Post where Id = @Id", new { Id = id }).ToList(), "Mapper Query");
139+
140+
tester.Run(500);
141+
}
142+
143+
[Test]
144+
public void Run_single_select()
145+
{
146+
var tester = new Tester();
147+
148+
var db = OpenDbConnection();
149+
tester.Add(id => db.SelectFmt<Post>("select * from Post where Id = {0}", id), "OrmLite Query");
150+
151+
tester.Run(500);
152+
}
153+
154+
[Test]
155+
public void Run_multi_select_Dapper()
156+
{
157+
var tester = new Tester();
158+
159+
var db = OpenDbConnection();
160+
tester.Add(id => db.Query<Post>("select top 1000 * from Post").ToList(), "Mapper Query");
161+
162+
tester.Run(50);
163+
}
164+
165+
[Test]
166+
public void Run_multi_select_OrmLite()
167+
{
168+
var tester = new Tester();
169+
170+
var db = OpenDbConnection();
171+
tester.Add(id => db.SelectFmt<Post>("select top 1000 * from Post"), "OrmLite Query");
172+
173+
tester.Run(50);
174+
}
175+
176+
[Test]
177+
public void Run_multi_select_OrmLite_SqlExpression()
178+
{
179+
var tester = new Tester();
180+
181+
var db = OpenDbConnection();
182+
tester.Add(id => db.Select<Post>(q => q.Limit(1000)), "OrmLite Query Expression");
183+
184+
tester.Run(50);
185+
}
186+
}
187+
}

0 commit comments

Comments
 (0)