Skip to content

Instantly share code, notes, and snippets.

@stuarthillary
Forked from abdullin/LICENSE
Created October 22, 2019 13:43
Show Gist options
  • Select an option

  • Save stuarthillary/f3e03cfd321d41b91d42656d575ed46a to your computer and use it in GitHub Desktop.

Select an option

Save stuarthillary/f3e03cfd321d41b91d42656d575ed46a to your computer and use it in GitHub Desktop.

Revisions

  1. @abdullin abdullin revised this gist Jul 26, 2018. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion about.md
    Original file line number Diff line number Diff line change
    @@ -14,4 +14,9 @@ This naive implementation is a start for building up a deterministic simulation
    # Questions
    * How would you implement a ring benchmark in golang?
    * This .NET Core implementation is way faster than Erlang. What does Erlang spend time on?
    * Why is this implementation faster than FoundationDB Flow? What extra work could the Flow do?
    * Why is this implementation faster than FoundationDB Flow? What extra work could the Flow do?


    # I want to learn more! What is next?

    Next, we'll plug into .NET Core async/await to simulate processes running in parallel. [Check it out](https://github.com/abdullin/simasync).
  2. @abdullin abdullin revised this gist Jul 26, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions LICENSE
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    MIT License 2018 Rinat Abdullin
  3. @abdullin abdullin revised this gist Feb 14, 2018. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions about.md
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,9 @@ Given `N=1000` and `M=1000`, the result on my machine is:
    00:00:00.0260680
    ```

    Any real-world implementation would be engineered to handle various edge cases, inherently slowing things down. Erlang is a nice example of that.
    This naive implementation is a start for building up a deterministic simulation of a cluster in a world full of failures. Another building block is the time simulation: https://github.com/abdullin/simcpu

    This naive implementation is a start for building up a deterministic simulation of a cluster in a world full of failures. Another building block is the time simulation: https://github.com/abdullin/simcpu
    # Questions
    * How would you implement a ring benchmark in golang?
    * This .NET Core implementation is way faster than Erlang. What does Erlang spend time on?
    * Why is this implementation faster than FoundationDB Flow? What extra work could the Flow do?
  4. @abdullin abdullin revised this gist Feb 13, 2018. No changes.
  5. @abdullin abdullin revised this gist Feb 13, 2018. 2 changed files with 14 additions and 2 deletions.
    14 changes: 14 additions & 0 deletions about.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    This is a naive (see for yourself!) implementation that explores the idea of simulating a ring of `N` actors that send the messages to each over `M` times.

    Concept of the ring benchmark and simulated actors is taken from the gist by Preetam Jinka: https://gist.github.com/Preetam/98e80cd17ecb8748c72b

    Given `N=1000` and `M=1000`, the result on my machine is:

    ```
    /usr/local/share/dotnet/dotnet /Users/rinat/proj/core/SimRing/bin/Release/netcoreapp2.0/SimRing.dll
    00:00:00.0260680
    ```

    Any real-world implementation would be engineered to handle various edge cases, inherently slowing things down. Erlang is a nice example of that.

    This naive implementation is a start for building up a deterministic simulation of a cluster in a world full of failures. Another building block is the time simulation: https://github.com/abdullin/simcpu
    2 changes: 0 additions & 2 deletions output.txt
    Original file line number Diff line number Diff line change
    @@ -1,2 +0,0 @@
    /usr/local/share/dotnet/dotnet /Users/rinat/proj/core/SimRing/bin/Release/netcoreapp2.0/SimRing.dll
    00:00:00.0260680
  6. @abdullin abdullin created this gist Feb 13, 2018.
    71 changes: 71 additions & 0 deletions Program.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;

    namespace SimRing {
    class Program {
    static void Main(string[] args) {
    const int n = 1000;
    const int m = 1000;

    var actors = new List<Actor>();
    var sim = new Sim();
    for (int i = 0; i < n; i++) {
    var next = (i + 1) % n;
    actors.Add(new Actor(next, sim, m));
    }

    sim.Send(0, new {hello = "world"});

    var watch = Stopwatch.StartNew();
    while (sim.GetNextMessage(out var msg)) {
    actors[msg.Recipient].HandleMessage(msg.Body);
    }

    Console.WriteLine(watch.Elapsed);
    }
    }

    public class Actor {
    readonly int NextActor;
    int _counter;
    readonly Sim _sim;
    readonly int _m;

    public Actor(int nextActor, Sim sim, int m) {
    NextActor = nextActor;
    _sim = sim;
    _m = m;
    }

    public void HandleMessage(object message) {
    _counter++;
    if (_counter <= _m) {
    _sim.Send(NextActor, message);
    }
    }
    }

    public struct Message {
    public readonly int Recipient;
    public readonly object Body;

    public Message(int recipient, object body) {
    Recipient = recipient;
    Body = body;
    }
    }

    public sealed class Sim {
    readonly Queue<Message> _messages = new Queue<Message>();

    public void Send(int recipient, object message) {
    _messages.Enqueue(new Message(recipient, message));
    ;
    }

    public bool GetNextMessage(out Message msg) {
    return _messages.TryDequeue(out msg);
    }
    }
    }
    2 changes: 2 additions & 0 deletions output.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    /usr/local/share/dotnet/dotnet /Users/rinat/proj/core/SimRing/bin/Release/netcoreapp2.0/SimRing.dll
    00:00:00.0260680