// Copyright Christophe Bertrand.
//#define TIMER_DEBUGGING
using System;
using System.Collections;
using System.Windows;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Windows.Media;
using System.IO;
namespace UniversalSerializerResourceTests
{
public enum StreamManagement { SerializeInRAM, SerializeToFile };
/// <summary>
/// Logique d'interaction pour MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public IEnumerable SerializersCollection { get {
return
CommonOptions.SerializerInstances.Select((ser) => ser.Name);
} }
public IEnumerable DataCollection
{ get
{ return AvailableDataDescriptors.Descriptors; } }
public IEnumerable StreamManagementCollection
{
get
{
return Enum.GetValues(typeof(StreamManagement));
}
}
ObservableCollection<string> LogStrings { get; set; }
ObservableCollection<string> SeriesLogStrings { get; set; }
readonly Brush NormalTextboxBackgroundColor;
readonly Brush OrangeBrush = new SolidColorBrush(new Color() { R = 255, G = 160, B = 80, A = 255 });
public MainWindow()
{
InitializeComponent();
this.NormalTextboxBackgroundColor = this.TableFileName.Background;
this.TableFileName.TextChanged += TableFileName_TextChanged;
this.LogStrings = new ObservableCollection<string>();
this.Log.ItemsSource = this.LogStrings;
this.SeriesLogStrings = new ObservableCollection<string>();
this.SeriesLog.ItemsSource = this.SeriesLogStrings;
}
void TableFileName_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
bool PathExists;
try
{
PathExists = Directory.Exists(Path.GetDirectoryName(TableFileName.Text));
}
catch
{
PathExists = false;
}
this.TableFileName.Background =
PathExists ?
this.NormalTextboxBackgroundColor
: OrangeBrush;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.UnitTestOptions.Compute(CommonOptions.SerializerInstances[this.Serializer.SelectedIndex], null,null);
}
private void Window_Closed(object sender, EventArgs e)
{
//Application.Current.Shutdown(); // closes any dynamic window as well.
}
private void ButtonRunWholeTest_Click(object sender, RoutedEventArgs e)
{
this.LogStrings.Clear();
this.WholeTestOptions.Compute(null, this.LogStrings, this.TableFileName.Text);
}
private void StartSeries_Click(object sender, RoutedEventArgs e)
{
this.SeriesLogStrings.Clear();
}
}
// #############################################################################
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 start;
long end;
readonly long freq;
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();
this.WorkingSet64AtStart = Process.GetCurrentProcess().WorkingSet64;
}
{
this.initialGCMemory = System.GC.GetTotalMemory(true);
}
timer = new System.Threading.Timer(TimerCallback, null, 60, 60);
if (!QueryPerformanceFrequency(ref freq))
throw new Exception();
if (!QueryPerformanceCounter(ref this.start))
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.start) * 1.0e3 / (double)freq;
#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();
}
}
// #############################################################################
// #############################################################################
// #############################################################################
}