Showing posts with label Performance Testing. Show all posts
Showing posts with label Performance Testing. Show all posts

Thursday, 10 October 2019

Setting and getting private variable of a class without properties



Setting or getting private variable is quite a corner scenario while developing a new application and mainly developer will always find a way to access the private variable. But there is a direct way where we can access the private variable using reflection without much of hassle

What is the real-world scenario where I would need to access a private variable?
In real-world scenario I don’t you may ever need to access private variable in your own application. If you do then you’ll simply change it access specifier.

     UTC or TDD development you might find yourself in scenario where you need to access or change the value of private variable of a class. In UTCs we often do such things to by check some negative scenario or for some other reasons.

      Third party dll or library, we may need to access private variable of a third-party variable

These are the cases where I need to access private variable

How could we achieve this?
Quite simple, you just need to do some R&D with reflection classes and need to spend some time with Type class provided in .net framework.

Example-
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CrossProjectDemo
{
    public class Car
    {
        private string name = string.Empty;
        private string prvtVariable = "PrivateVariable";

        public string Name {
            get { return name; }
            set { name = value; }
        }

        public Car()
        {
        
        }
    }
}

As you can see there two private variables
1. name
2.
prvtVariable

While name is being accessed in a public Property Name, prvtVariable will be accessed directly

Let’s see how
First in your application import Reflection namespace
using System.Reflection;

Then follow the code-
Car c = new Car();
Type typ = typeof(Car);
FieldInfo type = typ.GetField("prvtVariable", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var value = type.GetValue(c);



In above image you can see that the type.IsPrivate is true which means variable is private



And we can access its value into the value variable.
That’s easy right.
Now, Let’s move towards how we can change its value. Lets’ make few changes to the existing code
Just add one-line right after Getvalue() method call

var value = type.GetValue(c);
type.SetValue(c, "Not so much");

and now you have changed the value of a private variable



Easy, isn’t it. So here we’re accessing private variables through reflection.
For more detail info like this visit this
Thanks

Saturday, 28 September 2019

Performance comparison of Generic and non Generic List after JIT compilation


Performance of generic and non-generic list is well known. But there is a catch. We should always consider results which comes after JIT compilation. The difference between the test run of the code snippet and running the code after JIT does its job may surprise you.
  1. Now what happens when you run the test application for performance comparison.
  2. When you run the application, JIT runs i.e. Just in Time compiler.
  3. JIT compiles the application, optimize the code and finds the best approach to executes the code.
  4. All these JIT steps are time consuming which are included in first execution, so the execution time is always way more than actual run time
  5. So how you can calculate the actual run time? Simple, to check the performance of your test application with test data, call code snippet in a loop.

Example
private static void GenericListPerformance()
{
     var genList = new List<int>() { 10, 100, 13, 19, 14, 100, 99 };
     var nonGenList = new ArrayList() { 10, 100, 13, 19, 14, 100, 99 };

     var stWatch = Stopwatch.StartNew();
     genList.Sort();
     stWatch.Stop();
     Console.WriteLine("Generic List sorting "+ stWatch.Elapsed.TotalMilliseconds.ToString());

     stWatch = Stopwatch.StartNew();
     nonGenList.Sort();
     stWatch.Stop();
Console.WriteLine("Non Generic List sorting "+ stWatch.Elapsed.TotalMilliseconds.ToString());
}

Here I have created a simple method to compare the two list ArrayList and List<T>. I have used Stopwatch which is present in System.Diagnostics namespace of .net framework and then printed the messages with time.

static void Main(string[] args)
{
         for (int i = 0; i < 5; i++)
            {
                Console.WriteLine(string.Format("<--------Call Number {0} -------->",i));
                GenericListPerformance();
                Console.WriteLine();
            }
}

Now this is where I have used the actual concept. I have called the same method in main inside a loop which runs 5 times. So, when it run for first time JIT can optimize the code and find the best approach to run this code. Later, JIT will use the native code and give us the best results.

Result

See the results.
For first run the execution time is-
For Generic - 0.0263
For Non-Generic – 0.233
And later it came to be line
Generic – 0.0012, 0.0013, 0.0011, 0.0013
Non-Generic – 0.0032, 0.0026, 0.0026, 0.0029



Therefor, we should always consider the JIT time

Subscribe to my blog if you want to get notified about more detailed analysis of topics. Comment if you have any question or want me to do some analysis on the topic you want.
Thanks

Send All Azure App Insights Or Logs Data To Tool Like Datadog

  Introduction Microsoft Azure provides a variety of insights and monitoring logs that you can monitor to check the state of a resource and ...