package org.apache.directory.test.perf;


import java.util.Hashtable;
import java.util.Random;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;


/* 
 */
class SearchTest extends Thread 
{
    private static Random r = new Random( System.currentTimeMillis() );
    static int nbLoop;
    static String ldapUrl;
    static String principal;
    static String password;
    static String dnFormat;

    public void run()
    {
        try
        {
            Hashtable env = new Hashtable( 5, 0.75f );
            
            /*
             * Specify the initial context implementation to use.
             * This could also be set by using the -D option to the java program.
             * For example,
             *   java -Djava.naming.factory.initial=com.sun.jndi.ldap.LdapCtxFactory \
             */
            env.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" );
            /* Specify host and port to use for directory service */
            env.put( Context.PROVIDER_URL, ldapUrl );
            env.put( Context.SECURITY_PRINCIPAL, principal );
            env.put( Context.SECURITY_CREDENTIALS, password );
    
            /* get a handle to an Initial DirContext */
            DirContext ctx = new InitialDirContext( env );
            int result = 0;
    
            long t0 = System.currentTimeMillis();
    
            for ( int i = 0; i < nbLoop; i++ )
            {
                String dn = dnFormat.replaceFirst( "@", Integer.toString( r.nextInt( 10000 ) + 1 ) );
                result += search( ctx, dn );
            }
    
            long t1 = System.currentTimeMillis();
            
            System.out.println( "Delta = " + ( t1 - t0 ) + " for " + result + " entries" );
        }
        catch ( NamingException ne )
        {
            
        }
    }

    private static int search( DirContext ctx, String dn )
    {

        try
        {

            Attributes attrs = ctx.getAttributes( dn );

            if ( attrs == null )
            {
                System.out.println( dn + "has no attributes" );
                return 0;
            }
            else
            {
                //System.out.println( attrs );
                return 1;
            }
        }
        catch ( NamingException e )
        {
            System.err.println( "Rdentry example failed." );
            e.printStackTrace();
            return 0;
        }
    }


    public static void main( String[] args ) throws NamingException
    {
        if ( args.length == 0 )
        {
            System.out
                .println( "Usage : java -jar test-ldap.jar <nb loop> <nb threads> <ldap URL> <principal> <password> <entry>" );
            System.out
                .println( "example : java -jar test-ldap.jar 1000 10 \"ldap://localhost:10389\" \"uid=admin,ou=system\" \"secret\" \"uid=user.{0},ou=Users,dc=cs,dc=hacettepe,dc=edu,dc=tr\"" );
            return;
        }

        nbLoop = Integer.parseInt( args[0] );
        int nbThreads = Integer.parseInt( args[1] );
        ldapUrl = args[2];
        principal = args[3];
        password = args[4];
        dnFormat = args[5];



        for ( int i = 0; i < nbThreads; i++ )
        {
            new SearchTest().start();
        }
    }
}
