Skip to content

Instantly share code, notes, and snippets.

@wiserfirst
Last active March 6, 2018 23:25
Show Gist options
  • Save wiserfirst/7a304cb833cef738220a4547739a1028 to your computer and use it in GitHub Desktop.
Save wiserfirst/7a304cb833cef738220a4547739a1028 to your computer and use it in GitHub Desktop.
Dynamics test program
Total Processed: 100
Total Success: 100
Total Failed: 0
Total Exceptions: 0
Total Test Data: 100
Parallel Threads: 2
Time Elapsed (ms): 155385
Total Processed: 100
Total Success: 100
Total Failed: 0
Total Exceptions: 0
Total Test Data: 100
Parallel Threads: 5
Time Elapsed (ms): 156097
Total Processed: 100
Total Success: 91
Total Failed: 9
Total Exceptions: 0
Total Test Data: 100
Parallel Threads: 50
Time Elapsed (ms): 142023
Total Processed: 100
Total Success: 100
Total Failed: 0
Total Exceptions: 0
Total Test Data: 100
Parallel Threads: 10
Time Elapsed (ms): 175942
Total Processed: 100
Total Success: 100
Total Failed: 0
Total Exceptions: 0
Total Test Data: 100
Parallel Threads: 20
Time Elapsed (ms): 172201
Total Processed: 100
Total Success: 100
Total Failed: 0
Total Exceptions: 0
Total Test Data: 100
Parallel Threads: 10
Time Elapsed (ms): 168902
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PP.CRM.PascalMessage.Tester
{
class Program
{
static void Main(string[] args)
{
StagingContactTokenTester tester = new StagingContactTokenTester();
tester.Run();
}
}
}
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IdentityModel.Tokens;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace PP.CRM.PascalMessage.Tester
{
public class StagingContactTokenTester
{
//TODO: Uncomment then substitute your correct Dynamics 365 organization service
// address for either CRM Online or on-premise (end with a forward-slash).
private static string serviceUrl = "https://pascalpress.crm6.dynamics.com/"; // CRM Online
//private static string serviceUrl = "https://<organization name>.<domain name>/"; // CRM IFD
//private statics string serviceUrl = "http://myserver/myorg/"; // CRM on-premises
//TODO: For an on-premises deployment, set your organization credentials here. (If
// online or IFD, you can you can disregard or set to null.)
private static string userAccount = null; //CRM user account
private static string domain = null; //CRM server domain
//TODO: For CRM Online or IFD deployments, substitute your app registration values
// here. (If on-premise, you can disregard or set to null.)
private static string clientId = "dc290830-99b0-4e3b-ab2e-cd16c55cf3aa"; //e.g. "e5cf0024-a66a-4f16-85ce-99ba97a24bb2"
private static string redirectUrl = "https://d365salesapptest.sable37.com"; //e.g. "http://localhost/SdkSample"
public void Run()
{
int maxParallel = 10;
int numberOfTestData = 100;
int totalProcessed = 0;
int totalSuccess = 0;
int totalFailed = 0;
var exceptions = new ConcurrentQueue<Exception>();
List<string> testData = new List<string>();
var stagingContacts = @"{""pas_activated_at"" : ""2013-05-16T23:54:55.000000Z"",
""pas_address1"" : ""Primary Department"", ""pas_address2"" : ""60 Park Pde"",
""pas_bel_contact_id"" : ""1577895"", ""pas_country"" : ""Australia"",
""pas_created_at"" : ""2013-05-16T23:54:55Z"",
""pas_email"" : ""rbutt@stpatricks.qld.edu.au"", ""pas_first_name"" : ""Rosemary"",
""pas_last_login_at"" : ""2013-05-31T02:42:57.000000Z"",
""pas_last_name"" : ""Butt"", ""pas_login"" : ""paddysboy01"",
""pas_phone_number"" : ""07 3631 9000"", ""pas_postcode"" : ""4017"",
""pas_school_code"" : ""P37573"", ""pas_source"" : ""bel"", ""pas_state"" : ""QLD"",
""pas_town"" : ""SHORNCLIFFE"", ""pas_updated_at"" : ""2013-09-05T23:57:26Z"",
""pas_user_type"" : ""Teacher""}";
var stagingContactsFailed = "{\"pas_user_type\":\"Teacher\",\"pas_updated_at\":\"2013-08-01T06:47:39Z\",\"pas_town\":\"OAKHURST\",\"pas_state\":\"NSW\",\"pas_source\":\"bel\",\"pas_school_code\":\"P40253\",\"pas_postcode\":\"2761\",\"pas_phone_number\":\"02 9677 2455\",\"pas_login\":\"ewhary@rjas.nsw.edu.au\",\"pas_last_name\":\"Whary\",\"pas_first_name\":\"Michelle\",\"pas_email\":\"ewhary@rjas.nsw.edu.au\",\"pas_created_at\":\"2013-08-01T06:45:37Z\",\"pas_country\":\"Australia\",\"pas_bel_contact_id\":\"1683405\",\"pas_address2\":\"93 Hyatts Rd\",\"pas_address1\":\"Primary Department\",\"pas_activated_at\":\"2013-08-01T06:45:37.000000Z\"}";
for (int i = 0; i < numberOfTestData; i++)
{
testData.Add(stagingContacts);
}
Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();
Parallel.ForEach(testData, new ParallelOptions { MaxDegreeOfParallelism = maxParallel }, d =>
{
try
{
ProcessStagingRecord(ref totalSuccess, ref totalFailed, d);
totalProcessed++;
}
catch (Exception e)
{
exceptions.Enqueue(e);
}
});
sw.Stop();
Console.WriteLine("Total Processed: {0}", totalProcessed);
Console.WriteLine("Total Success: {0}", totalSuccess);
Console.WriteLine("Total Failed: {0}", totalFailed);
Console.WriteLine("Total Exceptions: {0}", exceptions.Count);
Console.WriteLine("Total Test Data: {0}", numberOfTestData);
Console.WriteLine("Parallel Threads: {0}", maxParallel);
Console.WriteLine("Time Elapsed (ms): {0}", sw.ElapsedMilliseconds);
var logName = string.Format("log_{0}.txt", DateTime.Now.ToString("ddMMyyy_HHmmss"));
using (StreamWriter logger = new StreamWriter(logName))
{
logger.WriteLine("Total Processed: {0}", totalProcessed);
logger.WriteLine("Total Success: {0}", totalSuccess);
logger.WriteLine("Total Failed: {0}", totalFailed);
logger.WriteLine("Total Exceptions: {0}", exceptions.Count);
logger.WriteLine("Total Test Data: {0}", numberOfTestData);
logger.WriteLine("Parallel Threads: {0}", maxParallel);
logger.WriteLine("Time Elapsed (ms): {0}", sw.ElapsedMilliseconds);
foreach (var item in exceptions)
{
logger.WriteLine(item.ToString());
}
}
Console.ReadLine();
}
private static void ProcessStagingRecord(ref int totalSuccess, ref int totalFailed, string payload)
{
Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext authContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext("https://login.windows.net/pascalpress.com.au", false);
//AuthenticationResult result = authContext.AcquireToken(serviceUrl, clientId, new UserCredential("REMOLSalesSvcTest@pascalpress.com.au", ""));
AuthenticationResult result = authContext.AcquireToken(serviceUrl, clientId, new UserCredential("REMOLSalesSvc@pascalpress.com.au", ""));
//Create an HTTP client to send a request message to the CRM Web service.
using (HttpClient httpClient = new HttpClient())
{
//Specify the Web API address of the service and the period of time each request
// has to execute.
httpClient.BaseAddress = new Uri(serviceUrl);
httpClient.Timeout = new TimeSpan(0, 2, 0); //2 minutes
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "api/data/v8.2/pas_stagingcontacts");
request.Content = new StringContent(payload, Encoding.UTF8, "application/json");
//Send the post request to the Web API using a GET request.
HttpResponseMessage response = httpClient.SendAsync(request).Result;
if (response.IsSuccessStatusCode)
{
//Get the response content and parse it.
//Console.WriteLine(response.StatusCode);
totalSuccess++;
}
else
{
Console.WriteLine("{0}: {1}", response.StatusCode, response.ReasonPhrase);
totalFailed++;
}
}
}
/// <summary> Displays exception information to the console. </summary>
/// <param name="ex">The exception to output</param>
private static void DisplayException(Exception ex)
{
Console.WriteLine("The application terminated with an error.");
Console.WriteLine(ex.Message);
while (ex.InnerException != null)
{
Console.WriteLine("\t* {0}", ex.InnerException.Message);
ex = ex.InnerException;
}
}
}
}
@stuart
Copy link

stuart commented Mar 6, 2018

++ is not threadsafe...it is probably not a problem here, but I would not rely on the totalSuccess and totalFailed being accurate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment