Fork me on GitHub

NuGet Snippet:

C:\_tmp\stressTest>nuget install -ExcludeVersion Microsoft.CrmSdk.CoreAssemblies
Attempting to resolve dependency 'Microsoft.IdentityModel (= 6.1.7600.16394)'.
Installing 'Microsoft.IdentityModel 6.1.7600.16394'.
Successfully installed 'Microsoft.IdentityModel 6.1.7600.16394'.
Installing 'Microsoft.CrmSdk.CoreAssemblies 6.1.0'.
Successfully installed 'Microsoft.CrmSdk.CoreAssemblies 6.1.0'.

C:\_tmp\stressTest>
C:\_tmp\stressTest>nuget install -ExcludeVersion FSharp.Data
Attempting to resolve dependency 'Zlib.Portable (= 1.9.2)'.
Installing 'Zlib.Portable 1.9.2'.
Successfully installed 'Zlib.Portable 1.9.2'.
Installing 'FSharp.Data 2.0.4'.
Successfully installed 'FSharp.Data 2.0.4'.

C:\_tmp\stressTest>

Script Snippet (DG.StressTest.cmd):

@echo off

:: Add the paths for the F# SDK 3.x (from higher version to lower)
set FSHARPSDK=^
C:\Program Files (x86)\Microsoft SDKs\F#\3.1\Framework\v4.0\;^
C:\Program Files (x86)\Microsoft SDKs\F#\3.0\Framework\v4.0\

cls

:: Execute the script "only" with the first "fsianycpu.exe" found
for %%i in (fsianycpu.exe) do "%%~$FSHARPSDK:i" DG.StressTest.fsx %*

pause

Code Snippet (DG.StressTest.fsx):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#r @"System.Runtime.Serialization"
#r @"System.ServiceModel"
 
// nuget install -ExcludeVersion Microsoft.CrmSdk.CoreAssemblies
#r @"Microsoft.CrmSdk.CoreAssemblies\lib\net40\Microsoft.Xrm.Sdk.dll"
 
// nuget install -ExcludeVersion FSharp.Data
#r @"FSharp.Data\lib\net40\FSharp.Data.dll"
 
#load @"DG.Auth.fsx" // Just contains let usr = "usr" and let pwd = "pwd"
 
open System
open System.Runtime.Serialization
open System.ServiceModel.Description
 
open Microsoft.Xrm.Sdk
open Microsoft.Xrm.Sdk.Client
open Microsoft.Xrm.Sdk.Messages
open Microsoft.Xrm.Sdk.Query
 
open FSharp.Data
 
/// Utils
let r = new System.Random()

let crmCount (proxy:OrganizationServiceProxy) logicalName (date:DateTime) = 
  let f = FilterExpression()
  f.AddCondition(@"createdon", ConditionOperator.GreaterEqual, date.ToUniversalTime())
 
  let q = QueryExpression(logicalName)
  q.ColumnSet <- ColumnSet(logicalName + "id")
  q.Criteria <- f
  q.PageInfo <- PagingInfo()
  q.PageInfo.PageNumber <- 1
 
  seq{ let resp = proxy.RetrieveMultiple(q)
       yield! resp.Entities
 
       let rec retrieveMultiple' (ec:EntityCollection) pn = seq{
         match ec.MoreRecords with
         | true ->
           q.PageInfo.PageNumber <- (pn + 1)
           q.PageInfo.PagingCookie <- ec.PagingCookie
 
           let resp' = proxy.RetrieveMultiple(q)
 
           yield! resp'.Entities
           yield! retrieveMultiple' resp' (pn + 1)
         | false -> () }
 
       yield! retrieveMultiple' resp 1 }
  |> Seq.length

/// Connection info:
let uri = Uri("https://org.api.crm4.dynamics.com/XRMServices/2011/Organization.svc");
 
let ac = AuthenticationCredentials()
ac.ClientCredentials.UserName.UserName <- DG.Auth.usr
ac.ClientCredentials.UserName.Password <- DG.Auth.pwd
 
let m = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(uri)
let tc = m.Authenticate(ac)
let p = new OrganizationServiceProxy(m, tc.SecurityTokenResponse)
 
/// Test data:
let data = FreebaseData.GetDataContext()
 
let names = data.Society.People.``Family names`` |> Seq.take 250
let namesCache = names |> Seq.toArray |> Array.map(fun x -> x.Name)
 
let titles = data.``Products and Services``.Business.``Job titles`` |> Seq.take 250
let titlesCache = titles |> Seq.toArray |> Array.map(fun x -> x.Name)
 
let products = data.``Products and Services``.``Food & Drink``.Foods |> Seq.take 250
let productsCache = products |> Seq.toArray |> Array.map(fun x -> x.Name)
 
let countries = data.Commons.Location.Countries |> Seq.take 250
let countriesCache = countries |> Seq.toArray |> Array.map(fun x -> x.Name)
 
let cities = data.Commons.Location.``City/Town/Villages`` |> Seq.take 250
let citiesCache = cities |> Seq.toArray |> Array.map(fun x -> x.Name)
 
let companyName name = name + " Company"
let streetName name = name + " Street " + string(r.Next(1,1000))
let phoneNumber () = "555-" + string(r.Next(1000,10000))
let zipCode () = string(r.Next(1000,10000))
let email firstname lastname = (firstname + "." + lastname + "@mail.co.dk").ToLower()
 
/// Create as many accounts as possible
let createAccount () =
  let a = Entity("account")
  a.Attributes.Add("name", 
    namesCache.[r.Next(0,250)] + " " + 
    namesCache.[r.Next(0,250)] |> companyName)
  a.Attributes.Add("telephone1", phoneNumber())
  a.Attributes.Add("address1_line1", namesCache.[r.Next(0,250)] |> streetName)
  a.Attributes.Add("address1_city", citiesCache.[r.Next(0,250)])
  a.Attributes.Add("address1_postalcode", zipCode())
  a.Attributes.Add("address1_country", countriesCache.[r.Next(0,250)])
  a
 
// One account per thread
let createAccounts date concurrency =
  Array.Parallel.init concurrency (fun _ -> createAccount ())
  |> Array.Parallel.map(
    fun x -> 
      try
        match (date > DateTime.Now) with
        | true -> 
          let p' = new OrganizationServiceProxy(m, tc.SecurityTokenResponse)
          p'.Create(x) |> Some
        | false -> None
      with ex -> None)
 
// Ten accounts per thread
let createAccounts' date concurrency =
  Array.Parallel.init concurrency
    (fun _ -> 
      let em = new ExecuteMultipleRequest()
      em.Settings <- new ExecuteMultipleSettings()
      em.Settings.ContinueOnError <- true
      em.Settings.ReturnResponses <- true
      em.Requests <- new OrganizationRequestCollection()
      em)
  |> Array.Parallel.map(
    fun x -> 
      try
        Array.Parallel.init 10
          (fun _ -> 
            let cr = new CreateRequest()
            cr.Target <- createAccount()
            x.Requests.Add(cr)) |> ignore
        match (date > DateTime.Now) with
        | true -> 
          let p' = new OrganizationServiceProxy(m, tc.SecurityTokenResponse)
          p'.Execute(x) :?> ExecuteMultipleResponse |> Some
        | false -> None
      with ex -> None)
 
/// Stress Test
let rec stressTestCrm date concurrency = 
  match (date > DateTime.Now) with
  | true -> 
    createAccounts' date concurrency |> ignore
    stressTestCrm date concurrency
  | false -> ()
 
/// Concurrent users (threads)
let concurrency = (1 <<< 10) // 1024
 
/// Start and Stop DateTimes
let startDate = DateTime.Now
let stopDate  = startDate.AddMinutes(60.)
 
/// Perfom stress test and print outcome
stressTestCrm stopDate concurrency
 
(crmCount p "account" startDate, startDate.ToString("o"))
||> printfn "Accounts created: %i, since: %s"

Code result:

Accounts created: 141225, since: 2014-09-16T22:31:53.9798783+02:00
Press any key to continue . . .

Architecture (Lenovo ThinkPad W540):

All

Code Snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#if INTERACTIVE
#r "System.Data.Services.Client"
#r "FSharp.Data.TypeProviders"
#endif

open System
open System.Net
open System.Data.Services.Client
open Microsoft.FSharp.Data

[<Literal>]
let url = @"https://demo.crm4.dynamics.com/XRMServices/2011/OrganizationData.svc/"

[<Literal>] // "%TMP%/odata/OrganizationData.csdl"
let csdl = __SOURCE_DIRECTORY__  + @"/odata/OrganizationData.csdl" 

type Xrm = 
    TypeProviders.ODataService<
        ServiceUri = url,
        LocalSchemaFile = csdl,
        ForceUpdate = false>

let ctx = Xrm.GetDataContext()

// To be used when writing JavaScript with OData
ctx.DataContext.SendingRequest.Add (
    fun eventArgs -> printfn "-Url: %A" eventArgs.Request.RequestUri)
ctx.DataContext.SendingRequest.Add (
    fun eventArgs -> printfn "-Query: %s" eventArgs.Request.RequestUri.Query)

// Remember to "pipe" to a Sequence, in order to evaluate the Linq Query:
query { for a in ctx.AccountSet do
        where (a.AccountNumber = "42")
        select (a.AccountNumber, a.AccountId)
        skip 5
        take 1} 
|> Seq.map id

Code output:

val url : string =
  "https:demo.crm4.dynamics.com/XRMServices/2011/OrganizationData.svc/"
val csdl : string =
  "C:\Users\rsm\AppData\Local\Temp\odata\OrganizationData.csdl"
type Xrm =
  class
    static member GetDataContext : unit -> Xrm.ServiceTypes.SimpleDataContextTypes.demoContext
     + 1 overload
    nested type ServiceTypes
  end
val ctx : Xrm.ServiceTypes.SimpleDataContextTypes.demoContext
val it : unit = ()
-Uri: https:demo.crm4.dynamics.com/XRMServices/2011/OrganizationData.svc/AccountSet()?$filter=AccountNumber eq '42'&$skip=5&$top=1&$select=AccountNumber,AccountId
-Query: $filter=AccountNumber eq '42'&$skip=5&$top=1&$select=AccountNumber,AccountId
val it : seq<string * Guid> = seq []

Remark:

The reason LocalSchemaFile = csdl and ForceUpdate = false are set to static values are because Microsoft still doesn’t allow us to use OData from a server side context (we still need to login to CRM Online with a browser that supports JavaScript). If anybody have a hint on how to access the CSDL service from a .NET application, please write a comment below.

The point is that even though there is no data returned in the .NET application from CRM Online, it doesn’t matter as we just want to use the queries (nicely written in Linq with intellisense and type-safety) outputted from fun eventArgs -> printfn "-Query: %s" eventArgs.Request.RequestUri.Query for our JavaScript code in combination with the official SDK.REST.js library:

1
2
3
4
5
6
7
8
9
SDK.REST.retrieveMultipleRecords(
  "Account",
  "$filter=AccountNumber eq '42'&$skip=5&$top=1&$select=AccountNumber,AccountId", // query
  function (results) {
    // Do stuff
  },
  errorHandler,
  onCompleteHandler
);

The current way it’s done (and built on @deprecated technologies) is just not very handy (and has never been) as it requires to expand your current CRM tenant with a 3rd party managed solution:

CRM 2011 OData Query Designer

References:

Code Snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
let NOT = function | 1 -> 0 | _ -> 1
let AND x y = match x,y with | (1,1) -> 1 | _ -> 0
let OR  x y = match x,y with | (0,0) -> 0 | _ -> 1
let XOR x y = match x,y with | (1,1) | (0,0) -> 0 | _ -> 1
let NAND x y = (x,y) ||> AND |> NOT
let NOR  x y = (x,y) ||> OR  |> NOT
let XNOR x y = (x,y) ||> XOR |> NOT

let HALFADDER x y = (x,y) ||> AND,(x,y) ||> XOR
let FULLADDER x y z =
  let c,s   = (x,y) ||> HALFADDER
  let c',s' = (z,s) ||> HALFADDER
  (c,c') ||> OR, s'

let itb n =
  System.Convert.ToString(0+n, 2).PadLeft(32,'0')
  |> Seq.map string |> Seq.map int |> Seq.toList

let bti (ls:int list) =
  ls |> List.map string |> List.reduce (+)
     |> fun x -> System.Convert.ToInt32(x,2)

let add x y =
  (x |> itb , y |> itb)
  ||> List.zip
  |> List.rev
  |> List.fold(
    fun (c,zs) (x,y) -> (x,y,c) |||> FULLADDER |> fun (c',z) -> c',z::zs) (0,[])
  |> fun (x,ys) -> ys |> bti

// Eight cases:
(0,0,0) |||> FULLADDER;;
(1,0,0) |||> FULLADDER;;
(0,1,0) |||> FULLADDER;;
(1,1,0) |||> FULLADDER;;
(0,0,1) |||> FULLADDER;;
(1,0,1) |||> FULLADDER;;
(0,1,1) |||> FULLADDER;;
(1,1,1) |||> FULLADDER;;

// Examples taken from 'Domino Addition - Numberphile'
(42,17) ||> add;;
(55,27) ||> add;

Code output:

val NOT : _arg1:int -> int
val AND : x:int -> y:int -> int
val OR : x:int -> y:int -> int
val XOR : x:int -> y:int -> int
val NAND : x:int -> y:int -> int
val NOR : x:int -> y:int -> int
val XNOR : x:int -> y:int -> int
val HALFADDER : x:int -> y:int -> int * int
val FULLADDER : x:int -> y:int -> z:int -> int * int
val itb : n:int -> int list
val bti : ls:int list -> int
val add : x:int -> y:int -> int

> val it : int * int = (0, 0)
> val it : int * int = (0, 1)
> val it : int * int = (0, 1)
> val it : int * int = (1, 0)
> val it : int * int = (0, 1)
> val it : int * int = (1, 0)
> val it : int * int = (1, 0)
> val it : int * int = (1, 1)

> val it : int = 59
> val it : int = 82

References:

Code Snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
type SeqMonad() =
  member t.Bind(m,f) = Seq.concat(Seq.map f m)
  member t.Return v = seq{ yield v }
let seqMonad = SeqMonad()

let permutations ls = 
  let rec insertions x = function
    | []             -> [[x]]
    | (y :: ys) as l -> (x::l)::(List.map (fun x -> y::x) (insertions x ys))
  let rec permutations' = function
    | []      -> seq [ [] ]
    | x :: xs -> Seq.concat (Seq.map (insertions x) (permutations' xs))
  ls |> permutations'

let md5 s =
  System.BitConverter
    .ToString(
      System.Security.Cryptography.MD5
        .Create()
        .ComputeHash(buffer = System.Text.Encoding.UTF8.GetBytes(s = s)))
    .Replace("-", System.String.Empty)
    .ToLower()

let factorial n = 
  let rec fact acc = function | 0 -> acc | i -> fact (acc * i) (i - 1)
  (1,n) ||> fact

let unitTestPermutations () = 
  "FooBar" 
  |> Seq.toList
  |> fun xs -> xs |> permutations |> Seq.length,
               xs |> Seq.length   |> factorial
  |> fun (x,y) -> x = y

let unitTestMD5 () =  
  // [ mon@mbai7 tmp ] md5 -s "FooBar"
  // MD5 ("FooBar") = f32a26e2a3a8aa338cd77b6e1263c535
  "FooBar" |> md5 |> fun x -> x = "f32a26e2a3a8aa338cd77b6e1263c535"

(unitTestPermutations() && unitTestMD5()) |> function 
  | true -> () 
  | false -> failwith "Must be n! permuations per string"

let cache file =
  use reader = System.IO.File.OpenText(file)
  let rec cache' acc = function
    | true -> acc
    | false -> cache' (acc |> Set.add(reader.ReadLine())) reader.EndOfStream
  cache' Set.empty reader.EndOfStream

let root     = __SOURCE_DIRECTORY__
let wordList = System.IO.Path.Combine(root,"wordlist.txt")
let anagram  = @"poultry outwits ants"
let hash     = @"4624d200580677270a54ccff86b9610e"
let words    = anagram.Split(char " ")
let cached   = wordList |> cache

words |> Array.map(fun x  -> x  |> Seq.toList |> permutations)
      |> Array.map(fun xs -> xs |> Seq.map(fun ys -> ys |> List.map string))
      |> Array.map(fun xs -> xs |> Seq.map(fun ys -> ys |> List.reduce(+)))
      |> Array.map(fun xs -> xs |> Seq.filter(fun x -> (x,cached) ||> Set.contains))
      |> fun xs -> xs.[0],xs.[1],xs.[2]
      |> fun (xs,ys,zs) -> seqMonad{let! x = xs
                                    let! y = ys
                                    let! z = zs
                                    return (x,y,z)}
      |> Seq.map(fun (x,y,z) -> x + " " + y + " " + z)
      |> Seq.map(fun x -> x |> md5, x)
      |> Seq.filter(fun (x,y) -> x = hash)
      |> Seq.map(fun (x,y) -> y)
      |> Seq.truncate 1
      |> fun x -> x |> printfn "%A"

Code output:

type SeqMonad =
  class
    new : unit -> SeqMonad
    member Bind : m:seq<'b> * f:('b -> #seq<'d>) -> seq<'d>
    member Return : v:'a -> seq<'a>
  end
val seqMonad : SeqMonad
val permutations : ls:'a list -> seq<'a list>
val md5 : s:string -> string
val factorial : n:int -> int
val unitTestPermutations : unit -> bool
val unitTestMD5 : unit -> bool
val cache : file:string -> Set<string>
val root : string = "/Users/mon/tmp"
val wordList : string = "/Users/mon/tmp/wordlist.txt"
val anagram : string = "poultry outwits ants"
val hash : string = "4624d200580677270a54ccff86b9610e"
val words : string [] = [|"poultry"; "outwits"; "ants"|]
val cached : Set<string> =
  set
    ["a"; "a's"; "ab's"; "abaci"; "aback"; "abacus"; "abacus's"; "abacuses";
     "abaft"; ...]

Code result:

> seq []
> val it : unit = ()

References:

Code Snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
let bitlength (x:bigint) =
  let xs  = x.ToByteArray()
  let n   = xs |> Array.length
  let msb = xs.[n-1]
  let rec bitlength' a = function
    | 0uy  -> a
    | msb' -> (a+1,msb' >>> 1) ||> bitlength'
  ((n-1)*8,msb) ||> bitlength'

let split (x:bigint) m =
  let y = x >>> m
  y,(x - (y <<< m))

let karatsuba x y =
  let r = 1 <<< 10
  let leq x y = (x |> bitlength) <= y
  let rec karatsuba' = function
    | (x',y') when (x',r) ||> leq || (y',r) ||> leq -> (x' * y')
    | (x',y') ->
      let n = (x' |> bitlength, y' |> bitlength) ||> max
      let m = n >>> 1
      let h1,l1 = (x',m) ||> split
      let h2,l2 = (y',m) ||> split 
      let z0 = (l1,l2)       |> karatsuba'
      let z1 = (l1+h1,l2+h2) |> karatsuba'
      let z2 = (h1,h2)       |> karatsuba'   
      (z2 <<< (2 * m)) + ((z1 - z0 - z2) <<< m) + z0
  (x,y) |> karatsuba'

let fib n = // tail-recursive with two accs
  let rec fib' a1 a2 = function
    | 0 -> 0I
    | 1 -> a1 + a2
    | i -> fib' a2 (a1 + a2) (i - 1)
  fib' 1I 0I n

let fibfast n =
  let inline inner x y i =
    let a = x * (2I * y - x)
    let b = y * y + x * x
    match i % 2 = 0 with | true -> (a,b) | false -> (b, a+b)
  let rec fibfast' k = function
    | 0 -> k (0I,1I)
    | i -> fibfast' (fun (x,y) -> k((x,y,i) |||> inner)) (i >>> 1)
  (id,n) ||> fibfast' |> fst

let fibfastkarat n =
  let inline inner x y i =
    let a = (x,((2I,y) ||> karatsuba) - x) ||> karatsuba
    let b = ((y,y) ||> karatsuba) + ((x,x) ||> karatsuba)
    match i % 2 = 0 with | true -> (a,b) | false -> (b, a+b)
  let rec fibfastkarat' k = function
    | 0 -> k (0I,1I)
    | i -> fibfastkarat' (fun (x,y) -> k((x,y,i) |||> inner)) (i >>> 1)
  (id,n) ||> fibfastkarat' |> fst

Code output:

> val bitlength : x:bigint -> int
> val split : x:bigint -> m:int32 -> bigint * System.Numerics.BigInteger
> val karatsuba : x:bigint -> y:bigint -> System.Numerics.BigInteger
> val fib : n:int -> System.Numerics.BigInteger
> val fibfast : n:int -> System.Numerics.BigInteger
> val fibfastkarat : n:int -> System.Numerics.BigInteger

Correctness test:

1
2
3
4
5
let correctness =
  ((10. ** 6. |> int |> fib),
   (10. ** 6. |> int |> fibfast),
   (10. ** 6. |> int |> fibfastkarat))
  |> fun (x,y,z) -> x = y && x = z;;

Correctness output:

> val correctness : bool = true

Performance test:

1
2
3
4
5
6
7
8
9
let duration f =
  let t = System.Diagnostics.Stopwatch()
  t.Start()
  let x = f()
  x,t.ElapsedMilliseconds |> float

duration(fun _ -> 10. ** 6. |> int |> fib)          |> snd;;
duration(fun _ -> 10. ** 6. |> int |> fibfast)      |> snd;;
duration(fun _ -> 10. ** 6. |> int |> fibfastkarat) |> snd;;

Performance output:

> val duration : f:(unit -> 'a) -> 'a * float
> val it : float = 198480.0
> val it : float = 4623.0
> val it : float = 1082.0

References: