ldap simple performance testing

background

When the first contact ldap, a junior internship that summer, using java access user attributes AD server, it has been basically forgotten how to engage. The language and wanted to go to write an example ldap server security group in AD user acquisition, I would like to test how high performance can be achieved. But there is no good ldap library (found only in a go-ldap github, uncertain whether good - go under the helpless language https://github.com/go-ldap/ldap, you can try again later ).
Considering the ldap operation, the most famous should be the openldap, so would like to try this.

Download the code, compile

Download the latest version http://www.openldap.org/software/download/OpenLDAP/openldap-release/, lib documentation: http://www.openldap.org/software/man.cgi?query=ldap , I was under openldap-2.4.46.tgz, just at the beginning of the wrong, the next version 2.4.9, a compiler error directly, said after the Internet search to use the 2.4.15 version, then download it again.
configure, the BDB also reported missing error, because no SLAPD, so we --enable-sldap = no to skip.

./configure --enable-slapd=no
make depend
make
make install

Internet just to find a code, compile try, it really can be.

gcc t1.c -L /usr/local/lib/ -lldap -llber

Performance test: the test environment, the old computer AMD A10 6700,12G DDR3, hyper-V installed windows2012, and configure domain controllers; docker start centos, centos compiled based on openldap test code.
Test code is as follows:

#include <stdio.h>
#include <ldap.h>
#include <sys/time.h>


void print_result(LDAP* ld, LDAPMessage *e)
{
    BerElement *ber;
    char *a;
    char **vals;
    int i;

    for ( a = ldap_first_attribute( ld, e, &ber ); a != NULL; a = ldap_next_attribute( ld, e, ber ) )
    {
        if ((vals = ldap_get_values( ld, e, a)) != NULL )
        {
            for ( i = 0; vals[i] != NULL; i++ )
            {
                //printf( "%s: %s\n", a, vals[i] );

                if (0 == strcmp("memberOf", a))
                {
                    printf("get team:%s\n", vals[i]);
                }
            }
            //printf("print one val end\n");
            ldap_value_free( vals );
        }
        ldap_memfree( a );
    }

    if ( ber != NULL )
    {
        ber_free( ber, 0 );
    }

    //printf("print end\n");
}

void search_ext(LDAP *ld, char *finddn, char *filter)
{
    int rc;
    int msg;
    LDAPMessage *result, *e;
    int finish = 0;
    struct timeval tm = {0};
    tm.tv_sec = -1;

    rc = ldap_search_ext(ld, finddn, LDAP_SCOPE_SUBTREE, filter, NULL, 0, NULL, NULL, NULL, &tm, &msg);
    if (rc != LDAP_SUCCESS)
    {
        fprintf(stderr, "ldap_search_ext_s: rc: %d, %s\n", rc, ldap_err2string(rc));
        return( 1 );
    }

    //printf("ldap_search_ext success\n");

    int r = ldap_result(ld, msg, LDAP_MSG_ONE, NULL, &result);
    if (r > 0)
    {
        for (e = ldap_first_message(ld, result); e != NULL; e = ldap_next_message(ld, result))
        {
            print_result(ld, e);
        }
    }

    //printf("search_ext end\n");

    return;

}


int main()
{
    LDAP *ld;
    #define HOSTNAME "192.168.1.110"
    #define PORT_NUMBER 389
    #define FIND_DN "dc=test,dc=com"

    LDAPMessage *result, *e;
    BerElement *ber;
    char *a;
    char **vals;
    int i, rc;
    int i_version = 3;
    struct timeval tm = {0};
    tm.tv_sec = -1;

    ldap_set_option(NULL, LDAP_OPT_PROTOCOL_VERSION, &i_version);
    ldap_set_option(NULL, LDAP_OPT_REFERRALS, LDAP_OPT_ON);

    time_t t1 = time(NULL);
    for (i = 0; i < 1000; i++)
    {
        if ( (ld = ldap_init( HOSTNAME, PORT_NUMBER )) == NULL )
        {  
            perror( "ldap_init" );
            return( 1 );
        }
        //printf( "ldap_init success\n" );

        rc = ldap_simple_bind_s( ld, "cn=administrator,cn=Users,dc=test,dc=com", "Yanhong001");
        if ( rc != LDAP_SUCCESS )
        {
            fprintf(stderr, "ldap_simple_bind_s: rc: %d, %s\n", rc, ldap_err2string(rc));
            return( 1 );
        }

        // printf( "ldap_simple_bind_s success\n" );

        search_ext(ld, "dc=test, dc=com", "(&(objectclass=person)(sAMAccountName=user001))");

        //printf("searchexe end:%d\n", i);

        ldap_unbind_ext(ld, NULL, NULL);
    }
    time_t t2 = time(NULL);

    printf("time:%d\n", t2-t1);

    return 0;
}

Test results: without print, authentication and search execution times 1000 to 6 seconds, with print 8 seconds; if only authentication, not search, 5 seconds. Description there is room for optimization here. Which certified only once, and then recycled search, will be stuck in a period of time when ldap_result perform a second search, looking through the capture, AD server after each request, immediately respond, but the client will always send zeroWindow and KeepAlive message, it is estimated that they are doing in openldap wait, rather than the server responding.

Guess you like

Origin www.cnblogs.com/luckpiky/p/11441919.html