-
Notifications
You must be signed in to change notification settings - Fork 112
/
taskvent.cs
60 lines (51 loc) · 1.48 KB
/
taskvent.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using ZeroMQ;
namespace Examples
{
static partial class Program
{
public static void TaskVent(string[] args)
{
//
// Task ventilator
// Binds PUSH socket to tcp://127.0.0.1:5557
// Sends batch of tasks to workers via that socket
//
// Author: metadings
//
// Socket to send messages on and
// Socket to send start of batch message on
using (var context = new ZContext())
using (var sender = new ZSocket(context, ZSocketType.PUSH))
using (var sink = new ZSocket(context, ZSocketType.PUSH))
{
sender.Bind("tcp://*:5557");
sink.Connect("tcp://127.0.0.1:5558");
Console.WriteLine("Press ENTER when the workers are ready...");
Console.ReadKey(true);
Console.WriteLine("Sending tasks to workers...");
// The first message is "0" and signals start of batch
sink.Send(new byte[] { 0x00 }, 0, 1);
// Initialize random number generator
var rnd = new Random();
// Send 100 tasks
int i = 0;
long total_msec = 0; // Total expected cost in msecs
for (; i < 100; ++i)
{
// Random workload from 1 to 100msecs
int workload = rnd.Next(100) + 1;
total_msec += workload;
byte[] action = BitConverter.GetBytes(workload);
Console.WriteLine("{0}", workload);
sender.Send(action, 0, action.Length);
}
Console.WriteLine("Total expected cost: {0} ms", total_msec);
}
}
}
}