1 /*******************************************************************************
2 
3     Parametized test for the "one to many" scenario, when the many don't
4     communicate
5 
6 *******************************************************************************/
7 
8 module One_To_Many;
9 
10 import core.thread;
11 import std.algorithm;
12 import std.datetime.stopwatch;
13 import std.range;
14 import std.stdio;
15 import geod24.LocalRest;
16 
17 alias StringAPI = DataAPI!string;
18 alias StringNode = DataNode!string;
19 
20 alias WordAPI = DataAPI!size_t;
21 alias WordNode = DataNode!size_t;
22 
23 interface DataAPI (T)
24 {
25     /* MonoTime */ ulong receive (T);
26     T forward (T);
27 }
28 
29 final class DataNode (T) : DataAPI!T
30 {
31     override ulong receive (T value)
32     {
33         return MonoTime.currTime.ticks();
34     }
35 
36     override T forward (T value)
37     {
38         return value;
39     }
40 }
41 
42 void runTest (size_t node_count, size_t iterations)
43 {
44     // Setup
45     RemoteAPI!(StringAPI)[] nodes =
46         iota(node_count).map!(_ => RemoteAPI!StringAPI.spawn!StringNode()).array;
47     Duration[][] timings = new Duration[][node_count];
48     foreach (i; 0 .. node_count) timings[i] = new Duration[](iterations);
49     StopWatch sw = StopWatch(AutoStart.no);
50 
51     // Test
52     sw.start();
53     foreach (count; 0 .. iterations)
54     {
55         foreach (node_index, node; nodes)
56         {
57             ulong hnsecs = node.receive("Hello darkness my old friend");
58             timings[node_index][count] = MonoTime.currTime - *(cast(MonoTime*) &hnsecs);
59         }
60     }
61     sw.stop();
62 
63     // Results & teardown
64     writefln("Sending %d messages to %d nodes took took %s total",
65              iterations, node_count, sw.peek());
66     writefln("The average response time was: %s",
67             dur!"hnsecs"(cast(ulong) timings[].frontTransversal.map!(d => d.total!"hnsecs").mean()));
68     foreach (idx, node; nodes)
69         node.ctrl.shutdown();
70     thread_joinAll();
71 }