Skip to content

Instantly share code, notes, and snippets.

@CRC-Mismatch
Last active May 17, 2016 21:37
Show Gist options
  • Save CRC-Mismatch/baabb07842575494b0d245ae44f7e2cc to your computer and use it in GitHub Desktop.
Save CRC-Mismatch/baabb07842575494b0d245ae44f7e2cc to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
namespace SizeTest
{
class SizeTester
{
[DllImport("kernel32", SetLastError = true)]
static extern unsafe SafeFileHandle CreateFile(
string FileName, // file name
uint DesiredAccess, // access mode
uint ShareMode, // share mode
IntPtr SecurityAttributes, // Security Attr
uint CreationDisposition, // how to create
uint FlagsAndAttributes, // file attributes
IntPtr hTemplate // template file
);
const uint FILE_FLAG_NO_BUFFERING = 0x20000000;
static long lastsize = 0;
static long cursize = 0;
static long lasttime = 1;
static Stopwatch ss;
static void Main(string[] args)
{
SafeFileHandle handle = CreateFile(".test",
(uint)FileAccess.ReadWrite,
(uint)FileShare.None,
IntPtr.Zero,
(uint)FileMode.Create,
FILE_FLAG_NO_BUFFERING,
IntPtr.Zero);
FileStream f = new FileStream(handle, FileAccess.ReadWrite, 1, false);
long blksz = 512;
double bestspeed = 0;
byte[] bts = new byte[67108864];
for (long i = 512; i <= 67108864; i *= 2)
{
Console.Write(String.Format("\rTesting block size: {0:N0} B", i));
long sum = 0, sz = 0;
bool stop = false;
f.SetLength(0);
for (int j = 0; j < 50; j++)
{
f.Flush(true);
Stopwatch s = new Stopwatch();
s.Start();
f.Write(bts, 0, (int)i);
f.Flush(true);
s.Stop();
sum += s.ElapsedTicks;
if (s.ElapsedMilliseconds > 1000)
{
stop = true;
break;
}
}
if (stop) continue;
sz = f.Length;
if (sz * 1.0 / sum * 1.0 > bestspeed)
{
bestspeed = sz * 1.0 / sum * 1.0;
blksz = i;
}
}
f.SetLength(0);
ss = new Stopwatch();
ss.Start();
Timer t = new Timer(new TimerCallback(WriteLine), null, 500, 500);
Console.WriteLine();
Console.WriteLine(String.Format("Testing with block size of {0:N0} bytes:", blksz));
try
{
while (true)
{
f.Write(bts, 0, (int)blksz);
cursize = f.Length;
f.Flush(true);
}
}
catch { }
while (blksz > 0)
{
try
{
f.Write(bts, 0, (int)blksz);
cursize = f.Length;
f.Flush(true);
}
catch
{
blksz /= 2;
}
}
long totalsz = f.Length;
t.Change(Timeout.Infinite, Timeout.Infinite);
f.SetLength(0);
f.Close();
ss.Stop();
File.Delete(".test");
Console.WriteLine("\rTotal size is: ");
Console.WriteLine(String.Format("{0,20:N2} GiB | {1,20:N2} GB", totalsz / 1073741824.0, totalsz / 1000000000.0));
Console.WriteLine(String.Format("{0,20:N2} MiB | {1,20:N2} MB", totalsz / 1048576.0, totalsz / 1000000.0));
Console.WriteLine(String.Format("{0,20:N2} KiB | {1,20:N2} KB", totalsz / 1024.0, totalsz / 1000.0));
Console.WriteLine(String.Format("{0,20:N0} B | {0,20:N0} B", totalsz));
long bps = totalsz * 1000 / ss.ElapsedMilliseconds;
if (bps < 10000)
Console.WriteLine("Average speed: " + String.Format("{0:N2}", totalsz*1000 / ss.ElapsedMilliseconds) + " B/s");
else if (bps < 100000000)
Console.WriteLine("Average speed: " + String.Format("{0:N2}", (totalsz * 1000 / ss.ElapsedMilliseconds)/1024) + " KB/s");
else if (bps < 1000000000000)
Console.WriteLine("Average speed: " + String.Format("{0:N2}", (totalsz * 1000 / ss.ElapsedMilliseconds) / 1048576) + " MB/s");
else
Console.WriteLine("Average speed: " + String.Format("{0:N2}", (totalsz * 1000 / ss.ElapsedMilliseconds) / 1073741824) + " MB/s");
Console.WriteLine("Total time taken: " + ss.Elapsed.ToString("g"));
Console.Write("Finished...");
Console.ReadKey(true);
}
static void WriteLine(Object o)
{
long time = ss.ElapsedMilliseconds - lasttime;
long size = cursize - lastsize;
lasttime = ss.ElapsedMilliseconds;
lastsize = cursize;
if (lastsize < 10000)
Console.Write("\r" + String.Format("{0,10:N0} B at ", lastsize));
else if (lastsize < 100000000)
Console.Write("\r" + String.Format("{0,10:N2} KiB at ", lastsize / 1024.0));
else if (lastsize < 1000000000000)
Console.Write("\r" + String.Format("{0,10:N2} MiB at ", lastsize / 1048576.0));
else if (lastsize < 10000000000000000)
Console.Write("\r" + String.Format("{0,10:N2} GiB at ", lastsize / 1073741824.0));
long speed = (time != 0) ? size*1000 / time : 0;
if (speed < 10000)
Console.Write(String.Format("{0,12:N0} B/s ", speed));
else if (speed < 100000000)
Console.Write(String.Format("{0,10:N2} KiB/s ", speed / 1024.0));
else if (speed < 1000000000000)
Console.Write(String.Format("{0,10:N2} MiB/s ", speed / 1048576.0));
else if (speed < 10000000000000000)
Console.Write(String.Format("{0,10:N2} GiB/s ", speed / 1073741824.0));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment