How can I speed up my code?
-
I am running this from one domain and against a different domain. The process is incredibly slow and I'm wondering how I can speed my code up so that instead of it taking 20 minutes, it will only take 1 minute. I have noticed that every once in a while it will only take 1 minute and I'm wondering if I need to point it to a specific DC in the domain I am looking through or maybe something else? getAllGroupMemberGroupsForGroup("membergroup", "domain.domain.com") private List getAllGroupMemberGroupsForGroup(string groupName, string domainName) { PrincipalContext context = new PrincipalContext(ContextType.Domain, domainName); GroupPrincipal objects = GroupPrincipal.FindByIdentity(context, groupName); List groupMembersList = new List(); PrincipalSearchResult objectMembers = null; if (objects != null) { objectMembers = objects.GetMembers(); } else { return groupMembersList; } foreach (Principal objectMember in objectMembers) { if (objectMember.StructuralObjectClass == "group") { string objectDomain = objectMember.Context.Name; groupMembersList.Add(objectMember.Name + "|" + objectDomain + "|" + objectMember.DistinguishedName); } } return groupMembersList; }
-
I am running this from one domain and against a different domain. The process is incredibly slow and I'm wondering how I can speed my code up so that instead of it taking 20 minutes, it will only take 1 minute. I have noticed that every once in a while it will only take 1 minute and I'm wondering if I need to point it to a specific DC in the domain I am looking through or maybe something else? getAllGroupMemberGroupsForGroup("membergroup", "domain.domain.com") private List getAllGroupMemberGroupsForGroup(string groupName, string domainName) { PrincipalContext context = new PrincipalContext(ContextType.Domain, domainName); GroupPrincipal objects = GroupPrincipal.FindByIdentity(context, groupName); List groupMembersList = new List(); PrincipalSearchResult objectMembers = null; if (objects != null) { objectMembers = objects.GetMembers(); } else { return groupMembersList; } foreach (Principal objectMember in objectMembers) { if (objectMember.StructuralObjectClass == "group") { string objectDomain = objectMember.Context.Name; groupMembersList.Add(objectMember.Name + "|" + objectDomain + "|" + objectMember.DistinguishedName); } } return groupMembersList; }
By domain, you mean
AppDomain
? If so, performance will always be terrible because it has to serialize everything when crossing a domain. If not, then ignore this answer. MarcImperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project! Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny
-
I am running this from one domain and against a different domain. The process is incredibly slow and I'm wondering how I can speed my code up so that instead of it taking 20 minutes, it will only take 1 minute. I have noticed that every once in a while it will only take 1 minute and I'm wondering if I need to point it to a specific DC in the domain I am looking through or maybe something else? getAllGroupMemberGroupsForGroup("membergroup", "domain.domain.com") private List getAllGroupMemberGroupsForGroup(string groupName, string domainName) { PrincipalContext context = new PrincipalContext(ContextType.Domain, domainName); GroupPrincipal objects = GroupPrincipal.FindByIdentity(context, groupName); List groupMembersList = new List(); PrincipalSearchResult objectMembers = null; if (objects != null) { objectMembers = objects.GetMembers(); } else { return groupMembersList; } foreach (Principal objectMember in objectMembers) { if (objectMember.StructuralObjectClass == "group") { string objectDomain = objectMember.Context.Name; groupMembersList.Add(objectMember.Name + "|" + objectDomain + "|" + objectMember.DistinguishedName); } } return groupMembersList; }
This doesn't make a lot of sense. Are you getting all of the group memberships of a specified user or are you getting all of the group memberships of a list of users who are members of a specified group, or what? You never spelled out exactly what this code is supposed to be doing.
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave Kreskowiak -
This doesn't make a lot of sense. Are you getting all of the group memberships of a specified user or are you getting all of the group memberships of a list of users who are members of a specified group, or what? You never spelled out exactly what this code is supposed to be doing.
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave KreskowiakMy goal is to enumerate through the groups and subgroups and get all of the non duplicate groups/subgroups and put them into a list. After that I checked to see if a specific user is a member of any of the groups I've enumerated to the list of groups/subgroups.
-
My goal is to enumerate through the groups and subgroups and get all of the non duplicate groups/subgroups and put them into a list. After that I checked to see if a specific user is a member of any of the groups I've enumerated to the list of groups/subgroups.
"Enumerate through the groups and subgroups" of what? What is the starting object for this query? Are you returning ALL of the groups in the directory? There is no such thing as a "subgroup". What I think you're referring to is a group that is a member of another group. Or why not just start with the user object and get the list of groups it is a member of?
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave Kreskowiak -
"Enumerate through the groups and subgroups" of what? What is the starting object for this query? Are you returning ALL of the groups in the directory? There is no such thing as a "subgroup". What I think you're referring to is a group that is a member of another group. Or why not just start with the user object and get the list of groups it is a member of?
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave KreskowiakI am enumerating through groups recursively. There is a starting parent group, and I'm starting with the parent and enumerating through all of the child groups of that parent and every group that is a member of the child groups that are found. At my organization, there may be a nested hierarchy of 12 groups or more and unfortunately there may be some of the same groups nested in other groups as the local admin qualification is to have a pulse. Does that explain it better?
-
I am enumerating through groups recursively. There is a starting parent group, and I'm starting with the parent and enumerating through all of the child groups of that parent and every group that is a member of the child groups that are found. At my organization, there may be a nested hierarchy of 12 groups or more and unfortunately there may be some of the same groups nested in other groups as the local admin qualification is to have a pulse. Does that explain it better?
That's better. If you are making a request to the domain controller for every group at every level, you're going through an expensive operation, every single time. Just because you're code hits one domain controller for one request does not mean that the same domain controller is going to service all subsequent requests. You could be throwing requests all over the network without knowing it. Now, each request is expensive so it would be better to make them once and then cache the result. Create a Dictionary to cache the objects returned. When you make a request for a group, lookup the group name in the cache to see if it's there first. If not, go to the directory to get it and cache it for use next time around. Whenever you go get a list of groups, add them to the cache if needed.
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave Kreskowiak -
That's better. If you are making a request to the domain controller for every group at every level, you're going through an expensive operation, every single time. Just because you're code hits one domain controller for one request does not mean that the same domain controller is going to service all subsequent requests. You could be throwing requests all over the network without knowing it. Now, each request is expensive so it would be better to make them once and then cache the result. Create a Dictionary to cache the objects returned. When you make a request for a group, lookup the group name in the cache to see if it's there first. If not, go to the directory to get it and cache it for use next time around. Whenever you go get a list of groups, add them to the cache if needed.
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave KreskowiakI have not created a dictionary before, so do you recommend this be stored in a text file or in a database? This morning I got 323 group results in 65 seconds, I would imagine that when I try after lunch it will take 10x as long. Is it better for me to try and dictate which DC I am hitting? I can view which DC I'm hitting, so maybe my issue of sporadicness is actually a DC problem? What do you think?
-
I have not created a dictionary before, so do you recommend this be stored in a text file or in a database? This morning I got 323 group results in 65 seconds, I would imagine that when I try after lunch it will take 10x as long. Is it better for me to try and dictate which DC I am hitting? I can view which DC I'm hitting, so maybe my issue of sporadicness is actually a DC problem? What do you think?
Dictionary Class[^] You don't store it anywhere.
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave Kreskowiak