SringSring

Thursday, 30 October 2014

Top 10 Reasons Your Application Is Running Poorly

“Hey…the Internet is down” or “the application is really slow” are common problems I’ve heard over the years. As the Ned Stark meme goes, how does one simply fix “it’s slow”?

Full disclosure, this list is not scientific, it’s just based upon experience dealing with customers trying to solve application issues.

Configuration – The wrong Thread Pool or Connection Pool Settings.
Not providing enough threads to match large load can cause serious blocking issues and create  bottlenecks.
Memory Management – Improper Memory Configuration for the Code Size and Transaction Volume.
Improper memory settings can cause excessive garbage collection and REMEMBER, in Java,
a “Full Garbage Collection” means ALL THREADS STOP.
Web Services  – SOAP based web services have more overall overhead when compared to REST based web services.
Did you know that a simple SOAP web service call that returns a temperature or a zip code requires 1 or 2KB of packaging data? Like shipping a watch in a big box full of foam peanuts. Unnecessary overhead.
Object Relational Mapping – We are really talking about Java and Hibernate Abuse.
Application Code – Poorly written code. Make ALL of your developers read Code Complete by Steve McConnell
  and Implement Unit Testing. Both can great improve the quality of their code.
Input/Output Bottlenecks – Storage and network bottlenecks –
Ensure your disks are configured for sufficient I/O.
You need to make sure you have sufficient I/O configurations, especially in virtual machine and complex environments.
Logging Accidentally left on DEBUG mode – After your major load test, did you leave your logging solution in DEBUG mode? This can create a huge bottleneck that correlates directly with load. However, this problem is pretty sneaky and you won’t discover this issues unless you run a major load test.
Request Overload – We have too many inbound requests coming in and not enough resources to handle the load —
Simply a bandwidth issue.
Database – It’s probably not the database. Oracle DBA’s typically run a fast databases.
SQL Server implementations are typically less tuned for performance.
Database, cont. – It’s probably the database, bad index or just a bad query.
 Could also be a developer writing a query inside a query. (See Above)

Wednesday, 29 October 2014

Junit Test cases examples

package Com.Srini.Junit;

import static org.junit.Assert.*;

import org.junit.Test;

public class MyUnitTest {
@Test
   public void testConcatenate() {
       MyUnit myUnit = new MyUnit();

       String result = myUnit.concatenate("one", "two");

       assertEquals("onetwo", result);

   }
@Test
   public void testGetTheStringArray() {
       MyUnit myUnit = new MyUnit();

       String[] expectedArray = {"one", "two", "three"};

       String[] resultArray =  myUnit.getTheStringArray();

       assertArrayEquals(expectedArray, resultArray);
   }
@Test
   public void testGetTheBoolean() {
       MyUnit myUnit = new MyUnit();

       assertTrue (myUnit.getTheBoolean());

       assertFalse(myUnit.getTheBoolean());
   }

}
-------------------------------------------
package Com.Srini.Junit;

public class MyUnit {
public String concatenate(String one, String two){
        return one + two;
    }
public String[] getTheStringArray(){
String strArry[]={"one", "two", "three"};
return strArry;
}

public boolean getTheBoolean() {
// TODO Auto-generated method stub
return true;
}
}
----------------------------------
package Com.Srini.Junit;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.junit.Test;

public class MyMatcherTest {
@Test
    public void testWithMatchers() {
        assertThat("this string", is("this string"));
        assertThat(123, is(123));
    }
}

Display OrderByValue in map

package Com.Srini.program;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

public class OrderByValue {
public static void main(String a[]){
       Map<String, Integer> map = new HashMap<String, Integer>();
       map.put("java", 20);
       map.put("C++", 45);
       map.put("Java2Novice", 2);
       map.put("Unix", 67);
       map.put("MAC", 26);
       map.put("Why this kolavari", 93);
       Set<Entry<String, Integer>> set = map.entrySet();
       List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set);
       Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
       {
           public int compare( Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2 )
           {
               return (o2.getValue()).compareTo( o1.getValue() );
           }
       } );
       for(Map.Entry<String, Integer> entry:list){
           System.out.println(entry.getKey()+" ==== "+entry.getValue());
       }
/*Map<String, Integer> map = new HashMap<String, Integer>();
       map.put("java", 20);
       map.put("C++", 45);
       map.put("Java2Novice", 2);
       map.put("Unix", 67);
       map.put("MAC", 26);
       map.put("Why this kolavari", 93);
       Set<Entry<String, Integer>> set = map.entrySet();
     
       Iterator itr = set.iterator();
       while(itr.hasNext())
       {
           System.out.println(itr.next());
       }*/
     
   }
}

StringTokenizer in java

package Com.Srini.program;

import java.util.StringTokenizer;

public class MyStringTokens {
//public class MyStringTokens {
   public static void main(String a[]){
       String msg = "http://10.123.43.67:80/";
       StringTokenizer st = new StringTokenizer(msg,"://.");
       while(st.hasMoreTokens()){
           System.out.println(st.nextToken());
       }
   }
}

Hash Code Implementation in java

package Com.Srini.program;

import java.util.HashMap;

public class MyHashcodeImpl {
public static void main(String a[]){
       
       HashMap<Price, String> hm = new HashMap<Price, String>();
       hm.put(new Price("Banana", 20), "Banana");
       hm.put(new Price("Apple", 40), "Apple");
       hm.put(new Price("Orange", 30), "Orange");
       //creating new object to use as key to get value
       Price key = new Price("Banana", 20);
       System.out.println("Hashcode of the key: "+key.hashCode());
       System.out.println("Value from map: "+hm.get(key));
   }
}

class Price{
   
   private String item;
   private int price;
   
   public Price(String itm, int pr){
       this.item = itm;
       this.price = pr;
   }
   
   public int hashCode(){
       System.out.println("In hashcode");
       int hashcode = 0;
       hashcode = price*20;
       hashcode += item.hashCode();
       return hashcode;
   }
   
   public boolean equals(Object obj){
       System.out.println("In equals");
       if (obj instanceof Price) {
           Price pp = (Price) obj;
           return (pp.item.equals(this.item) && pp.price == this.price);
       } else {
           return false;
       }
   }
   
   public String getItem() {
       return item;
   }
   public void setItem(String item) {
       this.item = item;
   }
   public int getPrice() {
       return price;
   }
   public void setPrice(int price) {
       this.price = price;
   }
   
   public String toString(){
       return "item: "+item+"  price: "+price;
   }
}

Fibonacci series program in java

package Com.Srini.program;

public class MyFibonacci {
 /*public static void main(String a[]){
       
         int febCount = 15;
         int[] feb = new int[febCount];
         feb[0] = 0;
         feb[1] = 1;
         for(int i=2; i < febCount; i++){
             feb[i] = feb[i-1] + feb[i-2];
         }

         for(int i=0; i< febCount; i++){
                 System.out.print(feb[i] + " ");
         }
       
    }*//*public final static void main(String[] args){
     double d = 10.0 / -0;
     if(d == Double.POSITIVE_INFINITY)
     System.out.println("Positive infinity");
     else
       System.out.println("Negative infinity");
    }*/

 public static void main(String[] args){
   int[] a = {1};
   MyFibonacci t = new MyFibonacci();
   t.increment(a);
   System.out.println(a[a.length - 1]);
}
 void increment(int[] i){
    i[i.length - 1]++;
 
 }

}

Deadlock in threads

package Com.Srini.program;

public class MyDeadlock {

String str1 = "Java";
    String str2 = "UNIX";
   
    Thread trd1 = new Thread("My Thread 1"){
        public void run(){
            while(true){
                synchronized(str1){
                    synchronized(str2){
                        System.out.println(str1 + str2);
                    }
                }
            }
        }
    };
   
    Thread trd2 = new Thread("My Thread 2"){
        public void run(){
            while(true){
                synchronized(str2){
                    synchronized(str1){
                        System.out.println(str2 + str1);
                    }
                }
            }
        }
    };
   
    public static void main(String a[]){
        MyDeadlock mdl = new MyDeadlock();
        mdl.trd1.start();
        mdl.trd2.start();
    }
}