Skip to content

Commit

Permalink
Speed up verification by generating comparison data in a separate thr…
Browse files Browse the repository at this point in the history
…ead while reading. Verify in about half of original time.
  • Loading branch information
Doug Krahmer committed Sep 28, 2018
1 parent bcbaef8 commit 49e1257
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
5 changes: 2 additions & 3 deletions MediaTester/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
Expand Down Expand Up @@ -32,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.3.1.0")]
[assembly: AssemblyFileVersion("0.3.1.0")]
[assembly: AssemblyVersion("0.3.2.0")]
[assembly: AssemblyFileVersion("0.3.2.0")]
59 changes: 39 additions & 20 deletions MediaTesterLib/MediaTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace MediaTesterLib
{
Expand Down Expand Up @@ -504,41 +506,58 @@ private bool VerifyTestFileDataBlock(int testFileIndex, string testFilePath, int

private bool VerifyTestFileDataBlock(FileStream fileReader, int fileIndex, int dataBlockIndex, out int bytesVerified, out int bytesFailed, out long readBytesPerSecond)
{
byte[] knownGoodDataBlock = null;
var thread = new Thread(() =>
{
knownGoodDataBlock = GenerateDataBlock(fileIndex, dataBlockIndex, DATA_BLOCK_SIZE);
})
{
IsBackground = true
};
thread.Start();

var dataBlock = ReadDataBlock(fileReader, dataBlockIndex, out readBytesPerSecond);
return VerifyDataBlock(dataBlock, fileIndex, dataBlockIndex, out bytesVerified, out bytesFailed);
while (thread.ThreadState == System.Threading.ThreadState.Running)
{
Thread.Sleep(10);
}

return VerifyDataBlock(dataBlock, fileIndex, dataBlockIndex, out bytesVerified, out bytesFailed, knownGoodDataBlock: knownGoodDataBlock);
}

private byte[] ReadDataBlock(FileStream fileReader, int dataBlockIndex, out long readBytesPerSecond)
{
int dataBlockStartIndex = dataBlockIndex * DATA_BLOCK_SIZE;
long dataBlockSize = fileReader.Length - dataBlockStartIndex;
if (dataBlockSize > DATA_BLOCK_SIZE)
dataBlockSize = DATA_BLOCK_SIZE;
int dataBlockStartIndex = dataBlockIndex * DATA_BLOCK_SIZE;
long dataBlockSize = fileReader.Length - dataBlockStartIndex;
if (dataBlockSize > DATA_BLOCK_SIZE)
dataBlockSize = DATA_BLOCK_SIZE;

var dataBlock = new byte[(int)dataBlockSize];
var dataBlock = new byte[(int)dataBlockSize];

if (fileReader.Position != dataBlockStartIndex)
fileReader.Seek(dataBlockStartIndex, SeekOrigin.Begin);
if (fileReader.Position != dataBlockStartIndex)
fileReader.Seek(dataBlockStartIndex, SeekOrigin.Begin);

var stopwatch = new Stopwatch();
stopwatch.Start();
int readBytes = fileReader.Read(dataBlock, 0, dataBlock.Length);
stopwatch.Stop();
readBytesPerSecond = (long)((double)readBytes / stopwatch.Elapsed.TotalSeconds);
var stopwatch = new Stopwatch();
stopwatch.Start();
int readBytes = fileReader.Read(dataBlock, 0, dataBlock.Length);
stopwatch.Stop();
readBytesPerSecond = (long)((double)readBytes / stopwatch.Elapsed.TotalSeconds);

if (readBytes != dataBlock.Length)
{
Array.Resize(ref dataBlock, readBytes);
}
if (readBytes != dataBlock.Length)
{
Array.Resize(ref dataBlock, readBytes);
}

return dataBlock;
return dataBlock;
}

private bool VerifyDataBlock(byte[] dataBlock, int fileIndex, int dataBlockIndex, out int bytesVerified, out int bytesFailed)
private bool VerifyDataBlock(byte[] dataBlock, int fileIndex, int dataBlockIndex, out int bytesVerified, out int bytesFailed, byte[] knownGoodDataBlock = null)
{
bytesVerified = 0;
bytesFailed = 0;
var knownGoodDataBlock = GenerateDataBlock(fileIndex, dataBlockIndex, dataBlock.Length);
if (knownGoodDataBlock == null || knownGoodDataBlock.Length != dataBlock.Length)
knownGoodDataBlock = GenerateDataBlock(fileIndex, dataBlockIndex, dataBlock.Length);

if (dataBlock.Length > knownGoodDataBlock.Length)
{
bytesFailed = dataBlock.Length;
Expand Down

0 comments on commit 49e1257

Please sign in to comment.