Saturday, February 27, 2010

How To Tune Performance of OpenDS

OpenDS logo
Set the following JVM_ARGS in the file $OPENDS_INSTALL_DIR/config/java.properties for the start-ds command.

-Xms1536m -Xmx1536m -XX:NewSize=768m -XX:MaxNewSize=768m -XX:SurvivorRatio=5 -XX:TargetSurvivorRatio=90 -XX:MaxTenuringThreshold=12 -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode -XX:+CMSIncrementalPacing -XX:+CMSParallelRemarkEnabled -XX:+UseParNewGC -XX:MaxPermSize=128m -XX:+UseTLAB -XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled -XX:+DisableExplicitGC -XX:+HeapDumpOnOutOfMemoryError

Also to prevent the OPENDS HIGH CPU USAGE problem you must enable the beackendDB (berkley JE Database) cache by using the following command.Note that this command can be used while system is running up and have load.Tested enough :)

[root@ochemw bin]# ./dsconfig -h localhost --port 4444 -D "cn=Directory Manager" -w password-X -n set-backend-prop --backend-name userRoot --set db-cache-percent:50
For the details and cache percentage calculations you can refer to the opends perf. tuning page.

Friday, February 26, 2010

A few nice links

http://www.tutorialspoint.com/java/
http://www.exampledepot.com/

Dating with Date and Flirting with Calendar

import java.util.Properties;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Set;
import java.util.*;
import java.util.Collections;
import java.text.SimpleDateFormat;
import java.text.ParseException;

public class DatingWithDate {

private static final SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
protected final static long MILLSECS_PER_DAY = 24L * 60L * 60L * 1000L;

public static void main (String[] args) throws IOException, ParseException {
Calendar now = Calendar.getInstance();
printCalendar (now);
now.add(Calendar.DAY_OF_YEAR,1);
printCalendar (now);
System.exit(0);

}
private static void printCalendar(Calendar calendar)
{
String date = df.format(calendar.getTime());
System.out.println(date);
}

}

Tuesday, February 23, 2010

Command line Reader Sample

import java.io.*;
import java.lang.*;

public class DecimalToOctal {
public static void main(String[] args) throws IOException{
BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the decimal number:");
String deci = buff.readLine();
int value = Integer.parseInt(deci);
String str = Integer.toString(value,8);
System.out.println("octal:=" + str);
}
}

Monday, February 22, 2010

Blocking Queue Sample

import java.util.*;
import java.util.concurrent.*;
import java.io.*;


public class BlockingQueue {
    private static List waitingCustomerList = null;
    private static final String filePath = "/home/serkans/phoneNumbersOnly.txt";
    private static ArrayBlockingQueue waitingCustomersQueue = null;


    public static void main (String[] args)  {


        loadNumbers();


        initializeBlockingQueue();


        Runnable callcenterGirl = new Runnable (){
            public void run () {
                while (true) {


                 String phoneNumber = null;
                 try {
                     phoneNumber = waitingCustomersQueue.poll (10000L, TimeUnit.MILLISECONDS);
                     if (null != phoneNumber){
                         System.out.println (" -- Callcenter girl is helping to : " + phoneNumber);
                     }else{
                         System.out.println ( " -- Callcenter girl is waiting  : " + phoneNumber);
                     }
                 } catch (InterruptedException ex) {
                     System.out.println ("  -- Callcenter girl is interrupted ex:");
                     ex.printStackTrace();
                 }
             }


         }
        };


        Thread t = new Thread (callcenterGirl);
        t.start();




    }


    private static ArrayBlockingQueue initializeBlockingQueue () {
        try {
            waitingCustomersQueue = new ArrayBlockingQueue(waitingCustomerList.size (), true, waitingCustomerList);
        } catch (Exception ex) {
            ex.printStackTrace();
            System.exit(1);
        }
        return waitingCustomersQueue;
    }


    private static void loadNumbers () {
        Properties props = new Properties();
        try {
            props.load (new FileInputStream (new File (filePath)));
            System.out.println (" -- item count in file is :" + props.size());
            waitingCustomerList = new ArrayList (props.keySet());
        } catch (IOException ex) {
            System.out.println (" -- Unable to find file : "+filePath);
        }
    }
}

Wednesday, February 17, 2010

Are you sickened with QUARTZ database ??

import java.io.*;
import java.util.*;

public class WorkHourChecker {

private final static long MILLSECS_PER_HOUR = 1L * 60L * 60L * 1000L;

public static void main (String[] args) throws IOException, InterruptedException {
String workHours = "2-5,19-23,14-15";
checkTimeAndWork (workHours);
System.exit(0);
}

private static void checkTimeAndWork (String workHours) throws InterruptedException,
NumberFormatException {
Date now = new Date();
boolean isWorkhour = false;
StringTokenizer hoursTokenizer = new StringTokenizer(workHours,",");

while (hoursTokenizer.hasMoreTokens()) {
String duration = (String) hoursTokenizer.nextElement();
StringTokenizer durationTokenizer = new StringTokenizer(duration,"-");
int startHour = -1;
int endHour = -1;
while (durationTokenizer.hasMoreElements()) {
startHour = Integer.parseInt((String)durationTokenizer.nextElement()) ;
endHour = Integer.parseInt((String)durationTokenizer.nextElement()) ;
}
if (now.getHours() >= startHour && now.getHours() <= endHour){
isWorkhour = true;
break;
}else{
isWorkhour = false;
}

}

if (isWorkhour){
System.out.println ("-- In the work hours ..");
doSomeBusiness ();
System.out.println ("-- I am very tired i will get some rest!!");
Thread.currentThread().sleep (MILLSECS_PER_HOUR);
checkTimeAndWork (workHours);
}else{
System.out.println(" -- Not in the work hours yet, sleep 1 hour");
Thread.currentThread().sleep (MILLSECS_PER_HOUR);
checkTimeAndWork (workHours);
}
}

private static void doSomeBusiness (){
System.out.println (" -- I have started to work..");
}

}

Tuesday, February 16, 2010

Expose your Java classes as a MBean

link : Mbean sample

Getting SQL Statistics between given dates

ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY HH24:MI:SS';

EXECUTE DBMS_LOGMNR.START_LOGMNR( STARTTIME => '16-FEB-2010 14:30:00', ENDTIME => '16-FEB-2010 15:00:00',options=> DBMS_LOGMNR.DICT_FROM_ONLINE_CATALOG +DBMS_LOGMNR.CONTINUOUS_MINE);

set pause on;

set linesize 2000;

SELECT TIMESTAMP,SQL_REDO,SESSION# FROM V$LOGMNR_CONTENTS WHERE seg_owner='CSVM' and TABLE_NAME = 'UMMESSAGE' and sql_redo like '%1647293%'

Monday, February 15, 2010

Executing linux commands from Java

package sunels;

import java.io.*;

public class LinuxCommandExecutor {
public static void main (String args[]) {
try {
ProcessBuilder pb = new ProcessBuilder ("/bin/grep", "phoneNumber", "/space/subscribersDBdump.txt");

Process proc = pb.start ();

BufferedInputStream in = new BufferedInputStream (proc.getInputStream ());
FileOutputStream fout = new FileOutputStream ("/space/phoneNumbersOnly.txt");

byte[] buffer = new byte[2048];

while (true) {
int nRead = in.read (buffer);

if (nRead == -1)
break;

fout.write (buffer, 0, nRead);
System.out.print (".");
}

fout.close ();
proc.waitFor ();

}
catch (Exception e) {
e.printStackTrace ();
}
}
}

Sample Jmeter Java Test

package sunels;

import java.io.Serializable;
import org.apache.jmeter.protocol.java.sampler.*;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import java.util.List;
import java.util.Properties;
import java.io.IOException;
import java.util.ArrayList;
import java.io.FileInputStream;
import com.mycompany.myproduct.um.msgstore.MessagestoreFactory;
import com.mycompany.myproduct.um.profile.ProfileFactory;
import java.util.Date;
import com.mycompany.myproduct.um.common.Language;
import com.mycompany.myproduct.um.profile.Subscriber;
import java.util.Random;

public class JmeterTest implements JavaSamplerClient , Serializable {

private static List msisdns = new ArrayList();
private static int totalRecordCount = 0;
private static java.util.Date startDate = new java.util.Date();
private static final String umModuleName = "UM";
private ProfileFactory profilefactory = ProfileFactory.getInstance (umModuleName);
private MessagestoreFactory msgstoreFactory = MessagestoreFactory.getInstance (umModuleName);
private Random randomGenerator = new Random();
private static int testType = 1;

static {
try {
Properties props = new Properties();
props.load (new FileInputStream ("/tmp/phoneNumbers.txt"));
msisdns = new ArrayList (props.keySet());
totalRecordCount = msisdns.size();
System.out.println (" -- Msisdns are loaded ....");
}
catch (IOException e) {
e.printStackTrace ();
System.exit (1);
}
}

public void setupTest(JavaSamplerContext context) {
}

public Arguments getDefaultParameters() {
Arguments params = new Arguments();
return params;
}

public synchronized String getMsisdn (){
String subscriberMsisdn = (String) msisdns.get(randomGenerator.nextInt(totalRecordCount));
return subscriberMsisdn;
}

public Subscriber playWithSubscriber (String subscriberPhoneNumber) throws Exception {
Subscriber subscriber = null;
subscriber = (Subscriber) profilefactory.querySubscriberByPhoneNumber (profilefactory.getCommonFactory ().createPhoneNumber (subscriberPhoneNumber));

if (subscriber != null) {
if(testType != 1){
Language subscriberLanguage = subscriber.getLanguage ();
if (subscriberLanguage == Language.ENGLISH) {
subscriber.setLanguage (Language.UKRAINIAN);
}
else {
subscriber.setLanguage (Language.ENGLISH);
}
Date currDate = new java.util.Date ();
subscriber.getSubscription ().setLastLoginDate (currDate);
subscriber.getSubscription ().setLastMessageActivityDate (currDate);
subscriber.save ();
}
}
else {
System.out.println (" -- Unable to find subscriber with msisdn :" + subscriberPhoneNumber);

}
return subscriber;
}

public SampleResult runTest(JavaSamplerContext context) {
SampleResult results = new SampleResult();
results.sampleStart();
String msisdn = getMsisdn();
try{

playWithSubscriber (msisdn);
results.setSuccessful(true);
results.setResponseCodeOK();
results.setResponseMessage("Subscriber found:" + msisdn);
} catch (Exception ex){
ex.printStackTrace();
results.setSuccessful(true);
results.setResponseCode("Exception*");
results.setResponseMessage("Subscriber not found:" + msisdn);
}
results.sampleEnd();
return results;
}

public void teardownTest(JavaSamplerContext context) {
}
}

Locate any java class file in your filesystem in runtime

package sunels;

import java.security.*;
import java.net.URL;

public class ClasspathUtil {
public static void main (String[] args) {
ClasspathUtil classpathutil = new ClasspathUtil ();
ProtectionDomain pDomain = classpathutil.getClass().getProtectionDomain();
CodeSource cSource = pDomain.getCodeSource();
URL loc = cSource.getLocation();
System.out.println (" -- Class< " + classpathutil.getClass().getCanonicalName() + "> Found in : {" + classpathutil.getClass().getCanonicalName() + " :" + loc+"}");
System.exit (0);
}
}
note: This example is finding itself

Paging on Records with JPA

JPA Paging

int maxRecords = 250;
int startPosition = 0;
String queryString = "SELECT u FROM test.VmUsers u where u.userCli LIKE :pattern ";
while (true){

try {
Query selectQuery = subscriberEntityManager.createQuery (queryString);
selectQuery.setParameter ("pattern", "%"+pattern);

selectQuery.setFirstResult(startPosition);
selectQuery.setMaxResults (maxRecords);
List users = selectQuery.getResultList ();
if (users.isEmpty ()) {
break;
}
}catch (Exception ex1) {
ex1.printStackTrace();
}
}

Executing some jobs in parallel

import java.util.*;
import java.io.*;

public class Test {

public static void main (String[] args) {
Runnable r = new Runnable () {

public void run () {
while (true) {
try {
System.out.println ("-- this lines are being printed in another thread.."+Thread.currentThread().getName());
Thread.currentThread().sleep(130);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
};

new Thread (r).start();
while (true) {
System.out.println (" -- this lines are bing printed in main thread.."+Thread.currentThread().getName());
try {
Thread.currentThread ().sleep (60);
} catch (InterruptedException ex) {
//noop
}
}
}
}

-- this lines are being printed in another thread..Thread-0
-- this lines are bing printed in main thread..main
-- this lines are bing printed in main thread..main
-- this lines are bing printed in main thread..main
-- this lines are being printed in another thread..Thread-0
-- this lines are bing printed in main thread..main
-- this lines are bing printed in main thread..main
-- this lines are being printed in another thread..Thread-0
-- this lines are bing printed in main thread..main
-- this lines are bing printed in main thread..main
-- this lines are being printed in another thread..Thread-0
-- this lines are bing printed in main thread..main
-- this lines are bing printed in main thread..main
-- this lines are being printed in another thread..Thread-0

Thursday, February 11, 2010

Text Replacement in VI editor

3801234561
3801234562
3801234563
3801234564
3801234565
3801234566
3801234567
3801234568
:g/380/s//994

Wednesday, February 10, 2010

JMeter Command Line Interface and Usage

Editing the default in the bin/JMeter.properties file

The following properties affect the summariser:

# Define the following property to automatically start a summariser
# with that name(applies to non-GUI mode ony)
summariser.name=summary
#
# interval between summaries (in seconds) default 3 minutes
summariser.interval=180
#
# Write messages to log file
summariser.log=true
#
# Write messages to System.out
summariser.out=true


[root@OCMPRBT2 bin]# ./jmeter -n -t /space/ukrain/voicemail/JmeterTest/myTest.jmx -l logOut.jtl &

summary + 108 in 21.8s = 5.0/s Avg: 1291 Min: 229 Max: 6317 Err: 0 (0.00%)

summary = 206 in 52.5s = 3.9/s Avg: 1827 Min: 229 Max: 6974 Err: 0 (0.00%)



The lines with "summary +" are incremental for the latest summariser period, the lines with "summary =" are cumulative. The above was with a summariser period of 20 secs, you can see the actual periods can sometimes be longer than the specified period and the length of the very first period is somewhat random. You get the throughput statistics as well as average, min and max response times, and how many errors were detected (assuming your JMeter test plan as assertions to detect errors).

Tuesday, February 9, 2010

Built In Java Tools & JProfiler

List Java Processes
[serkans@serkanslnx smartconnect] jps -l
16563 sun.tools.jps.Jps
8765 ReporterGeneratePdf
Getting Thread Dump of a process with the given id

[root@serkanslnx telco]# jstack 1009
Attaching to process ID 1009, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 1.5.0_10-b03

Thread 1079: (state = IN_NATIVE)
- java.lang.UNIXProcess.waitForProcessExit(int) @bci=0 (Interpreted frame)
- java.lang.UNIXProcess.access$900(java.lang.UNIXProcess, int) @bci=2, line=20 (Interpreted frame)
- java.lang.UNIXProcess$1$1.run() @bci=165, line=132 (Interpreted frame)

JVM Arguments for Profiling and Debugging
-Xdebug -Xrunjdwp:transport=dt_socket,address=1980,server=y,suspend=n
OR
-Xdebug -Xrunjdwp:transport=dt_socket,address=1980,server=y,suspend=y

JVM_ARGS and LD_LIBRARY_PATH for JProfiler Debugging
-Xint -Xrunjprofiler:port=8849 -Xbootclasspath/a:/space/tools/jprofiler3/bin/agent.jar
and also
export LD_LIBRARY_PATH=/opt/jprofiler3/bin/linux-x86:$LD_LIBRARY_PATH

Monday, February 1, 2010

Adding Jars to your CLASSPATH in linux

export CLASSPATH=/space/mol.jar:$CLASSPATH
for j in $JMETER_HOME/lib/*.jar; do
CLASSPATH=$j:$CLASSPATH
echo $j
done


Note: This script initially adds the /space/mol.jar to the classpath.After than it adds all jars under the $JMETER_HOME/lib directory to the classpath.

Reverse read from file in UNIX

Linux TAC Command
[root@x1 bin]# cat numbersInOrder.txt
1
2
3
4
5
6
7
8
9
10
[root@x1 bin]# tac numbersInOrders.txt
10
9
8
7
6
5
4
3
2
1
[root@x1 bin]#