Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Thursday, December 12, 2019

Handling PubSub updates from multiple clients in pub-sub scenario with Reactive Extentions

A typical pub-sub scenario is subscribing to handle requests from multiple clients and then handling client updates in a buffered way. Let's see how this could be done using Reactive Extensions.

To make this scenario more concrete - imagine your server receives a stream of frequent updates on number of stocks (e.g. IBM_1...IBM_10 in example below). We want to process the most recent price per stock and display it to a trader at most once a second.

All we would need - is to use ReactivePubSubHandler given below, providing "selector" to identify a pub-sub client (i.e. what stock was updated) and an action to be implemented (e.g. update price display) - say once in a second.



using System;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Threading;

namespace ReactivePubSub
{
    class Program
    {
        class ClientUpdate
        {
            public string StockName { getset; }
            public float Price { getset; }
        }

        static void Main()
        {
            var sampledAction = new ReactivePubSubHandler<ClientUpdatestring>(1000, _=>_.StockName, _ => Console.WriteLine(
                $"{DateTime.Now} Processed update for client #{_.StockName} Price={_.Price}"));

            var rndStockName = new Random();
            
            for (int i = 0; i < 1000; i++)
            {
                var clientUpdate = new ClientUpdate()
                {
                    StockName = "IBM_" + rndStockName.Next(0, 10),
                    Price = (float) (rndStockName.Next(10000, 11000) / 100.0)
                };
                sampledAction.OnClientRequest(clientUpdate);
                Thread.Sleep(200);
            }
        }
    }

    public class ReactivePubSubHandler<TClientDataTClientDataKey>
    {
        public ReactivePubSubHandler(int sampleIntervalMs, Func<TClientDataTClientDataKey> pubSubTopicSelector, Action<TClientData> pubSubAction)
        {
            var sampledActionsSubscription = _clientsSubscription.GroupBy(pubSubTopicSelector)
                .Select(x => x.Sample(TimeSpan.FromMilliseconds(sampleIntervalMs))).SelectMany(x => x);
            sampledActionsSubscription.Subscribe(pubSubAction);
        }

        public void OnClientRequest(TClientData clientData)
        {
            _clientsSubscription.OnNext(clientData);
        }

        private readonly Subject<TClientData> _clientsSubscription = new Subject<TClientData>();
    }
}

Monday, May 07, 2012

GC KeepAlive method–what it really does

Quite useful, I was missing this corner..
Below is quote from documentation as it is provided by code author.



// This method DOES NOT DO ANYTHING in and of itself.  It's used to
// prevent a finalizable object from losing any outstanding references
// a touch too early. The JIT is very aggressive about keeping an
// object's lifetime to as small a window as possible, to the point
// where a 'this' pointer isn't considered live in an instance method
// unless you read a value from the instance. So for finalizable
// objects that store a handle or pointer and provide a finalizer that
// cleans them up, this can cause subtle ----s with the finalizer
// thread. This isn't just about handles - it can happen with just
// about any finalizable resource.
//
// Users should insert a call to this method near the end of a
// method where they must keep an object alive for the duration of that
// method, up until this method is called. Here is an example:
//
// "...all you really need is one object with a Finalize method, and a
// second object with a Close/Dispose/Done method. Such as the following
// contrived example:
//
// class Foo {
// Stream stream = ...;
// protected void Finalize() { stream.Close(); }
// void Problem() { stream.MethodThatSpansGCs(); }
// static void Main() { new Foo().Problem(); }
// }
//
//
// In this code, Foo will be finalized in the middle of
// stream.MethodThatSpansGCs, thus closing a stream still in use."
//
// If we insert a call to GC.KeepAlive(this) at the end of Problem(), then
// Foo doesn't get finalized and the stream says open.
[MethodImplAttribute(MethodImplOptions.InternalCall)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
public static extern void KeepAlive(Object obj);

Monday, March 01, 2010

::boost for C# - Umbrella

Nice project: http://umbrella.codeplex.com/. Provides many components similar to the ::boost basics (like bind()...) - for C#.

Technorati Tags: ,,

Wednesday, November 11, 2009

Practical tutorial on using Sgen.exe to improve performance of XML serialization startup.

There is an overhead incurred when using serialization in your application, which is caused by generation of a ‘helper’ assembly for each type that is serialized with XmlSerializer (per-process-per-domain). (Btw – this has subtle adverse effect – if you use serialization – your application should have Read/Write permissions to %windir%\temp folder).

This overhead may be avoided by using Sgen.exe tool to pre-generate serialization assemblies.
Basically it should be pretty straightforward, especially if your FooBar.dll assembly for which you pre-generating serialization assembly doesn’t has a lot of references. Unfortunately – if you do have a lot of references – you would have to add them all using /reference:c:\foo1\foo1.dll /reference:c:\foo2\foo2.dll /reference:c:\foo3\foo3.dll ……

Here is a workaround:
To generate serialization assembly manually (could be done as a post-build step):
1) Visual Studio –> Foo.csproj –> Properties –> Build –> Generate Serialization Assembly –> On
You would expect it to work automatically now – but it wouldn’t, or saying it more precisely it would create serialization assembly only if your assembly includes proxy types (e.g. web-services).
Anyway – build your assembly, open MSVC output window and look for sgen.exe line in the end of output.
2) Copy whole sgen.exe line – it would include all the references needed.
3) Remove from it /proxytypes parameter – this will cause sgen.exe to generate serializers for regular types.
4) Add /force parameter to force overwrite of previously created assembly.

Now you almost done except of following caveats:
1) If you have types with the same name in different namespaces you would get error saying something like:
Error: There was an error reflecting type ‘FooNamespace.Foo’ – Types ‘FooNamespace1.Foo’ and ‘FooNamespace2.Foo’ both use the XML type name. Use XML attributes to specify a unique XML name and/or namespace for the type.
To resolve it – differentiate your types by specifying XML namespace:
   1:  using System.Xml.Serialization;


   2:   


   3:  [XmlType(Namespace = "urn:Foo1")]


   4:  [XmlRoot(Namespace = "urn:Foo1")]


   5:  public class Foo{}





2)  If you have properties with readonly or internal properties, or internal types – you would get the following error:


Error … property or indexer cannot be assigned to it is read only.

You may either mark such property with [XmlIgnore] attribute or (for internal) – add following line to AssemblyInfo.cs file:





[assembly: InternalsVisibleTo("<AssemblyName>.XmlSerializers, PublicKeyToken=null")]






You may still add it as part of build process manually modifying MSBUILD script in .csproj file.


Look here or here for details.




Technorati Tags: ,,


Wednesday, June 17, 2009

Barrier multithreading primitive in C#






public class Barrier
{
public Barrier(int count)
{
Count = count;
}

public int Count{get; set;}

public void Wait()
{
lock(this)
{
if (--Count > 0)
{
System.Threading.Monitor.Wait(this);
}
else
{
System.Threading.Monitor.PulseAll(this);
}
}
}
}


Monday, June 01, 2009

Comprehensive list of Debugger Visualizers for Visual Studio




Here is a comprehensive list of Debugger Visualizers for Visual Studio.
It's based on (sorted and merged) info from here, here and here and my own additions.

Here you go:

ASP, WEB:
ASP.NET control graph visualizer - http://odetocode.com/Blogs/scott/archive/2006/01/12/2726.aspx
ASP.NET Cache Visualizer - http://blog.bretts.net/PermaLink,guid,07cd8437-862e-45c6-b24e-3a286fce1b66.aspx
ASP.NET Control Visualizer - http://blog.bretts.net/PermaLink,guid,87d735a0-1592-4711-860f-8a1d29c9630f.aspx
ASP.NET Visualizers - http://weblogs.asp.net/scottgu/archive/2006/01/12/435236.aspx
WebVisualizers - http://webvisualizers.codeplex.com/


Graphics & UI:
Bitmap Debugger Visualizer - http://blogs.geekdojo.net/brian/archive/2006/08/06/bitmapvisualizer.aspx
Graphics Debugger Visualizer - http://www.codeproject.com/KB/macros/GraphicDebuggerVisualizer.aspx
ControlTree visualizer - http://odetocode.com/Blogs/scott/archive/2006/01/12/2726.aspx


DataSource & Data:
Data Debugger Visualizer - http://www.codeproject.com/KB/grid/datadebuggervisualizer.aspx
Debugger Visualizer for Visual Studio - http://coding-time.blogspot.com/2009/03/debugger-visualizer-for-visual-studio.html
Mole Visualizer For All Project Types - http://www.codeproject.com/KB/macros/MoleForVisualStudio.aspx
http://www.moleproject.com/
http://joshsmithonwpf.wordpress.com/mole/
Righthand Dataset Debugger Visualizer - http://cs.rthand.com/blogs/blog_with_righthand/pages/Righthand-Dataset-Debugger-Visualizer.aspx
XML Visualizer - http://blogs.conchango.com/howardvanrooijen/archive/2005/11/24/2424.aspx
IEnumerable Visualizer -
http://rapiddevbookcode.codeplex.com/wikipage?title=EnumerableDebugVisualizer

LINQ, DB:
DB Connection Visualizer - http://dbvisualizer.codeplex.com/
LINQ Expressions DebuggerVisualizer - http://www.manuelabadia.com/blog/PermaLink,guid,9160035f-490f-46bd-ab55-516b5c7545af.aspx
LINQ Expression Tree Visualizer- Look in VS 2008 Samples
LINQ to SQL Debug Visualizer - http://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspx
LINQ Query Visualizer - http://msdn.microsoft.com/en-us/library/bb629285.aspx
LINQ to SQL Visualizer - http://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspx
LINQ to Entity Visualizer - http://visualstudiogallery.msdn.microsoft.com/en-us/99468ece-689b-481c-868c-19e00e0a4e69


System & miscellanea:
CAB Visualization - http://www.codeplex.com/WorkItemVisualizer
GUID Debugger Visualizer - http://devmatter.blogspot.com/2006/04/guid-debugger-visualizer-for-visual.html
IL Visualizer - http://blogs.msdn.com/haibo_luo/archive/2006/11/16/take-two-il-visualizer.aspx
Improving Visual C++ Debugging - http://www.codeguru.com/columns/kate/article.php/c15291
PowerShell Debug Visualizer - http://poshdebugvisualizer.codeplex.com/
Regular Expression Visualizers - http://weblogs.asp.net/rosherove/archive/2005/11/26/AnnoucingRegexKit10.aspx
Sharepoint debug visualizer - http://spdv.codeplex.com/
WindowsIdentity Debugger Visualizer - http://geekswithblogs.net/khanna/archive/2006/01/05/64903.aspx


WCF:
WCF Visualizers Tool - http://wcfvisualizer.codeplex.com/


WPF:
WPF Tree Debugger Visualizer - http://wpftreevisualizer.codeplex.com/
DepO WPF Dependency Object Visualizer - http://www.codeplex.com/dathanliblikdepo
WPF Debugger Visualizer - http://blogs.oosterkamp.nl/blogs/thomas/archive/2009/03/11/wpf-debugger-visualizer.aspx
XAML Debugger Visualizer for WPF - http://www.codeproject.com/KB/WPF/XamlVisualizer.aspx
Snoop - a WPF Utility - http://blois.us/Snoop/


XML:
Xml Visualizer v.2 - http://www.codeplex.com/XmlVisualizer
Lithium XML Debugger Visualizer - http://blogs.conchango.com/howardvanrooijen/archive/2005/04/11/1267.aspx


Monday, July 30, 2007

Free e-book: Rapid C# Windows Development


"Rapid C# for Windows Development" by Joseph Chancellor is available (in .pdf format) for free download from LLBLGen Pro site.

Monday, July 16, 2007

Benchmarking LINQ vs. DAAB vs. ADO.NET



Abstract: This article presents benchmarking results of performance of SP calls using LINQ, Data-access application block and ADO.NET.

Some time ago I was doing LINQ performance benchmarking, looking for an option of replacing our existing data layer with it. One thing that was especially important for me to check - was performance of executing SP's. The reason is that most of our DAL logic resides in SP's at one hand and that performance-critical parts are implemented as SP's as well (for all the good reasons, like possibility to benefit from queries compilation etc).

Below are performance benchmarking results of execution uspGetManagerEmployees SP from AdventureWorks DB. (Basically it does some sort of JOIN to retrieve records of employees data for the specified Manager ID).
I tested three data-access models - raw ADO.NET vs. DAAB (our current DAL workhorse) vs. LINQ (May 2006 preview version).

As a prerequisite for running this test you'll need AdventureWorks DB and installation of LINQ.
Few side-notes:
  • I've left opening of DB connection out of the context of benchmarked section. Only SP execution is measured.

  • I use ExecuteReader rather than ExecuteDataSet, as it is much more lightweight. For LINQ benchmark I using GetEnumerator().

So here goes the test code:

1. Plain ADO.NET:


public static void SqlSPBench()
{
string connectionString =
"Data Source=localhost;Initial Catalog=AdventureWorks;Integrated Security=True";

using( SqlConnection conn = new SqlConnection( connectionString ) )
{
conn.Open();

Stopwatch watch = new Stopwatch();
watch.Start();

int count = 10000;
for( int i = 0; i < count; i++ )
{
using( SqlCommand cmd = new SqlCommand() )
{
cmd.Connection = conn;
cmd.CommandText = "uspGetManagerEmployees";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add( new SqlParameter( "ManagerID", 3 ) );

using( SqlDataReader reader = cmd.ExecuteReader( CommandBehavior.Default ) )
{
//while (reader.Read())
//{
// Console.WriteLine(reader[3]);
//}
}
}
}
watch.Stop();
Console.WriteLine(
"Running " + count +
" SP's using SQL ExecuteReader took " + watch.ElapsedMilliseconds + " ms");
Console.ReadLine();
}
}


2. DAAB (Data Access Application Block):


public static void SqlSpDAAB()
{
Database adventureWorksDataBase = DatabaseFactory.CreateDatabase( "AdventureWorks" );

Stopwatch watch = new Stopwatch();
watch.Start();

int count = 10000;
for( int i =0; i<count; i++ )
{
using( DBCommandWrapper sp_uspGetManagerEmployees =
adventureWorksDataBase.GetStoredProcCommandWrapper(
"uspGetManagerEmployees" ) )
{
sp_uspGetManagerEmployees.AddInParameter( "@ManagerID", DbType.Int32, 3 );
using( IDataReader dataReader =
adventureWorksDataBase.ExecuteReader( sp_uspGetManagerEmployees ) )
{
//DataSet results =
// adventureWorksDataBase.ExecuteDataSet( sp_uspGetManagerEmployees );
}
}
}
watch.Stop();
Console.WriteLine(
"Running " + count + " SP's using DAAB took " +
watch.ElapsedMilliseconds + " ms");
Console.ReadLine();
}


3. LINQ (using ORM layer produced by SQLMetal.exe - "c:\Program Files\LINQ Preview\Bin\SqlMetal.exe" /server:. /database:AdventureWorks /pluralize /sprocs /code:AdventureWorks.cs):



public static void LinqSPBench()
{
string connectionString =
"Data Source=localhost;Initial Catalog=AdventureWorks;Integrated Security=True";
AdventureWorks adventureWorks = new AdventureWorks( connectionString );

Stopwatch watch = new Stopwatch();
watch.Start();

int count = 10000;
for( int i =0; i<count; i++ )
{
StoredProcedureResult<UspGetManagerEmployeesResult> result =
adventureWorks.UspGetManagerEmployees( 3 );
IEnumerator<UspGetManagerEmployeesResult> enumerator = result.GetEnumerator();
//while( enumerator.MoveNext() )
//{
// Console.WriteLine( enumerator.Current.LastName );
//}
}
watch.Stop();
Console.WriteLine(
"Running " + count +
" SP's using LINQ took " + watch.ElapsedMilliseconds + " ms");
Console.ReadLine();
}


Here are timing results of this benchmarking test:
  • Running 10000 SP's using SQL ExecuteReader took 42806 ms

  • Running 10000 SP's using DAAB took 46443 ms

  • Running 10000 SP's using LINQ took 55136 ms




Results
So, comparing to the raw ADO.NET - DAAB is 8% slower and LINQ is 28% slower.
Comparing to DAAB - LINQ is 18% slower.
CPU usage intensity is about 2% average for raw ADO.NET vs. about 8% for DAAB vs. about 20% for LINQ - order of magnitude worse than raw ADO.NET (CPU graphs captured with perfmon below).


Fig. 1 ADO.NET - CPU usage


Fig. 2 DAAB - CPU usage



Fig. 3 LINQ - CPU usage

In overall - this performance overhead is quite disappointing. I still consider to use LINQ, as we have DAL/BLL servers in our architecture, which we may merely upgrade to faster hardware, while isolating thin-client computers from this performance overhead. I think it's a big challenge for the LINQ team to address its performance.



Wednesday, April 11, 2007

Generics collection serializer


Here is small utility class to serialize/deserialize generics collection... My usage of it is passing of multiple parameters to a web service at once.



using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Xml.Serialization;
public class CollectionSerializer<TKey, TValue>
{
public static void Serialize(TextWriter writer, IDictionary<TKey, TValue> dictionary)
{
List<ListEntry<TKey, TValue>> entries = new List<ListEntry<TKey, TValue>>(dictionary.Count);
foreach (TKey key in dictionary.Keys)
{
entries.Add(new ListEntry<TKey, TValue>(key, dictionary[key]));
}

XmlSerializer serializer = new XmlSerializer(typeof(List<ListEntry<TKey, TValue>>));
serializer.Serialize(writer, entries);
}

public static void Deserialize(TextReader reader, IDictionary dictionary)
{
dictionary.Clear();
XmlSerializer serializer = new XmlSerializer(typeof(List<ListEntry<TKey, TValue>>));
List<ListEntry<TKey, TValue>> list = (List<ListEntry<TKey, TValue>>)serializer.Deserialize(reader);

foreach( ListEntry<TKey, TValue> entry in list )
{
dictionary[entry.Key] = entry.Value;
}
}

public class ListEntry<TKeyEntry, TValueEntry>
{
public ListEntry()
{}

public ListEntry(TKeyEntry key, TValueEntry value)
{
Key = key;
Value = value;
}

public TKeyEntry Key;
public TValueEntry Value;
}
}
Please note that standard Tuple type can't be used instead of custom ListEntry as it can't be serialized.