Skip to content

Use ConcurrentDictionary instead of SynchronizedDictionary#744

Closed
Youssef1313 wants to merge 5 commits intocastleproject:masterfrom
Youssef1313:conc-dict
Closed

Use ConcurrentDictionary instead of SynchronizedDictionary#744
Youssef1313 wants to merge 5 commits intocastleproject:masterfrom
Youssef1313:conc-dict

Conversation

@Youssef1313
Copy link
Copy Markdown

Fixes #743

@Youssef1313
Copy link
Copy Markdown
Author

While trying to write a perf test to this, it looks like the changes here somehow ended up making stuff unstable. Here is the test for reference:

// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Castle.DynamicProxy.Tests
{
	using System;
	using System.Diagnostics;
	using System.Linq;
	using System.Reflection;
	using System.Security.Permissions;
	using System.Threading.Tasks;

	using Castle.Core.Logging;
	using Castle.DynamicProxy.Generators;
	using Castle.DynamicProxy.Tests.Classes;

	using NUnit.Framework;

	[TestFixture]
	public class ProxyGeneratorPerfTest
	{
		[Test]
		public async Task Perf_Under_Heavy_Load()
		{
			var generator = new ProxyGenerator();
			int stressLevel = Environment.ProcessorCount;

			Type[] types = [typeof(I1), typeof(I2), typeof(I3), typeof(I4), typeof(I5)];
			var tasks = new Task[stressLevel];
			for (int i = 0; i < stressLevel; i++)
			{
				var iCopy = i;
				tasks[i] = Task.Run(() => generator.CreateInterfaceProxyWithoutTarget(types[iCopy % types.Length]));
			}

			var sw = Stopwatch.StartNew();

			for (int i = 0; i < 500; i++)
			{
				await Task.Delay(2);
			}
			sw.Stop();
			var elapsed = sw.ElapsedMilliseconds;

			await Task.WhenAll(tasks);
		}
	}

	internal interface I1
	{
		void M1();
		void M2();
	}

	internal interface I2
	{
		void M1();
		void M2();
	}

	internal interface I3
	{
		void M1();
		void M2();
	}

	internal interface I4
	{
		void M1();
		void M2();
	}

	internal interface I5
	{
		void M1();
		void M2();
	}
}

It's stable on master but not with this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider replacing SynchronizedDictionary with ConcurrentDictionary

1 participant