4

我一直在对我构建的 REST API 中的性能进行故障排除,除其他外,它根据提供的搜索词从 Active Directory 中返回用户列表。根据我为测试目的而内置的一些日志记录,我可以看到获取设置(例如 LDAP 搜索信息)和检索所有搜索结果的整个过程不到一秒钟:

30/08/2017 3:37:58 PM | Getting search results.
30/08/2017 3:37:58 PM | Retrieving default settings
30/08/2017 3:37:58 PM | Default settings retrieved. Creating directoryEntry
30/08/2017 3:37:58 PM | Search retrieved.
30/08/2017 3:37:58 PM | Iterating through search results.
30/08/2017 3:38:16 PM | Search results iteration complete.

但是,正如您所见,遍历这些搜索结果并填充我的用户列表需要 18 秒。这是我的代码:

SearchResultCollection resultList = new DirectorySearcher(CreateDirectoryEntry())
{
    Filter = ("(&(objectClass=user) (cn=*" + SearchTerm + "*))"),
    PropertiesToLoad =
    {
        "givenName",
        "sn",
        "sAMAccountName",
        "mail"
    }
}.FindAll();

foreach (SearchResult result in resultList)
{
    ADUser thisUser = new ADUser();

    try
    {
        thisUser.Firstname = result.Properties["givenName"][0].ToString();
    }
    catch
    {
        thisUser.Firstname = "Firstname not found";
    }
    try
    {
        thisUser.Lastname = result.Properties["sn"][0].ToString();
    }
    catch
    {
        thisUser.Lastname = "Lastname not found";
    }
    try
    {
        thisUser.EmailAddress = result.Properties["mail"][0].ToString();
    }
    catch
    {
        thisUser.EmailAddress = "Email address not found";
    }

    UserList.Add(thisUser);
}

这很香草,没有做任何花哨的事情。知道为什么这会这么慢,或者有什么建议我可以做些什么来加快速度吗?

更新

根据评论和答案,我从代码中删除了 null 检查。所以现在它看起来像这样:

foreach (SearchResult result in resultList)
{
    ADUser thisUser = new ADUser();

    thisUser.Firstname = result.Properties["givenName"][0].ToString();
    thisUser.Lastname = result.Properties["sn"][0].ToString();
    thisUser.EmailAddress = result.Properties["mail"][0].ToString();

    UserList.Add(thisUser);
}

这并没有提高性能。我可以看到这个循环仍然需要大约 18 秒,即使只返回一个结果。(这也证明了我的广告中糟糕的数据意味着我需要这个空检查!)

4

2 回答 2

3

在设置属性时,您依赖于异常thisUser,具体取决于您的目录,对于用户而言,没有填充其 、 和/或属性可能并不是那么异常。如果您有很长的搜索结果列表,所有这些例外情况都可以加起来。考虑检查所需的属性是否存在而不是使用/块:givenNamesnmailtrycatch

thisUser.Firstname = result.Properties.Contains("givenName")
    ? result.Properties["givenName"][0].ToString()
    : "Firstname not found";
thisUser.Lastname = result.Properties.Contains("sn")
    ? result.Properties["sn"][0].ToString()
    : "Lastname not found";
thisUser.EmailAddress = result.Properties.Contains("mail")
    ? result.Properties["mail"][0].ToString()
    : "Email address not found";

您可以将C# 6 中的null条件运算符与null-coalescing 运算符一起使用,以将其简化为以下内容:

thisUser.Firstname = result.Properties["givenName"]?[0]?.ToString() ?? "Firstname not found";
thisUser.Lastname = result.Properties["sn"]?[0]?.ToString() ?? "Lastname not found";
thisUser.EmailAddress = result.Properties["mail"]?[0]?.ToString() ?? "Email address not found";
于 2017-08-30T06:12:48.143 回答
1

你可以尝试这样的事情:

foreach (SearchResult result in resultList)
            {
                ADUser thisUser = new ADUser();
                if(result.Properties["givenName"][0] != null)
                    thisUser.Firstname = result.Properties["givenName"][0].ToString();
                else
                    thisUser.Firstname = "Firstname not found";

                if(thisUser.Lastname = result.Properties["sn"][0] != null)
                    thisUser.Lastname = result.Properties["sn"][0].ToString();
                else
                    thisUser.Lastname = "Lastname not found";

                if(result.Properties["mail"][0] != null)
                    thisUser.EmailAddress = result.Properties["mail"][0].ToString();
                else
                    thisUser.EmailAddress = "Email address not found";

                UserList.Add(thisUser);
            }
于 2017-08-30T06:12:34.123 回答