Click here to Skip to main content
15,892,537 members
Articles / Programming Languages / C#

UniversalSerializer

Rate me:
Please Sign up or sign in to vote.
4.97/5 (108 votes)
15 Apr 2018Ms-RL31 min read 262.5K   4K   299  
An universal and easy serialization library for .NET and .NET Core.

// Copyright Christophe Bertrand.

//#define TIMER_DEBUGGING

using System;
using System.Diagnostics;
using System.Threading;

namespace UniversalSerializerResourceTests
{


	// #############################################################################

	public class ResourceCounter : IDisposable
	{
		[System.Runtime.InteropServices.DllImport("KERNEL32")]
		private static extern bool QueryPerformanceCounter(
				ref long lpPerformanceCount);

		[System.Runtime.InteropServices.DllImport("KERNEL32")]
		private static extern bool QueryPerformanceFrequency(
				ref long lpFrequency);

		readonly long startTime;
		long end;
		readonly long freqTime;
		public double ElapsedTimeInMs;

		readonly long WorkingSet64AtStart;
		public long WorkingSet64ConsumptionPeak;

		long WorkingSet64Peak;
		long GCPeak;
#if TIMER_DEBUGGING
		long[] times=new long[500];
		int timerCounter;
#endif


		readonly long initialGCMemory;
		public long GCConsumptionPeak;

		readonly System.Threading.Timer timer;

		public ResourceCounter()
		{
			{
				GC.Collect();
				GC.WaitForPendingFinalizers();
				GC.Collect();

				GCNotificationStatus ns = GC.WaitForFullGCComplete(300);

				this.WorkingSet64AtStart = Process.GetCurrentProcess().WorkingSet64;
			}

			{
				this.initialGCMemory = System.GC.GetTotalMemory(true);
				//Thread.Sleep(600);
			}

			timer = new System.Threading.Timer(TimerCallback, null, 60, 60);

			if (!QueryPerformanceFrequency(ref freqTime))
				throw new Exception();
			if (!QueryPerformanceCounter(ref this.startTime))
				throw new Exception();
		}

		// We measure the RAM usage periodically.
		void TimerCallback(object state)
		{
			var currentWS = Process.GetCurrentProcess().WorkingSet64;
			if (currentWS > this.WorkingSet64Peak)
				this.WorkingSet64Peak = currentWS;
			var currentGC = System.GC.GetTotalMemory(false);
			if (currentGC > this.GCPeak)
				this.GCPeak = currentGC;
#if TIMER_DEBUGGING
			long act=0;
			QueryPerformanceCounter(ref act);
			this.times[this.timerCounter++] = act;
#endif
		}

		public void StopAndGetResourceMesures()
		{
			if (!QueryPerformanceCounter(ref this.end))
				throw new Exception();
			this.timer.Dispose();
			long WorkingSet64AtEnd = Process.GetCurrentProcess().WorkingSet64;
			var finalMemory = System.GC.GetTotalMemory(false);


			this.WorkingSet64ConsumptionPeak = Math.Max(WorkingSet64AtEnd, this.WorkingSet64Peak) - this.WorkingSet64AtStart;
			this.GCConsumptionPeak = Math.Max(finalMemory, this.GCPeak) - initialGCMemory;
			this.ElapsedTimeInMs = (double)(this.end - this.startTime) * 1.0e3 / (double)freqTime;


#if TIMER_DEBUGGING
			double[] dtemps = new double[this.timerCounter];
			for (int i = 0; i < this.timerCounter; i++)
				dtemps[i] = (double)(this.times[i] - this.start) * 1.0e3 / (double)freq;
#endif

		}

		public void Dispose()
		{
			this.timer.Dispose();
		}
	}

	// #############################################################################
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under Microsoft Reciprocal License


Written By
Software Developer (Senior) independent
France France
Hi !

I made my first program on a Sinclair ZX-81.
Since that day, I have the virus of computers. Smile | :)

Here is my website:
https://chrisbertrand.net

And my blog:
https://chrisbertrandprogramer.wordpress.com/

Comments and Discussions