Last active
October 23, 2019 00:08
-
-
Save lodejard/f9ce74f9b9002820917bfe412791515e to your computer and use it in GitHub Desktop.
write/read speed
This file contains hidden or 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
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Threading.Tasks; | |
namespace readmany | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
MainAsync(args).GetAwaiter().GetResult(); | |
} | |
static async Task MainAsync(string[] args) | |
{ | |
var data = new byte[1 << 20]; | |
var concurrency = 1; | |
var times = 400; | |
new Random().NextBytes(data); | |
var tasks = new List<Task<double>>(); | |
var sw = new Stopwatch(); | |
sw.Start(); | |
for (var loop = 0; loop != concurrency; ++loop) | |
{ | |
tasks.Add(DoWrites(data, times, loop)); | |
} | |
double Mbit = 0; | |
foreach (var task in tasks) | |
{ | |
Mbit += await task; | |
} | |
sw.Stop(); | |
tasks.Clear(); | |
var sw2 = new Stopwatch(); | |
sw2.Start(); | |
for (var loop = 0; loop != concurrency; ++loop) | |
{ | |
tasks.Add(DoReads(times, loop)); | |
} | |
Mbit = 0; | |
foreach (var task in tasks) | |
{ | |
Mbit += await task; | |
} | |
sw2.Stop(); | |
Console.WriteLine($"Total Write {Mbit / sw.Elapsed.TotalSeconds:N3} Mbps"); | |
Console.WriteLine($"Total Read {Mbit / sw2.Elapsed.TotalSeconds:N3} Mbps"); | |
} | |
private static async Task<double> DoWrites(byte[] data, int times, int loop) | |
{ | |
await Task.Delay(1); | |
var sw = new Stopwatch(); | |
using (var file = File.Create($@"z:\\data{loop}.dat")) | |
{ | |
sw.Start(); | |
for (var count = 0; count != times; ++count) | |
{ | |
await file.WriteAsync(data, 0, data.Length); | |
} | |
file.Close(); | |
sw.Stop(); | |
} | |
var MByte = (data.Length * times) * 1.0 / (1 << 20); | |
var Mbit = MByte * 8; | |
Console.WriteLine($"Write {Mbit / sw.Elapsed.TotalSeconds:N3} Mbps"); | |
return Mbit; | |
} | |
private static async Task<double> DoReads(int times, int loop) | |
{ | |
await Task.Delay(1); | |
var data = new byte[1 << 20]; | |
var total = 0; | |
var sw = new Stopwatch(); | |
using (var file = File.OpenRead($@"z:\\data{loop}.dat")) | |
{ | |
sw.Start(); | |
for (var count = 0; count != times; ++count) | |
{ | |
total += await file.ReadAsync(data, 0, data.Length); | |
} | |
if (total != data.Length * times) | |
{ | |
throw new Exception("Not enough data was read"); | |
} | |
file.Close(); | |
sw.Stop(); | |
} | |
var MByte = (data.Length * times) * 1.0 / (1 << 20); | |
var Mbit = MByte * 8; | |
Console.WriteLine($"Read {Mbit / sw.Elapsed.TotalSeconds:N3} Mbps"); | |
return Mbit; | |
} | |
} | |
} |
This file contains hidden or 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
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>netcoreapp3.0</TargetFramework> | |
</PropertyGroup> | |
</Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment