Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit 3ff60c1

Browse files
committed
Add stress test
1 parent e27a4f9 commit 3ff60c1

12 files changed

+325
-39
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using Titanium.Web.Proxy.Network;
4+
using System.Threading.Tasks;
5+
using System.Collections.Generic;
6+
7+
namespace Titanium.Web.Proxy.UnitTests
8+
{
9+
[TestClass]
10+
public class CertificateManagerTests
11+
{
12+
private readonly static string[] hostNames
13+
= new string[] { "facebook.com", "youtube.com", "google.com",
14+
"bing.com", "yahoo.com"};
15+
16+
private readonly Random random = new Random();
17+
18+
[TestMethod]
19+
public async Task Simple_Create_Certificate_Stress_Test()
20+
{
21+
var tasks = new List<Task>();
22+
23+
var mgr = new CertificateManager("Titanium","Titanium Root Certificate Authority");
24+
25+
mgr.ClearIdleCertificates(1);
26+
27+
for (int i = 0; i < 1000; i++)
28+
{
29+
foreach (var host in hostNames)
30+
{
31+
tasks.Add(Task.Run(async () =>
32+
{
33+
await Task.Delay(random.Next(0, 10) * 1000);
34+
35+
//get the connection
36+
var certificate = await mgr.CreateCertificate(host, false);
37+
38+
Assert.IsNotNull(certificate);
39+
40+
}));
41+
42+
}
43+
}
44+
45+
await Task.WhenAll(tasks.ToArray());
46+
47+
mgr.StopClearIdleCertificates();
48+
49+
}
50+
}
51+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using Titanium.Web.Proxy.Network;
4+
using System.Threading.Tasks;
5+
using System.Collections.Generic;
6+
using System.Diagnostics;
7+
8+
namespace Titanium.Web.Proxy.UnitTests
9+
{
10+
[TestClass]
11+
public class ConnectionManagerTests
12+
{
13+
private readonly static string[] hostNames
14+
= new string[] { "facebook.com", "youtube.com", "google.com",
15+
"bing.com", "yahoo.com"};
16+
17+
private readonly Random random = new Random();
18+
19+
[TestMethod]
20+
public async Task Simple_Create_Connection_Stress_Test()
21+
{
22+
var tasks = new List<Task>();
23+
24+
var mgr = new TcpConnectionManager();
25+
26+
mgr.ClearIdleConnections(1);
27+
28+
for (int i = 0; i < 1000; i++)
29+
{
30+
foreach (var host in hostNames)
31+
{
32+
tasks.Add(Task.Run(async () =>
33+
{
34+
await Task.Delay(random.Next(0, 10) * 1000);
35+
36+
//get the connection
37+
var httpConnection = await mgr.GetClient(host, 80, false, new Version(1, 1), null, null,
38+
8096, System.Security.Authentication.SslProtocols.Default,
39+
120, null, null);
40+
41+
//simulate a work with the connection
42+
await Task.Delay(random.Next(0, 10) * 1000);
43+
44+
//print total number of connections on cache
45+
Debug.WriteLine(mgr.connectionCache.Count);
46+
47+
Assert.IsNotNull(httpConnection);
48+
Assert.IsNotNull(httpConnection.TcpClient);
49+
Assert.IsTrue(httpConnection.TcpClient.Connected);
50+
51+
//release
52+
await mgr.ReleaseClient(httpConnection);
53+
}));
54+
55+
tasks.Add(Task.Run(async () =>
56+
{
57+
await Task.Delay(random.Next(0, 10) * 1000);
58+
//get the connection
59+
var httpsConnection = await mgr.GetClient(host, 443, true, new Version(1, 1), null, null,
60+
8096, System.Security.Authentication.SslProtocols.Default,
61+
120, null, null);
62+
63+
//simulate a work with the connection
64+
await Task.Delay(random.Next(0, 10) * 1000);
65+
66+
//print total number of connections on cache
67+
Debug.WriteLine(mgr.connectionCache.Count);
68+
69+
Assert.IsNotNull(httpsConnection);
70+
Assert.IsNotNull(httpsConnection.TcpClient);
71+
Assert.IsTrue(httpsConnection.TcpClient.Connected);
72+
73+
//release
74+
await mgr.ReleaseClient(httpsConnection);
75+
}));
76+
}
77+
}
78+
79+
await Task.WhenAll(tasks.ToArray());
80+
81+
mgr.StopClearIdleConnections();
82+
83+
}
84+
}
85+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("Titanium.Web.Proxy.UnitTests")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("Titanium.Web.Proxy.UnitTests")]
13+
[assembly: AssemblyCopyright("Copyright © 2016")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("b517e3d0-d03b-436f-ab03-34ba0d5321af")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProjectGuid>{B517E3D0-D03B-436F-AB03-34BA0D5321AF}</ProjectGuid>
7+
<OutputType>Library</OutputType>
8+
<AppDesignerFolder>Properties</AppDesignerFolder>
9+
<RootNamespace>Titanium.Web.Proxy.UnitTests</RootNamespace>
10+
<AssemblyName>Titanium.Web.Proxy.UnitTests</AssemblyName>
11+
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
14+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
15+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
16+
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
17+
<IsCodedUITest>False</IsCodedUITest>
18+
<TestProjectType>UnitTest</TestProjectType>
19+
</PropertyGroup>
20+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
21+
<DebugSymbols>true</DebugSymbols>
22+
<DebugType>full</DebugType>
23+
<Optimize>false</Optimize>
24+
<OutputPath>bin\Debug\</OutputPath>
25+
<DefineConstants>DEBUG;TRACE</DefineConstants>
26+
<ErrorReport>prompt</ErrorReport>
27+
<WarningLevel>4</WarningLevel>
28+
</PropertyGroup>
29+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
30+
<DebugType>pdbonly</DebugType>
31+
<Optimize>true</Optimize>
32+
<OutputPath>bin\Release\</OutputPath>
33+
<DefineConstants>TRACE</DefineConstants>
34+
<ErrorReport>prompt</ErrorReport>
35+
<WarningLevel>4</WarningLevel>
36+
</PropertyGroup>
37+
<ItemGroup>
38+
<Reference Include="System" />
39+
</ItemGroup>
40+
<Choose>
41+
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
42+
<ItemGroup>
43+
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
44+
</ItemGroup>
45+
</When>
46+
<Otherwise>
47+
<ItemGroup>
48+
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework" />
49+
</ItemGroup>
50+
</Otherwise>
51+
</Choose>
52+
<ItemGroup>
53+
<Compile Include="CertificateManagerTests.cs" />
54+
<Compile Include="ConnectionManagerTests.cs" />
55+
<Compile Include="Properties\AssemblyInfo.cs" />
56+
</ItemGroup>
57+
<ItemGroup>
58+
<ProjectReference Include="..\..\Titanium.Web.Proxy\Titanium.Web.Proxy.csproj">
59+
<Project>{8d73a1be-868c-42d2-9ece-f32cc1a02906}</Project>
60+
<Name>Titanium.Web.Proxy</Name>
61+
</ProjectReference>
62+
</ItemGroup>
63+
<Choose>
64+
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
65+
<ItemGroup>
66+
<Reference Include="Microsoft.VisualStudio.QualityTools.CodedUITestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
67+
<Private>False</Private>
68+
</Reference>
69+
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
70+
<Private>False</Private>
71+
</Reference>
72+
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Extension, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
73+
<Private>False</Private>
74+
</Reference>
75+
<Reference Include="Microsoft.VisualStudio.TestTools.UITesting, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
76+
<Private>False</Private>
77+
</Reference>
78+
</ItemGroup>
79+
</When>
80+
</Choose>
81+
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
82+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
83+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
84+
Other similar extension points exist, see Microsoft.Common.targets.
85+
<Target Name="BeforeBuild">
86+
</Target>
87+
<Target Name="AfterBuild">
88+
</Target>
89+
-->
90+
</Project>

Titanium.Web.Proxy.sln

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build", "Build", "{AC9AE37A
2929
.build\default.ps1 = .build\default.ps1
3030
EndProjectSection
3131
EndProject
32+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{BC1E0789-D348-49CF-8B67-5E99D50EDF64}"
33+
EndProject
34+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Titanium.Web.Proxy.UnitTests", "Tests\Titanium.Web.Proxy.UnitTests\Titanium.Web.Proxy.UnitTests.csproj", "{B517E3D0-D03B-436F-AB03-34BA0D5321AF}"
35+
EndProject
3236
Global
3337
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3438
Debug|Any CPU = Debug|Any CPU
@@ -43,12 +47,17 @@ Global
4347
{F3B7E553-1904-4E80-BDC7-212342B5C952}.Debug|Any CPU.Build.0 = Debug|Any CPU
4448
{F3B7E553-1904-4E80-BDC7-212342B5C952}.Release|Any CPU.ActiveCfg = Release|Any CPU
4549
{F3B7E553-1904-4E80-BDC7-212342B5C952}.Release|Any CPU.Build.0 = Release|Any CPU
50+
{B517E3D0-D03B-436F-AB03-34BA0D5321AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
51+
{B517E3D0-D03B-436F-AB03-34BA0D5321AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
52+
{B517E3D0-D03B-436F-AB03-34BA0D5321AF}.Release|Any CPU.ActiveCfg = Release|Any CPU
53+
{B517E3D0-D03B-436F-AB03-34BA0D5321AF}.Release|Any CPU.Build.0 = Release|Any CPU
4654
EndGlobalSection
4755
GlobalSection(SolutionProperties) = preSolution
4856
HideSolutionNode = FALSE
4957
EndGlobalSection
5058
GlobalSection(NestedProjects) = preSolution
5159
{F3B7E553-1904-4E80-BDC7-212342B5C952} = {B6DBABDC-C985-4872-9C38-B4E5079CBC4B}
60+
{B517E3D0-D03B-436F-AB03-34BA0D5321AF} = {BC1E0789-D348-49CF-8B67-5E99D50EDF64}
5261
EndGlobalSection
5362
GlobalSection(ExtensibilityGlobals) = postSolution
5463
EnterpriseLibraryConfigurationToolBinariesPath = .1.505.2\lib\NET35

Titanium.Web.Proxy/Http/HttpWebClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class HttpWebClient
1717
/// <summary>
1818
/// Connection to server
1919
/// </summary>
20-
internal TcpConnectionCache ServerConnection { get; set; }
20+
internal CachedTcpConnection ServerConnection { get; set; }
2121

2222
public Request Request { get; set; }
2323
public Response Response { get; set; }
@@ -37,7 +37,7 @@ public bool IsHttps
3737
/// Set the tcp connection to server used by this webclient
3838
/// </summary>
3939
/// <param name="Connection"></param>
40-
internal void SetConnection(TcpConnectionCache Connection)
40+
internal void SetConnection(CachedTcpConnection Connection)
4141
{
4242
Connection.LastAccess = DateTime.Now;
4343
ServerConnection = Connection;

Titanium.Web.Proxy/Network/TcpConnectionCache.cs renamed to Titanium.Web.Proxy/Network/CachedTcpConnection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Titanium.Web.Proxy.Network
88
/// <summary>
99
/// An object that holds TcpConnection to a particular server & port
1010
/// </summary>
11-
public class TcpConnectionCache
11+
public class CachedTcpConnection
1212
{
1313
internal string HostName { get; set; }
1414
internal int port { get; set; }
@@ -37,7 +37,7 @@ public class TcpConnectionCache
3737
/// </summary>
3838
internal DateTime LastAccess { get; set; }
3939

40-
internal TcpConnectionCache()
40+
internal CachedTcpConnection()
4141
{
4242
LastAccess = DateTime.Now;
4343
}

0 commit comments

Comments
 (0)