From 25551e698586544ef9af1a97a6fb15eed91f03ef Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Wed, 27 Feb 2013 13:48:10 -0600 Subject: [PATCH] Update TestPerformance to run single-threaded prior to .NET 4 (TaskScheduler is not available before that) --- .../Antlr4.Runtime.Test.v3.5.csproj | 1 - .../Antlr4.Runtime.Test/TestPerformance.cs | 50 ++++++++++++++++--- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/runtime/CSharp/Antlr4.Runtime.Test/Antlr4.Runtime.Test.v3.5.csproj b/runtime/CSharp/Antlr4.Runtime.Test/Antlr4.Runtime.Test.v3.5.csproj index f1391c9db..7d50f9b4e 100644 --- a/runtime/CSharp/Antlr4.Runtime.Test/Antlr4.Runtime.Test.v3.5.csproj +++ b/runtime/CSharp/Antlr4.Runtime.Test/Antlr4.Runtime.Test.v3.5.csproj @@ -53,7 +53,6 @@ - diff --git a/runtime/CSharp/Antlr4.Runtime.Test/TestPerformance.cs b/runtime/CSharp/Antlr4.Runtime.Test/TestPerformance.cs index c9c95248e..6a0dfeb07 100644 --- a/runtime/CSharp/Antlr4.Runtime.Test/TestPerformance.cs +++ b/runtime/CSharp/Antlr4.Runtime.Test/TestPerformance.cs @@ -1,12 +1,9 @@ namespace Antlr4.Runtime.Test { using System; - using System.Collections.Concurrent; using System.Collections.Generic; using System.Reflection; using System.Text; - using System.Threading.Tasks; - using System.Threading.Tasks.Schedulers; using Antlr4.Runtime; using Antlr4.Runtime.Atn; using Antlr4.Runtime.Dfa; @@ -14,7 +11,6 @@ using Antlr4.Runtime.Tree; using Microsoft.VisualStudio.TestTools.UnitTesting; using Sharpen; - using CancellationToken = System.Threading.CancellationToken; using Debug = System.Diagnostics.Debug; using DirectoryInfo = System.IO.DirectoryInfo; using FileInfo = System.IO.FileInfo; @@ -25,6 +21,13 @@ using StreamReader = System.IO.StreamReader; using Thread = System.Threading.Thread; +#if NET_4_0 + using System.Collections.Concurrent; + using System.Threading.Tasks; + using System.Threading.Tasks.Schedulers; + using CancellationToken = System.Threading.CancellationToken; +#endif + [TestClass] public class TestPerformance : BaseTest { @@ -184,6 +187,10 @@ /** * Number of parser threads to use. + * + * + * This value is ignored for .NET Framework 3.5 and earlier. + * */ private static readonly int NUMBER_OF_THREADS = 1; @@ -373,6 +380,7 @@ int sourceCount = 0; int inputSize = 0; +#if NET_4_0 BlockingCollection threadIdentifiers = new BlockingCollection(); for (int i = 0; i < NUMBER_OF_THREADS; i++) threadIdentifiers.Add(i); @@ -380,27 +388,39 @@ ICollection> results = new List>(); QueuedTaskScheduler executorServiceHost = new QueuedTaskScheduler(NUMBER_OF_THREADS); TaskScheduler executorService = executorServiceHost.ActivateNewQueue(); +#else + ICollection> results = new List>(); +#endif foreach (ICharStream input in sources) { sourceCount++; input.Seek(0); inputSize += input.Size; +#if NET_4_0 Task futureChecksum = Task.Factory.StartNew(new Callable_1(input, factory, threadIdentifiers).call, CancellationToken.None, TaskCreationOptions.None, executorService); - +#else + Func futureChecksum = new Callable_1(input, factory).call; +#endif results.Add(futureChecksum); } Checksum checksum = new CRC32(); - foreach (Task future in results) + foreach (var future in results) { +#if NET_4_0 int value = future.Result; +#else + int value = future(); +#endif if (COMPUTE_CHECKSUM) { updateChecksum(checksum, value); } } +#if NET_4_0 executorServiceHost.Dispose(); +#endif Console.Out.WriteLine("Total parse time for {0} files ({1} KB, {2} tokens, checksum 0x{3:X8}): {4}ms", sourceCount, @@ -418,7 +438,7 @@ { int states = 0; int configs = 0; - ISet uniqueConfigs = new HashSet(); + HashSet uniqueConfigs = new HashSet(); for (int i = 0; i < modeToDFA.Length; i++) { @@ -451,7 +471,7 @@ { int states = 0; int configs = 0; - ISet uniqueConfigs = new HashSet(); + HashSet uniqueConfigs = new HashSet(); for (int i = 0; i < decisionToDFA.Length; i++) { @@ -566,27 +586,41 @@ { private readonly ICharStream input; private readonly ParserFactory factory; +#if NET_4_0 private readonly BlockingCollection threadNumbers; +#endif +#if NET_4_0 public Callable_1(ICharStream input, ParserFactory factory, BlockingCollection threadNumbers) +#else + public Callable_1(ICharStream input, ParserFactory factory) +#endif { this.input = input; this.factory = factory; +#if NET_4_0 this.threadNumbers = threadNumbers; +#endif } public int call() { // this incurred a great deal of overhead and was causing significant variations in performance results. //Console.Out.WriteLine("Parsing file {0}", input.getSourceName()); +#if NET_4_0 int threadNumber = threadNumbers.Take(); +#else + int threadNumber = 0; +#endif try { return factory.parseFile(input, threadNumber); } finally { +#if NET_4_0 threadNumbers.Add(threadNumber); +#endif } } }