Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shahzaibj/perf concurrency investigation #2045

Draft
wants to merge 3 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package com.microsoft.identity.client.msal.automationapp.testpass.perf.broker.joined;

import android.util.Log;

import com.microsoft.identity.client.IAccount;
import com.microsoft.identity.client.Prompt;
import com.microsoft.identity.client.msal.automationapp.R;
import com.microsoft.identity.client.msal.automationapp.sdk.MsalAuthResult;
import com.microsoft.identity.client.msal.automationapp.sdk.MsalAuthTestParams;
import com.microsoft.identity.client.msal.automationapp.sdk.MsalSdk;
import com.microsoft.identity.client.msal.automationapp.testpass.broker.AbstractMsalBrokerTest;
import com.microsoft.identity.client.ui.automation.TokenRequestTimeout;
import com.microsoft.identity.client.ui.automation.interaction.PromptHandlerParameters;
import com.microsoft.identity.client.ui.automation.interaction.PromptParameter;
import com.microsoft.identity.client.ui.automation.interaction.microsoftsts.AadPromptHandler;
import com.microsoft.identity.labapi.utilities.client.LabQuery;
import com.microsoft.identity.labapi.utilities.constants.TempUserType;
import com.microsoft.identity.labapi.utilities.constants.UserType;

import org.junit.Test;

import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestBrokerPerfJoined extends AbstractMsalBrokerTest {

final ExecutorService executorService = Executors.newFixedThreadPool(25);

int f = 1;

@Test
public void testBrokerPerJoined() throws Throwable {
final String username = mLabAccount.getUsername();
final String password = mLabAccount.getPassword();

mBroker.performDeviceRegistration(username, password);

final MsalSdk msalSdk = new MsalSdk();

// Interactive call
final MsalAuthTestParams authTestParams = MsalAuthTestParams.builder()
.activity(mActivity)
.loginHint(username)
.scopes(Arrays.asList(getScopes()[0]))
.promptParameter(Prompt.SELECT_ACCOUNT)
.msalConfigResourceId(getConfigFileResourceId())
.build();

final MsalAuthResult authResult = msalSdk.acquireTokenInteractive(authTestParams, new com.microsoft.identity.client.ui.automation.interaction.OnInteractionRequired() {
@Override
public void handleUserInteraction() {
final PromptHandlerParameters promptHandlerParameters = PromptHandlerParameters.builder()
.prompt(PromptParameter.SELECT_ACCOUNT)
.loginHint(username)
.sessionExpected(true)
.consentPageExpected(false)
.speedBumpExpected(false)
.broker(mBroker)
.expectingBrokerAccountChooserActivity(false)
.build();

new AadPromptHandler(promptHandlerParameters)
.handlePrompt(username, password);
}
}, TokenRequestTimeout.MEDIUM);

authResult.assertSuccess();

final IAccount account = msalSdk.getAccount(mActivity,getConfigFileResourceId(),username);

final int numberOfOccurrenceOfTest = getScopes().length * f;

CompletableFuture<Boolean> future = new CompletableFuture<>();

// Silent call
for(int i = 0; i < numberOfOccurrenceOfTest; i++) {
int finalI = i;
executorService.submit(new Runnable() {
@Override
public void run() {
final MsalAuthTestParams silentParams = MsalAuthTestParams.builder()
.activity(mActivity)
.loginHint(username)
.authority(account.getAuthority())
.forceRefresh(true)
.scopes(Arrays.asList(getScopes()[finalI % f]))
.msalConfigResourceId(getConfigFileResourceId())
.build();


final long startTime = System.currentTimeMillis();

final MsalAuthResult silentAuthResult;
try {
silentAuthResult = msalSdk.acquireTokenSilent(silentParams, TokenRequestTimeout.SILENT);
} catch (Throwable e) {
future.completeExceptionally(e);
throw new RuntimeException(e);
}

final long endTime = System.currentTimeMillis();
final long elapsedTime = endTime - startTime;
Log.d("Perf", "Elapsed time for #" + finalI + " = " + elapsedTime);

silentAuthResult.assertSuccess();

if (finalI == (numberOfOccurrenceOfTest - 1)) {
future.complete(true);
}
}
});
}

future.get();
}

@Override
public LabQuery getLabQuery() {
return LabQuery.builder()
.userType(UserType.CLOUD)
.build();
}

@Override
public TempUserType getTempUserType() {
return null;
}

@Override
public String[] getScopes() {
return new String[]{
"user.read",
"https://graph.microsoft.com/user.read",
"https://graph.windows.net/user.read",
"https://outlook.office.com/user.read",
"https://graph.microsoft.com/.default"
};
}

@Override
public String getAuthority() {
return "https://login.microsoftonline.com/common";
}

@Override
public int getConfigFileResourceId() {
return R.raw.msal_config_default;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
// Copyright (c) Microsoft Corporation.
// All rights reserved.
//
// This code is licensed under the MIT License.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package com.microsoft.identity.client.msal.automationapp.testpass.perf.broker.nonjoined;

import android.util.Log;

import com.microsoft.identity.client.IAccount;
import com.microsoft.identity.client.Prompt;
import com.microsoft.identity.client.msal.automationapp.R;
import com.microsoft.identity.client.msal.automationapp.sdk.MsalAuthResult;
import com.microsoft.identity.client.msal.automationapp.sdk.MsalAuthTestParams;
import com.microsoft.identity.client.msal.automationapp.sdk.MsalSdk;
import com.microsoft.identity.client.msal.automationapp.testpass.broker.AbstractMsalBrokerTest;
import com.microsoft.identity.client.ui.automation.TokenRequestTimeout;
import com.microsoft.identity.client.ui.automation.annotations.RetryOnFailure;
import com.microsoft.identity.client.ui.automation.interaction.OnInteractionRequired;
import com.microsoft.identity.client.ui.automation.interaction.PromptHandlerParameters;
import com.microsoft.identity.client.ui.automation.interaction.PromptParameter;
import com.microsoft.identity.client.ui.automation.interaction.microsoftsts.AadPromptHandler;
import com.microsoft.identity.labapi.utilities.client.LabQuery;
import com.microsoft.identity.labapi.utilities.constants.TempUserType;
import com.microsoft.identity.labapi.utilities.constants.UserType;

import org.junit.Test;

import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

// Broker Auth for non-joined account - select_account
// https://identitydivision.visualstudio.com/DefaultCollection/DevEx/_workitems/edit/497069
//@RetryOnFailure(retryCount = 2)
public class TestBrokerPerfNonJoined extends AbstractMsalBrokerTest {

final ExecutorService executorService = Executors.newFixedThreadPool(25);

int f = 2;

@Test
public void testBrokerPerfNonJoined() throws Throwable {
final String username = mLabAccount.getUsername();
final String password = mLabAccount.getPassword();

final MsalSdk msalSdk = new MsalSdk();

// Interactive call
final MsalAuthTestParams authTestParams = MsalAuthTestParams.builder()
.activity(mActivity)
.loginHint(username)
.scopes(Arrays.asList(getScopes()[0]))
.promptParameter(Prompt.SELECT_ACCOUNT)
.msalConfigResourceId(getConfigFileResourceId())
.build();

final MsalAuthResult authResult = msalSdk.acquireTokenInteractive(authTestParams, new com.microsoft.identity.client.ui.automation.interaction.OnInteractionRequired() {
@Override
public void handleUserInteraction() {
final PromptHandlerParameters promptHandlerParameters = PromptHandlerParameters.builder()
.prompt(PromptParameter.SELECT_ACCOUNT)
.loginHint(username)
.sessionExpected(false)
.consentPageExpected(false)
.speedBumpExpected(false)
.broker(mBroker)
.expectingBrokerAccountChooserActivity(false)
.build();

new AadPromptHandler(promptHandlerParameters)
.handlePrompt(username, password);
}
}, TokenRequestTimeout.MEDIUM);

authResult.assertSuccess();

final IAccount account = msalSdk.getAccount(mActivity,getConfigFileResourceId(),username);

final int numberOfOccurrenceOfTest = getScopes().length * f;

CompletableFuture<Boolean> future = new CompletableFuture<>();

// Silent call
for(int i = 0; i < numberOfOccurrenceOfTest; i++) {
int finalI = i;
executorService.submit(new Runnable() {
@Override
public void run() {
final MsalAuthTestParams silentParams = MsalAuthTestParams.builder()
.activity(mActivity)
.loginHint(username)
.authority(account.getAuthority())
.forceRefresh(true)
.scopes(Arrays.asList(getScopes()[finalI % f]))
.msalConfigResourceId(getConfigFileResourceId())
.build();


final long startTime = System.currentTimeMillis();

final MsalAuthResult silentAuthResult;
try {
silentAuthResult = msalSdk.acquireTokenSilent(silentParams, TokenRequestTimeout.SILENT);
} catch (Throwable e) {
future.completeExceptionally(e);
throw new RuntimeException(e);
}

final long endTime = System.currentTimeMillis();
final long elapsedTime = endTime - startTime;
Log.d("Perf", "Elapsed time for #" + finalI + " = " + elapsedTime);

silentAuthResult.assertSuccess();

if (finalI == (numberOfOccurrenceOfTest - 1)) {
future.complete(true);
}
}
});
}

future.get();
}

@Override
public LabQuery getLabQuery() {
return LabQuery.builder()
.userType(UserType.CLOUD)
.build();
}

@Override
public TempUserType getTempUserType() {
return null;
}

@Override
public String[] getScopes() {
return new String[]{
"user.read",
"https://graph.microsoft.com/user.read",
"https://graph.windows.net/user.read",
"https://outlook.office.com/user.read",
"https://graph.microsoft.com/.default"
};
}

@Override
public String getAuthority() {
return "https://login.microsoftonline.com/common";
}

@Override
public int getConfigFileResourceId() {
return R.raw.msal_config_default;
}

}
Loading