Skip to content

Commit 475a789

Browse files
committed
Add StressTest csproj
1 parent 868e6d6 commit 475a789

File tree

4 files changed

+161
-1
lines changed

4 files changed

+161
-1
lines changed

Enyim.Caching/Enyim.Caching.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<Description>EnyimMemcachedCore is a Memcached client library for .NET Core. Usage: Add services.AddEnyimMemcached(...) and app.UseEnyimMemcached() in Startup. Add IMemcachedClient into constructor.</Description>
5-
<VersionPrefix>2.1.13</VersionPrefix>
5+
<VersionPrefix>2.2.0-beta1</VersionPrefix>
66
<Authors>cnblogs.com</Authors>
77
<TargetFramework>netstandard2.0</TargetFramework>
88
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.2</TargetFramework>
6+
<LangVersion>latest</LangVersion>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
11+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.2.0" />
12+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
13+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
14+
</ItemGroup>
15+
<ItemGroup>
16+
<ProjectReference Include="..\Enyim.Caching\Enyim.Caching.csproj" />
17+
</ItemGroup>
18+
19+
</Project>

Enyim.StressTest/Program.cs

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
using Enyim.Caching;
2+
using Microsoft.Extensions.Configuration;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Logging;
5+
using System;
6+
using System.Diagnostics;
7+
using System.Linq;
8+
using System.Threading;
9+
using System.Threading.Tasks;
10+
11+
namespace Enyim.StressTest
12+
{
13+
class Foo
14+
{
15+
public int[] Numbers { get; set; }
16+
public DateTime DateTime { get; set; }
17+
}
18+
19+
//From https://github.com/ZeekoZhu/memcachedcore-stress
20+
class Program
21+
{
22+
static async Task Main(string[] args)
23+
{
24+
var services = new ServiceCollection();
25+
services.AddEnyimMemcached(options => options.AddServer("memcached", 11211));
26+
services.AddLogging(x => x.AddConsole().SetMinimumLevel(LogLevel.Debug));
27+
await Run(services.BuildServiceProvider());
28+
}
29+
30+
static async Task TrySingle(IServiceProvider sp)
31+
{
32+
using (var scope = sp.CreateScope())
33+
{
34+
var memcached = scope.ServiceProvider.GetService<IMemcachedClient>();
35+
memcached.Add("test", new Foo(), Int32.MaxValue);
36+
var test = await memcached.GetValueAsync<Foo>("test");
37+
Console.WriteLine("Single Run: {0}", test.DateTime);
38+
}
39+
}
40+
41+
static async Task RunSync(int cnt, IServiceProvider sp)
42+
{
43+
Console.WriteLine("Use Get");
44+
await TrySingle(sp);
45+
var sw = Stopwatch.StartNew();
46+
var obj = new object();
47+
var errCnt = 0;
48+
var tasks =
49+
Enumerable.Range(0, cnt)
50+
.Select(i => Task.Run(() =>
51+
{
52+
using (var scope = sp.CreateScope())
53+
{
54+
var provider = scope.ServiceProvider;
55+
var memcached = provider.GetService<IMemcachedClient>();
56+
try
57+
{
58+
var foo = memcached.Get<Foo>("test");
59+
if (foo == null)
60+
{
61+
throw new Exception();
62+
}
63+
}
64+
catch (Exception e)
65+
{
66+
lock (obj)
67+
{
68+
errCnt += 1;
69+
}
70+
71+
// Console.WriteLine("Task: {0} Exception: {1}", i, e.GetType().FullName);
72+
}
73+
}
74+
}));
75+
await Task.WhenAll(tasks);
76+
sw.Stop();
77+
Thread.Sleep(TimeSpan.FromSeconds(3));
78+
await TrySingle(sp);
79+
Console.WriteLine($"Time: {sw.ElapsedMilliseconds}ms");
80+
Console.WriteLine($"Error Cnt: {errCnt}");
81+
Console.WriteLine($"Avg: {Convert.ToDouble(sw.ElapsedMilliseconds) / Convert.ToDouble(cnt)}ms");
82+
}
83+
84+
static async Task RunAsync(int cnt, IServiceProvider sp)
85+
{
86+
Console.WriteLine("Use GetValueAsync");
87+
await TrySingle(sp);
88+
var sw = Stopwatch.StartNew();
89+
var obj = new object();
90+
var errCnt = 0;
91+
var tasks =
92+
Enumerable.Range(0, cnt)
93+
.Select(i => Task.Run(async () =>
94+
{
95+
using (var scope = sp.CreateScope())
96+
{
97+
var provider = scope.ServiceProvider;
98+
var memcached = provider.GetService<IMemcachedClient>();
99+
try
100+
{
101+
var foo = await memcached.GetValueAsync<Foo>("test");
102+
if (foo == null)
103+
{
104+
throw new Exception();
105+
}
106+
}
107+
catch (Exception e)
108+
{
109+
lock (obj)
110+
{
111+
errCnt += 1;
112+
}
113+
114+
//Console.WriteLine("Task: {0} Exception: {1}", i, e.GetType().FullName);
115+
}
116+
}
117+
}));
118+
await Task.WhenAll(tasks);
119+
sw.Stop();
120+
Thread.Sleep(TimeSpan.FromSeconds(3));
121+
await TrySingle(sp);
122+
Console.WriteLine($"Time: {sw.ElapsedMilliseconds}ms");
123+
Console.WriteLine($"Error Cnt: {errCnt}");
124+
Console.WriteLine($"Avg: {Convert.ToDouble(sw.ElapsedMilliseconds) / Convert.ToDouble(cnt)}ms");
125+
}
126+
127+
static async Task Run(IServiceProvider sp)
128+
{
129+
var cnt = 1000000;
130+
await RunAsync(cnt, sp);
131+
await RunSync(cnt, sp);
132+
}
133+
}
134+
}

EnyimCachingCore.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sample", "sample", "{9954C9
1919
EndProject
2020
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleWebApp.IntegrationTests", "SampleWebApp.IntegrationTests\SampleWebApp.IntegrationTests.csproj", "{281CA49A-FEA5-4008-8D3D-0C4E1C548B8C}"
2121
EndProject
22+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Enyim.StressTest", "Enyim.StressTest\Enyim.StressTest.csproj", "{ADA7F6B5-619D-4435-83BF-AD558823EC40}"
23+
EndProject
2224
Global
2325
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2426
Debug|Any CPU = Debug|Any CPU
@@ -45,6 +47,10 @@ Global
4547
{281CA49A-FEA5-4008-8D3D-0C4E1C548B8C}.Debug|Any CPU.Build.0 = Debug|Any CPU
4648
{281CA49A-FEA5-4008-8D3D-0C4E1C548B8C}.Release|Any CPU.ActiveCfg = Release|Any CPU
4749
{281CA49A-FEA5-4008-8D3D-0C4E1C548B8C}.Release|Any CPU.Build.0 = Release|Any CPU
50+
{ADA7F6B5-619D-4435-83BF-AD558823EC40}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
51+
{ADA7F6B5-619D-4435-83BF-AD558823EC40}.Debug|Any CPU.Build.0 = Debug|Any CPU
52+
{ADA7F6B5-619D-4435-83BF-AD558823EC40}.Release|Any CPU.ActiveCfg = Release|Any CPU
53+
{ADA7F6B5-619D-4435-83BF-AD558823EC40}.Release|Any CPU.Build.0 = Release|Any CPU
4854
EndGlobalSection
4955
GlobalSection(SolutionProperties) = preSolution
5056
HideSolutionNode = FALSE
@@ -55,6 +61,7 @@ Global
5561
{FAF5E655-9ED4-4934-94F9-F0EDB77143AC} = {46CD2BBA-9846-42FA-823B-D03F68F81647}
5662
{3DDAC26F-8B56-4F69-8C69-A3CB6CF6B12B} = {46CD2BBA-9846-42FA-823B-D03F68F81647}
5763
{281CA49A-FEA5-4008-8D3D-0C4E1C548B8C} = {9954C92E-50E2-477C-AC47-31C857C46370}
64+
{ADA7F6B5-619D-4435-83BF-AD558823EC40} = {46CD2BBA-9846-42FA-823B-D03F68F81647}
5865
EndGlobalSection
5966
GlobalSection(ExtensibilityGlobals) = postSolution
6067
SolutionGuid = {D8C07FE8-52FF-48B8-A079-A180058AABC6}

0 commit comments

Comments
 (0)