Measure the degree of Parallelism in TPL

The parallel task library and TPL data stream let you specify the maximum degree of parallelism. This value is a limitation, not a guarantee. Indeed, TPL will determine the actual degree of parallelism based on a number of factors, including system resources that do not exceed the maximum maximum programmable.

Is there any mechanism for determining the choice that the TPL made for the degree of parallelism at a particular point in time?

I ask specifically because I have ported quite complex code to use the TPL data stream, and the overall throughput is much less than the source code. I would like to see what options TPL made on my behalf to understand why it is much slower.

+4
source share
1 answer

I faced a very similar situation with yours. I ended up using my log data to tell me about how many threads were used per minute. This gave me an approximate number, but not an exact one.

I do not believe that TPL can give you telemetry when using streams. If you want to implement something more precise, I would recommend that you implement the logic in each task / thread in order to mark some kind of general list of start time and its completion time. Here is an example of how I approach him to get started.

public class DoSomeTPLWork { public static void Start() { List<int> numberList = Enumerable.Range(1, 1000).ToList(); Parallel.ForEach(numberList, number => { ThreadTracking.ThreadStarted(); int square = number * number; Console.WriteLine("Square of {0} is {1}", number, square); ThreadTracking.ThreadFinished(); } ); var threadInfo = ThreadTracking.GetThreadInfo(); } } public class ThreadTracking { private static ConcurrentBag<ThreadInfo> _threadInfo = new ConcurrentBag<ThreadInfo>(); public static void ThreadStarted() { var threadInfo = new ThreadInfo(Thread.CurrentThread.ManagedThreadId); threadInfo.Start(); _threadInfo.Add(threadInfo); } public static void ThreadFinished() { var threadInfo = _threadInfo.Where(ti => ti.ThreadId == Thread.CurrentThread.ManagedThreadId && !ti.Complete).SingleOrDefault(); if(threadInfo != null) { threadInfo.Stop(); } } public static List<ThreadInfo> GetThreadInfo() { return _threadInfo.ToList(); } } public class ThreadInfo { public bool Complete { get; set; } public int ThreadId { get; set; } public DateTime? TimeStarted { get; set; } public DateTime? TimeFinished { get; set; } public ThreadInfo(int threadId) { ThreadId = threadId; } public void Start() { TimeStarted = DateTime.Now; Complete = false; } public void Stop() { TimeFinished = DateTime.Now; Complete = true; } } 

Using the data, you can see how many threads are being used for any second, adding a few more methods to query the data or simply pasting them into Excel to play with it.

+3
source

Source: https://habr.com/ru/post/1443331/


All Articles