Created
March 7, 2019 02:47
-
-
Save mahmoud-samy/aa5d16b080f0a3b85371a2625234d5da to your computer and use it in GitHub Desktop.
Time based TPL
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
CPU Task 14: 0001 ms at thread: 52 | | |
CPU Task 02: 1198 ms at thread: 58 | | |
IO Task 01: 2486 ms at thread: 1 | | |
IO Task 03: 2935 ms at thread: 1 | | |
IO Task 05: 3383 ms at thread: 1 | | |
IO Task 07: 3832 ms at thread: 1 | | |
IO Task 09: 4280 ms at thread: 1 | | |
IO Task 11: 4729 ms at thread: 1 | | |
---------------- window opened ---------------- | |
01: 2486 ms | |
02: 1198 ms | |
03: 2935 ms | |
05: 3383 ms | |
07: 3832 ms | |
09: 4280 ms | |
11: 4729 ms | |
14: 0001 ms | |
---------------- window closed ---------------- | |
IO Task 13: 5177 ms at thread: 1 | | |
IO Task 15: 5626 ms at thread: 1 | | |
---------------- window opened ---------------- | |
13: 5177 ms | |
15: 5626 ms | |
---------------- window closed ---------------- | |
CPU Task 06: 12368 ms at thread: 56 | | |
CPU Task 08: 12374 ms at thread: 55 | | |
CPU Task 04: 12376 ms at thread: 51 | | |
CPU Task 12: 12389 ms at thread: 25 | | |
CPU Task 10: 12402 ms at thread: 59 | | |
---------------- window opened ---------------- | |
04: 12376 ms | |
06: 12368 ms | |
08: 12374 ms | |
10: 12402 ms | |
12: 12389 ms | |
---------------- window closed ---------------- | |
Done |
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
#r "System.Reactive" | |
using System.Reactive.Linq; | |
public static void Print(string taskType, int id, int timeMS, params object[] args) => | |
Console.WriteLine("{0} Task {1:D2}: {2:D04} ms at thread: {3} | {4}", taskType, id, timeMS, System.Threading.Thread.CurrentThread.ManagedThreadId, string.Join(",", args)); | |
public static (int id, int time) DoCpuWork(int id) | |
{ | |
Random r = new Random(id); | |
int max = (int)Math.Pow(10.0, (double)r.Next(5,10)); | |
var watch = System.Diagnostics.Stopwatch.StartNew(); | |
double x = 0; | |
for (int i=0; i<max; i++) { | |
x += i.GetHashCode()*r.NextDouble(); | |
} | |
watch.Stop(); | |
int delta = (int)watch.ElapsedMilliseconds; | |
Print("CPU", id, delta); | |
return ((int)id, delta); | |
} | |
public static Task<(int id, int time)> DoCpuWorkAsync(int id) => Task.Run(()=>DoCpuWork(id)); | |
public static async Task<(int id, int time)> DoIOWorkAsync(int id) | |
{ | |
Random r = new Random(id); | |
var delta = r.Next(10000); | |
await Task.Delay(delta); | |
Print(" IO", id, delta); | |
return ((int)id, delta); | |
} | |
public static Task<(int id, int time)> DoMixedWorkAsync(int id) | |
{ | |
return (id%2==0) ? DoCpuWorkAsync(id) : DoIOWorkAsync(id); | |
} | |
var z = Enumerable.Range(1, 15) | |
.Select(_ => Observable.FromAsync<(int id, int time)>(()=>DoMixedWorkAsync(_))) | |
.Merge<(int id, int time)>() | |
.Buffer(new TimeSpan(0, 0, 5)) | |
.Subscribe((q) => { | |
Console.WriteLine("---------------- window opened ----------------"); | |
q.OrderBy(_=>_.id).ToList().ForEach( | |
w => Console.WriteLine(w.id.ToString("D02") + ": " + w.time.ToString("D04") + " ms") | |
); | |
Console.WriteLine("---------------- window closed ----------------"); | |
}); | |
await Task.Delay(20000); // Hack to keep xamarin workbook running, until I figure out how to fix it. | |
Console.WriteLine("Done"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment