This repository has been archived by the owner on Feb 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
67 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -431,3 +431,5 @@ $RECYCLE.BIN/ | |
!.vscode/tasks.json | ||
!.vscode/launch.json | ||
!.vscode/extensions.json | ||
|
||
.ionide |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.PHONY: format | ||
|
||
format: | ||
fantomas --recurse --pageWidth 80 . | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,67 @@ | ||
module Agents | ||
|
||
open System | ||
open System.Threading | ||
|
||
type Stats = | ||
{ Count: int; | ||
Sum: double; } | ||
{ Count: int | ||
Sum: double } | ||
|
||
type Msg = | ||
| Incr of double | ||
| Fetch of AsyncReplyChannel<Stats> | ||
| Die | ||
|
||
type StatsAgent() = | ||
|
||
let agent = | ||
MailboxProcessor.Start(fun inbox -> | ||
let rec loop stats = | ||
async { | ||
let! msg = inbox.Receive() | ||
match msg with | ||
| Incr(x) -> | ||
return! loop { Count = stats.Count + 1; Sum = stats.Sum + x} | ||
return! loop | ||
{ Count = stats.Count + 1 | ||
Sum = stats.Sum + x } | ||
| Fetch(replyChannel) -> | ||
replyChannel.Reply(stats) | ||
return! loop stats | ||
| Die -> return () | ||
} | ||
loop { Count = 0; Sum = 0.0 } | ||
) | ||
loop | ||
{ Count = 0 | ||
Sum = 0.0 }) | ||
|
||
interface IDisposable with | ||
member this.Dispose() = this.Die() | ||
|
||
member _.Incr(x) = agent.Post(Incr x) | ||
member _.Fetch() = agent.PostAndReply(Fetch) | ||
member _.Die() = agent.Post(Die) | ||
member _.Die() = agent.Post(Die) | ||
|
||
type NumberProducer = | ||
{ TotalNumbers: int | ||
SleepIntervalMilliseconds: int } | ||
|
||
type Poster = | ||
{ Producers: NumberProducer [] } | ||
|
||
type PosterAgent(statsAgent, poster) = | ||
let (poster: Poster) = poster | ||
let (statsAgent: StatsAgent) = statsAgent | ||
|
||
let produce (id, numberProducer) = | ||
async { | ||
for i in 1 .. numberProducer.TotalNumbers do | ||
statsAgent.Incr(double i) | ||
Thread.Sleep(numberProducer.SleepIntervalMilliseconds) | ||
printfn "Producer %d finished; Current stats: %A" id | ||
(statsAgent.Fetch()) | ||
} | ||
|
||
member _.Execute() = | ||
let producers = | ||
[ for id in 0 .. poster.Producers.Length - 1 -> | ||
produce (id, poster.Producers.[id]) ] | ||
Async.RunSynchronously(Async.Parallel producers) |> ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,28 @@ | ||
open System | ||
open Agents | ||
|
||
let measureTime f = | ||
let stopWatch = Diagnostics.Stopwatch.StartNew() | ||
f() | ||
stopWatch.Stop() | ||
stopWatch.Elapsed.TotalMilliseconds | ||
|
||
let useAgents() = | ||
use statsAgent = new StatsAgent() | ||
|
||
let producers = | ||
[| { TotalNumbers = 100 | ||
SleepIntervalMilliseconds = 50 } | ||
{ TotalNumbers = 200 | ||
SleepIntervalMilliseconds = 30 } |] | ||
|
||
let poster = { Producers = producers } | ||
|
||
let posterAgent = PosterAgent(statsAgent, poster) | ||
posterAgent.Execute() | ||
|
||
[<EntryPoint>] | ||
let main argv = | ||
use statsAgent = new StatsAgent() | ||
statsAgent.Incr(5.0) | ||
statsAgent.Incr(7.0) | ||
printfn "%A" (statsAgent.Fetch()) | ||
statsAgent.Incr(7.0) | ||
printfn "%A" (statsAgent.Fetch()) | ||
let elapsedTime = measureTime useAgents | ||
printfn "Program execution took %fms" elapsedTime | ||
0 // return an integer exit code |