Last active
April 7, 2020 09:01
-
-
Save oleksabor/f5ad3e4e7e257ba91be2b3d056c4d33a to your computer and use it in GitHub Desktop.
WriteAsync "performance" test
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
const int MaxAttempts = 5; | |
static void Main(string[] args) | |
{ | |
TestFileWriteAsync(); | |
TestFileWrite(); | |
TestFileWriteViaThread(); | |
Console.ReadLine(); | |
} | |
private static void TestFileWrite() | |
{ | |
//Clear(); | |
Stopwatch stopWatch = new Stopwatch(); | |
stopWatch.Start(); | |
Console.WriteLine("Begin TestFileWrite"); | |
for (int i = 0; i < MaxAttempts; ++i) | |
{ | |
TestFileWriteInt(i); | |
} | |
stopWatch.Stop(); | |
TimeSpan ts = stopWatch.Elapsed; | |
Console.WriteLine("TestFileWrite took: {0}", ts); | |
} | |
private static void TestFileWriteViaThread() | |
{ | |
//Clear(); | |
Stopwatch stopWatch = new Stopwatch(); | |
stopWatch.Start(); | |
Console.WriteLine("Begin TestFileWriteViaThread"); | |
List<Thread> _threads = new List<Thread>(); | |
for (int i = 0; i < MaxAttempts; i++) | |
{ | |
var t = new Thread(_ => TestFileWriteInt((int)_, "th")); | |
t.Start(i); | |
_threads.Add(t); | |
} | |
while (_threads.Any(_ => _.IsAlive)) | |
Thread.Sleep(100); | |
stopWatch.Stop(); | |
TimeSpan ts = stopWatch.Elapsed; | |
Console.WriteLine("TestFileWriteViaThread took: {0}", ts); | |
} | |
private static void TestFileWriteInt(int index, string suffix = "") | |
{ | |
List<string> lines = GenerateLines(index); | |
var f1 = string.Format(@"C:\Projects\DelMee\file{0}{1}.txt", index, suffix); | |
var f2 = string.Format(@"c:\Projects\DelMee\file{0}_bck{1}.txt", index, suffix); | |
File.WriteAllLines(f1, lines); | |
File.WriteAllLines(f2, lines); | |
var text = File.ReadAllLines(f1); | |
var text1 = File.ReadAllLines(f2); | |
} | |
private static void TestFileWriteAsync() | |
{ | |
//Clear(); | |
Console.WriteLine("Begin TestFileWriteAsync "); | |
Stopwatch stopWatch = new Stopwatch(); | |
stopWatch.Start(); | |
for (int i = 0; i < MaxAttempts; i++) | |
{ | |
List<string> lines = GenerateLines(i); | |
var allTasks = new[] | |
{ File.WriteAllLinesAsync(string.Format(@"C:\Projects\DelMee\file{0}as.txt", i), lines), | |
File.WriteAllLinesAsync(string.Format(@"c:\Projects\DelMee\file{0}_bckas.txt", i), lines) | |
}; | |
Task.WaitAll(allTasks); | |
var allTasks1 = new[] | |
{ File.ReadAllLinesAsync(string.Format(@"C:\Projects\DelMee\file{0}as.txt", i)), | |
File.ReadAllLinesAsync(string.Format(@"C:\Projects\DelMee\file{0}_bckas.txt", i)) | |
}; | |
Task.WaitAll(allTasks1); | |
} | |
stopWatch.Stop(); | |
TimeSpan ts = stopWatch.Elapsed; | |
Console.WriteLine("TestFileWriteAsync took: {0}", ts); | |
} | |
//private static void Clear() | |
//{ | |
// for (int i = 0; i < MaxAttempts; ++i) | |
// { | |
// System.IO.File.Delete(string.Format(@"C:\Projects\DelMee\file{0}.txt", i)); | |
// System.IO.File.Delete(string.Format(@"c:\Projects\DelMee\file{0}_bck.txt", i)); | |
// } | |
//} | |
private static List<string> GenerateLines(int i) | |
{ | |
List<string> lines = new List<string>(); | |
for (long j = 0; j < 615000; ++j) | |
{ | |
lines.AddRange(new string[] { "First line" + i.ToString(), "Second line" + i.ToString(), "Third line" + i.ToString() }); | |
} | |
return lines; | |
} |
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
const int MaxAttempts = 5; | |
static void Main(string[] args) | |
{ | |
TestFileWriteAsync(); | |
TestFileWrite(); | |
TestFileWriteViaThread(); | |
Console.ReadLine(); | |
} | |
private static void TestFileWrite() | |
{ | |
//Clear(); | |
Stopwatch stopWatch = new Stopwatch(); | |
stopWatch.Start(); | |
Console.WriteLine("Begin TestFileWrite"); | |
for (int i = 0; i < MaxAttempts; ++i) | |
{ | |
TestFileWriteInt(i); | |
} | |
stopWatch.Stop(); | |
TimeSpan ts = stopWatch.Elapsed; | |
Console.WriteLine("TestFileWrite took: {0}", ts); | |
} | |
private static void TestFileWriteViaThread() | |
{ | |
//Clear(); | |
Stopwatch stopWatch = new Stopwatch(); | |
stopWatch.Start(); | |
Console.WriteLine("Begin TestFileWriteViaThread"); | |
List<Thread> _threads = new List<Thread>(); | |
for (int i = 0; i < MaxAttempts; i++) | |
{ | |
var t = new Thread(_ => TestFileWriteInt((int)_, "th")); | |
t.Start(i); | |
_threads.Add(t); | |
} | |
while (_threads.Any(_ => _.IsAlive)) | |
Thread.Sleep(100); | |
stopWatch.Stop(); | |
TimeSpan ts = stopWatch.Elapsed; | |
Console.WriteLine("TestFileWriteViaThread took: {0}", ts); | |
} | |
private static void TestFileWriteInt(int index, string suffix = "") | |
{ | |
List<string> lines = GenerateLines(index); | |
var f1 = string.Format(@"C:\Projects\DelMee\file{0}{1}.txt", index, suffix); | |
var f2 = string.Format(@"c:\Projects\DelMee\file{0}_bck{1}.txt", index, suffix); | |
File.WriteAllLines(f1, lines); | |
File.WriteAllLines(f2, lines); | |
var text = File.ReadAllLines(f1); | |
var text1 = File.ReadAllLines(f2); | |
} | |
private static void TestFileWriteAsync() | |
{ | |
//Clear(); | |
Console.WriteLine("Begin TestFileWriteAsync "); | |
Stopwatch stopWatch = new Stopwatch(); | |
stopWatch.Start(); | |
for (int i = 0; i < MaxAttempts; i++) | |
{ | |
List<string> lines = GenerateLines(i); | |
var allTasks = new[] | |
{ File.WriteAllLinesAsync(string.Format(@"C:\Projects\DelMee\file{0}as.txt", i), lines), | |
File.WriteAllLinesAsync(string.Format(@"c:\Projects\DelMee\file{0}_bckas.txt", i), lines) | |
}; | |
Task.WaitAll(allTasks); | |
var allTasks1 = new[] | |
{ File.ReadAllLinesAsync(string.Format(@"C:\Projects\DelMee\file{0}as.txt", i)), | |
File.ReadAllLinesAsync(string.Format(@"C:\Projects\DelMee\file{0}_bckas.txt", i)) | |
}; | |
Task.WaitAll(allTasks1); | |
} | |
stopWatch.Stop(); | |
TimeSpan ts = stopWatch.Elapsed; | |
Console.WriteLine("TestFileWriteAsync took: {0}", ts); | |
} | |
static string buffer = new string('a', 25 * 1024 * 1024); | |
private static List<string> GenerateLines(int i) | |
{ | |
return new List<string>() { buffer }; | |
} |
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
const int MaxAttempts = 5; | |
static void Main(string[] args) | |
{ | |
TestFileWriteAsync(); | |
TestFileWrite(); | |
TestFileWriteViaThread(); | |
Console.ReadLine(); | |
} | |
private static void TestFileWrite() | |
{ | |
//Clear(); | |
Stopwatch stopWatch = new Stopwatch(); | |
stopWatch.Start(); | |
Console.WriteLine("Begin TestFileWrite"); | |
for (int i = 0; i < MaxAttempts; ++i) | |
{ | |
TestFileWriteInt(i); | |
} | |
stopWatch.Stop(); | |
TimeSpan ts = stopWatch.Elapsed; | |
Console.WriteLine("TestFileWrite took: {0}", ts); | |
} | |
private static void TestFileWriteViaThread() | |
{ | |
//Clear(); | |
Stopwatch stopWatch = new Stopwatch(); | |
stopWatch.Start(); | |
Console.WriteLine("Begin TestFileWriteViaThread"); | |
List<Thread> _threads = new List<Thread>(); | |
for (int i = 0; i < MaxAttempts; i++) | |
{ | |
var t = new Thread(_ => TestFileWriteInt((int)_, "th")); | |
t.Start(i); | |
_threads.Add(t); | |
} | |
while (_threads.Any(_ => _.ThreadState == System.Threading.ThreadState.Running)) | |
Thread.Sleep(100); | |
stopWatch.Stop(); | |
TimeSpan ts = stopWatch.Elapsed; | |
Console.WriteLine("TestFileWriteViaThread took: {0}", ts); | |
} | |
private static void TestFileWriteInt(int index, string suffix = "") | |
{ | |
var lines = GenerateLines(index); | |
var f1 = string.Format(@"C:\Projects\DelMee\file{0}{1}.txt", index, suffix); | |
var f2 = string.Format(@"c:\Projects\DelMee\file{0}_bck{1}.txt", index, suffix); | |
File.WriteAllBytes(f1, lines); | |
File.WriteAllBytes(f2, lines); | |
var text = File.ReadAllBytes(f1); | |
var text1 = File.ReadAllBytes(f2); | |
} | |
private static void TestFileWriteAsync() | |
{ | |
//Clear(); | |
Console.WriteLine("Begin TestFileWriteAsync "); | |
Stopwatch stopWatch = new Stopwatch(); | |
stopWatch.Start(); | |
for (int i = 0; i < MaxAttempts; i++) | |
{ | |
var lines = GenerateLines(i); | |
var allTasks = new[] | |
{ File.WriteAllBytesAsync(string.Format(@"C:\Projects\DelMee\file{0}as.txt", i), lines), | |
File.WriteAllBytesAsync(string.Format(@"c:\Projects\DelMee\file{0}_bckas.txt", i), lines) | |
}; | |
Task.WaitAll(allTasks); | |
var allTasks1 = new[] | |
{ File.ReadAllBytesAsync(string.Format(@"C:\Projects\DelMee\file{0}as.txt", i)), | |
File.ReadAllBytesAsync(string.Format(@"C:\Projects\DelMee\file{0}_bckas.txt", i)) | |
}; | |
Task.WaitAll(allTasks1); | |
} | |
stopWatch.Stop(); | |
TimeSpan ts = stopWatch.Elapsed; | |
Console.WriteLine("TestFileWriteAsync took: {0}", ts); | |
} | |
static string buffer = new string('a', 25 * 1024 * 1024); | |
static byte[] buffer2 = new byte[25 * 1024 * 1024]; | |
private static byte[] GenerateLines(int i) | |
{ | |
return buffer2; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment