Wednesday, April 19, 2017

Git for TFS users - a cheat sheet

It was tough.

When I started to use Git, because now nearby everybody is using Git, it was tough. Really tough.
Imagine transition from Visual Studio 2015 & TFS to Eclipse, Jira, Jenkins, Gerrit and Git (command line).

What I saw was that there are developers which have no other experience than Git. And Gerrit, And Jira. And Jenkins.
So those were satisfied.
I was not.

But another project arrived, with Jira, Jenkins, GitLab, Artifactory and ... with msbuild and Visual Studio 2015, and I was not lost :-)
So today I can say it is good to have experience with multiple development environments.

But to simplify start for TFS/Visual Studio developers, which are trying to use Git, I prepared a presentation and this "cheat sheet" which is improved version of this one.


TFS Version Control
Git
Workspace
Repository (aka. “Repo”)
Get Latest  (First time)
Clone
Get Latest (After first time)
Pull
Check in
Commit + Push
Check out
There is no lock ! no checkout !
Branch
Branch
Merge
Merge
Shelve
Stash - just local ! (on local machine ...)
Label
Tag
Included changes
Staged
Excluded changes
not staged

Also you can check may previous post about three common "scenarios" which are used in Git environments for "check-out - check-in"

Wednesday, August 17, 2016

Git: How to commit and merge

Hi,
I'm using Git just one year, and in second team, so here are three "algorithms" or "patterns" which I recognized yet:

1. Git+Gerrit+Jira, branch, commit --amend, upload

  • create branch per Jira item  
  • develop code
  • try to pull
  • solve merge conflicts
  • commit
  • upload (push + upload to gerrit )
  • ... wait for gerrit review ...
  • change code (regarding gerrit review)
  • try to pull
  • solve merge commits
  • commit --amend (rewrite your commit - be careful )
  • upload
  • ...
This is probably good solution for big projects with long term gerrit review process ...

2. Git+Gitlab, master, stash, merge, push

Probable suitable just for not so big project (lines/changes/developers)
Just one branch used locally.
  • You will change code.
  • When there is something new on origin/master you will try to pull
  • If it is impossible to pull, you will stash changes
    • pull
    • stash apply - solve merge conflicts 
  • continue if needed
  • push ( no review before push)

3. Git+Gitlab, master, commit, merge, push

The same as above but stash is not used, just commit and push.
But then merge commits will appears.

Probable suitable just for not so big project (lines/changes/developers)
Just one branch used locally.
  • You will change code.
  • When there is something new on origin/master you will try to pull
  • If is impossible to pull, you will commit changes
    • pull
    • solve merge conflicts 
  • continue if needed
  • push ( no review before push)

Any other ideas ?

Saturday, June 18, 2016

Java 8. What is cost of lambdas ?

I like the lambdas. From c# of course, but now is possible to use lambdas also in Java 8.
But what is performance ?
  1. Compare short list of strings with comparator defined as class
  2. Compare short list of strings with comparator defined as anonymous class
  3. Compare short list of strings with comparator defined as lambda
Results:
  1. 47563
  2. 78857
  3. 111320
So lambda comparator is  2.34 time slower than comparator defined by class, and 1.65 times slower than comparator defined by anonymous class.

And here is code:


package com.company;

import java.util.*;

public class Main {

    public static void main(String[] args) {

        CompareByClass();

        CompareByLambda();

        CompareByAnonymous();
    }
    private static void CompareByClass() {
        List<String> names = Arrays.asList("peter", "anna", "mike", "xenia");
        Date timeStart = new Date();
        for(int i=0; i<Integer.MAX_VALUE;i++) {
            Collections.sort(names, new myComparator());
        }
        Date timeStop = new Date();
        System.out.println(timeStop.getTime() - timeStart.getTime());
    }

    private static void CompareByAnonymous() {
        List<String> names = Arrays.asList("peter", "anna", "mike", "xenia");
        Date timeStart = new Date();
        for(int i=0; i<Integer.MAX_VALUE;i++) {
            Collections.sort(names, new Comparator<String>() {
                @Override
                public int compare(String a, String b) {
                    return b.compareTo(a);
                }
            });
        }
        Date timeStop = new Date();
        System.out.println(timeStop.getTime() - timeStart.getTime());
    }

    private static void CompareByLambda() {
        List<String> names = Arrays.asList("peter", "anna", "mike", "xenia");
        Date timeStart = new Date();
        for(int i=0; i<Integer.MAX_VALUE;i++) {
            Collections.sort(names, (String a, String b) -> b.compareTo(a));
        }
        Date timeStop = new Date();
        System.out.println(timeStop.getTime() - timeStart.getTime());
    }
}

package com.company;

import java.util.Comparator;

public class myComparator implements Comparator<String>
{
    public int compare(String a, String b) {
        return b.compareTo(a);
    }
}

Monday, June 13, 2016

Java: What is cost of casting and reflection ?

Simple question: What is cost of casting or using dynamic ?

.. but that was c# . What about Java ? No dynamics so we should use reflection.

Answers:
  1. Simple Counter
    • Counter as long (miliseconds):
      • 264
    • Counter as object, and cast to long: 
      • 13484
    •  Difference:
      •  casting from object to long is 51 times slower .
  2. Simple class with int property
    • No casting
      • 256
    • casting from object
      • 252
    • used as reflection
      • 29360114
    • Difference
      • casting from object to class is just .. 1.0158 times ... faster ?
      • but reflection ... is 114687.94 times slower !
Code:


package castingandgenerics;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

public class program {
 
 public static void main(String [ ] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException
 {
  objectAndLongComparison();
  objectAndClassComparison();
  
 }
 
    private static void objectAndLongComparison()
    {
        long longCounter = 0;
        Object objectCounter = longCounter;
        Date timeLongStart, timeLongStop, timeObjectStart, timeObjectStop;
        System.out.println("Object and Long comparison:");
        timeLongStart = new Date();
        for (int i = 0; i < Integer.MAX_VALUE; i++)
        {
            longCounter = longCounter + 1;
        }
        timeLongStop = new Date();
        System.out.println(timeLongStop.getTime() - timeLongStart.getTime());

        timeObjectStart = new Date();
        for (int i = 0; i < Integer.MAX_VALUE; i++)
        {
            objectCounter = ((long)objectCounter) + 1;
        }
        timeObjectStop = new Date();
        
        System.out.println(timeObjectStop.getTime() - timeObjectStart.getTime());
    }
    
    private static void objectAndClassComparison() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException
    {

        Date[] timeStart = new Date[3];
        Date[] timeStop = new Date[3];
        Class1 myClass = new Class1();
        Object myObject = myClass;
      //  dynamic myDynamic = myClass;

        System.out.println("Object and Class comparison:");
        timeStart[0] = new Date();

        for (int i = 0; i < Integer.MAX_VALUE; i++)
        {
            myClass.setMyProperty1(i);
            //myClass.MyProperty2 = string.Empty;
        }
        timeStop[0] = new Date();
        System.out.println(timeStop[0].getTime() - timeStart[0].getTime());

        timeStart[1] = new Date();
        for (int i = 0; i < Integer.MAX_VALUE; i++)
        {
            ((Class1)myObject).setMyProperty1(i);

        }
        timeStop[1] = new Date();
        System.out.println(timeStop[1].getTime() - timeStart[1].getTime());

        timeStart[2] = new Date();
        Class[] cArg = new Class[1];
        cArg[0] = int.class;
        for (int i = 0; i < 1024 ; i++)
        {
         Method method = myObject.getClass().getMethod("setMyProperty1",cArg[0]);
            method.invoke(myObject, i);

        }
        timeStop[2] = new Date();
        System.out.println((timeStop[2].getTime() - timeStart[2].getTime())*(Integer.MAX_VALUE/1024));

    }
}

Code is also here (GitHub).

Sunday, June 12, 2016

What is cost of casting or dynamic ?

Simple question: What is cost of casting or using dynamic ?

Answers:

  1. Simple Counter
    • Counter as long :
      • 00:00:02.6129381
    • Counter as object, and cast to long: 
      • 00:00:30.0840943
    •  Difference:
      •  casting from object to long is 11.51 times slower .
  2. Simple class with int property
    • No casting
      • 00:00:06.3978060
    • casting form object
      • 00:00:07.5841771
    • used as dynamic 
      • 00:01:27.3716838
    • Difference
      • casting from object to class is just 1.185 times slower ..
      • but dynamic.. is 13.65 times slower !
Code:


print 'hello world!'using System;
using System.Diagnostics;

namespace CsharpBoxingUnboxingAndGenericsExample
{
    class Program
    {
        static void Main(string[] args)
        {
            objectAndLongComparison();
            objectAndClassComparison();
        }

        private static void objectAndLongComparison()
        {
            long longCounter = 0;
            object objectCounter = longCounter;
            DateTime timeLongStart, timeLongStop, timeObjectStart, timeObjectStop;
            Trace.WriteLine("Object and Long comparison:");
            timeLongStart = DateTime.Now;
            for (int i = 0; i < int.MaxValue; i++)
            {
                longCounter = longCounter + 1;
            }
            timeLongStop = DateTime.Now;
            Trace.WriteLine(timeLongStop - timeLongStart);

            timeObjectStart = DateTime.Now;
            for (int i = 0; i < int.MaxValue; i++)
            {
                objectCounter = ((long)objectCounter) + 1;
            }
            timeObjectStop = DateTime.Now;
            
            Trace.WriteLine(timeObjectStop - timeObjectStart);
        }

        private static void objectAndClassComparison()
        {

            var timeStart = new DateTime[3];
            var timeStop = new DateTime[3];
            Class1 myClass = new Class1();
            object myObject = myClass;
            dynamic myDynamic = myClass;

            Trace.WriteLine("Object and Class comparison:");
            timeStart[0] = DateTime.Now;

            for (int i = 0; i < int.MaxValue; i++)
            {
                myClass.MyProperty1 = i;
                //myClass.MyProperty2 = string.Empty;
            }
            timeStop[0] = DateTime.Now;
            Trace.WriteLine(timeStop[0] - timeStart[0]);

            timeStart[1] = DateTime.Now;
            for (int i = 0; i < int.MaxValue; i++)
            {
                ((Class1)myObject).MyProperty1 = i;
                //((Class1)myObject).MyProperty2 = string.Empty;
            }
            timeStop[1] = DateTime.Now;
            Trace.WriteLine(timeStop[1] - timeStart[1]);

            timeStart[2] = DateTime.Now;
            for (int i = 0; i < int.MaxValue; i++)
            {
                myDynamic.MyProperty1 = i;
                //myDynamic.MyProperty2 = string.Empty;
            }
            timeStop[2] = DateTime.Now;
            Trace.WriteLine(timeStop[2] - timeStart[2]);

        }
    }
} 

If you would like to play with code here it is on GitHub.

Saturday, June 4, 2016

c#.Virtual, override, new and hidden example.

Are you not sure what virtual , override, new or hidden means ?
No problem , it is easy. Here is example:

Code:

using System.Diagnostics;

namespace VirtualAndNewExample
{
    class Program
    {
        static void Main(string[] args)
        {
            var baseClass = new BaseClass();
            var derivedVirtual = new DerivedVirtual();
            var derivedNew = new DerivedNew();
            var derivedHidden = new DerivedHidden();

            Trace.WriteLine("Not casted versions");
            baseClass.Name();
            derivedVirtual.Name();
            derivedNew.Name();
            derivedHidden.Name();
            Trace.WriteLine("Casted to base versions");
            baseClass.Name();
            ((BaseClass)derivedVirtual).Name();
            ((BaseClass)derivedNew).Name();
            ((BaseClass)derivedHidden).Name();
        }
    }

    public class BaseClass
    {
        public virtual void Name()
        {
            Trace.WriteLine("Base");
        }
    }

    public class DerivedVirtual : BaseClass
        {
            public override void Name()
            {
                Trace.WriteLine("DerivedVirtual");
            }
        }

    public class DerivedNew : BaseClass
    {
        public new void Name()
        {
            Trace.WriteLine("DerivedNew");
        }
    }

    public class DerivedHidden : BaseClass
    {
        public void Name()
        {
            Trace.WriteLine("DerivedHidden");
        }
    }

}

.. and output (in Output window) :
Not casted versions
Base
DerivedVirtual
DerivedNew
DerivedHidden
Casted to base versions
Base
DerivedVirtual
Base
Base


Now it is clear I hope .
If not you can play with this example also from GitHub.