ldap简单的性能测试

背景

第一次接触ldap时,是大三实习的那个暑假,使用java访问AD服务器中的用户属性,已经基本忘记如何搞了。这次本来是想用go语言和ldap写一个获取AD服务器中用户的安全组的例子,想测试一下可以达到多高的性能。但是无奈go语言下没有好的ldap库(只找到github中的一个go-ldap,不确定是否好--https://github.com/go-ldap/ldap,后面可以再试试)。
考虑到ldap操作,最出名的应该就是openldap,于是想先试试这个。

下载代码,编译

到http://www.openldap.org/software/download/OpenLDAP/openldap-release/ 下载最新的版本,lib的说明文档:http://www.openldap.org/software/man.cgi?query=ldap ,我下的是openldap-2.4.46.tgz,刚一开始下错了,下了2.4.9版本,一编译直接报错,上网查了说要用2.4.15以后的版本,于是就再次下载。
configure时,还会报缺失BDB的错误,由于不需要SLAPD,所以就--enable-sldap=no给跳过了。

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

上网随便找了一个代码,编译试试,果然可以。

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

测试性能:测试环境,老电脑AMD A10 6700,12G DDR3,hyper-V安装windows2012,并配置好域控;docker启动centos,在centos上编译基于openldap的测试代码。
测试代码如下:

#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;
}

测试结果:不加打印,执行1000次认证和search为6秒,加上打印为8秒;如果只做认证,不做search,为5秒。说明这里还有优化的余地。其中只认证一次,然后再循环search,执行第二次search时会卡在ldap_result上一段时间,通过抓包看,AD服务器每次在请求后,马上会回应,但是客户端会一直在发送zeroWindow和KeepAlive消息,估计是openldap中自己在做等待,而不是服务器响应的问题。

posted @ 2019-09-01 12:13  小懿大侠  阅读(1511)  评论(0编辑  收藏  举报