-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Benchmark structure for UInt classes #553
Merged
Merged
Changes from 16 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
029d709
basic benchmark structure for UInt classes
igormcoelho 58f6ffa
commented code2 from lights for now
igormcoelho 4849d08
updated tests. all seem correct now
igormcoelho 33dc851
Switch to using a benchmark method taking a method delegate to benchm…
93643f6
Make pass.
cb1f6da
1 million iterations.
7906691
Switch to ulong for the 4th option, and it is still the same speed.
d0377a4
fix test data for 50% equal data
lightszero e855535
make test pass
lightszero e2e506b
neo.UnitTests/UT_UIntBenchmarks.cs
igormcoelho ef72646
neo.UnitTests/UT_UIntBenchmarks.cs
igormcoelho f57f400
Base 20 - UInt160 tests
vncoelho 3ac663a
neo.UnitTests/UT_UIntBenchmarks.cs
igormcoelho 93d4eba
inlined 160
igormcoelho 4622e77
complete tests with UInt256 and UInt160
igormcoelho 03a9b47
neo.UnitTests/UT_UIntBenchmarks.cs
igormcoelho 02feace
Merge branch 'master' into benchmarks_uint
vncoelho c0456d9
Lights division calculation
igormcoelho e8f5449
Merge branch 'master' into benchmarks_uint
vncoelho 7145f57
Merge branch 'master' into benchmarks_uint
igormcoelho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,333 @@ | ||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Neo.Cryptography.ECC; | ||
using Neo.IO; | ||
using Neo.Ledger; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
using System.Diagnostics; | ||
using System; | ||
//using System.Runtime.CompilerServices.Unsafe; | ||
|
||
namespace Neo.UnitTests | ||
{ | ||
[TestClass] | ||
public class UT_UIntBenchmarks | ||
{ | ||
int MAX_TESTS = 1000000; // 1 million | ||
|
||
byte[][] base_32_1; | ||
byte[][] base_32_2; | ||
byte[][] base_20_1; | ||
byte[][] base_20_2; | ||
|
||
private Random random; | ||
|
||
[TestInitialize] | ||
public void TestSetup() | ||
{ | ||
int SEED = 123456789; | ||
random = new Random(SEED); | ||
|
||
base_32_1 = new byte[MAX_TESTS][]; | ||
base_32_2 = new byte[MAX_TESTS][]; | ||
base_20_1 = new byte[MAX_TESTS][]; | ||
base_20_2 = new byte[MAX_TESTS][]; | ||
|
||
for(var i=0; i<MAX_TESTS; i++) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @igormcoelho did you calculate the amount of RAM used for this test? is not excessive? |
||
{ | ||
base_32_1[i] = RandomBytes(32); | ||
base_20_1[i] = RandomBytes(20); | ||
if (i % 2 == 0) | ||
{ | ||
base_32_2[i] = RandomBytes(32); | ||
base_20_2[i] = RandomBytes(20); | ||
} | ||
else | ||
{ | ||
base_32_2[i] = new byte[32]; | ||
Buffer.BlockCopy(base_32_1[i], 0, base_32_2[i], 0, 32); | ||
base_20_2[i] = new byte[20]; | ||
Buffer.BlockCopy(base_20_1[i], 0, base_20_2[i], 0, 20); | ||
} | ||
} | ||
} | ||
|
||
private byte[] RandomBytes(int count) | ||
{ | ||
byte[] randomBytes = new byte[count]; | ||
random.NextBytes(randomBytes); | ||
return randomBytes; | ||
} | ||
|
||
public delegate Object BenchmarkMethod(); | ||
|
||
public (TimeSpan, Object) Benchmark(BenchmarkMethod method) | ||
{ | ||
Stopwatch sw0 = new Stopwatch(); | ||
sw0.Start(); | ||
var result = method(); | ||
sw0.Stop(); | ||
TimeSpan elapsed = sw0.Elapsed; | ||
Console.WriteLine($"Elapsed={elapsed} Sum={result}"); | ||
return (elapsed, result); | ||
} | ||
|
||
/* Could do this also so just pass the method to benchmark, but overhead of delegate call might affect benchmark | ||
public delegate int ComparisonMethod(byte[] b1, byte[] b2); | ||
|
||
public int BechmarkComparisonMethod(ComparisonMethod compareMethod) | ||
{ | ||
} | ||
*/ | ||
|
||
[TestMethod] | ||
public void Benchmark_CompareTo_UInt256() | ||
{ | ||
// testing "official UInt256 version" | ||
UInt256[] uut_32_1 = new UInt256[MAX_TESTS]; | ||
UInt256[] uut_32_2 = new UInt256[MAX_TESTS]; | ||
|
||
for(var i=0; i<MAX_TESTS; i++) | ||
{ | ||
uut_32_1[i] = new UInt256(base_32_1[i]); | ||
uut_32_2[i] = new UInt256(base_32_2[i]); | ||
} | ||
|
||
var checksum0 = Benchmark(() => | ||
jsolman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
var checksum = 0; | ||
for(var i=0; i<MAX_TESTS; i++) | ||
{ | ||
checksum += uut_32_1[i].CompareTo(uut_32_2[i]); | ||
} | ||
|
||
return checksum; | ||
}).Item2; | ||
|
||
var checksum1 = Benchmark(() => | ||
{ | ||
var checksum = 0; | ||
for(var i=0; i<MAX_TESTS; i++) | ||
{ | ||
checksum += code1_UInt256CompareTo(base_32_1[i], base_32_2[i]); | ||
} | ||
|
||
return checksum; | ||
}).Item2; | ||
|
||
var checksum2 = Benchmark(() => | ||
{ | ||
var checksum = 0; | ||
for(var i=0; i<MAX_TESTS; i++) | ||
{ | ||
checksum += code2_UInt256CompareTo(base_32_1[i], base_32_2[i]); | ||
} | ||
|
||
return checksum; | ||
}).Item2; | ||
|
||
var checksum3 = Benchmark(() => | ||
{ | ||
var checksum = 0; | ||
for(var i=0; i<MAX_TESTS; i++) | ||
{ | ||
checksum += code3_UInt256CompareTo(base_32_1[i], base_32_2[i]); | ||
} | ||
|
||
return checksum; | ||
}).Item2; | ||
|
||
checksum0.Should().Be(checksum1); | ||
checksum0.Should().Be(checksum2); | ||
checksum0.Should().Be(checksum3); | ||
} | ||
|
||
[TestMethod] | ||
public void Benchmark_CompareTo_UInt160() | ||
{ | ||
// testing "official UInt160 version" | ||
UInt160[] uut_20_1 = new UInt160[MAX_TESTS]; | ||
UInt160[] uut_20_2 = new UInt160[MAX_TESTS]; | ||
|
||
for(var i=0; i<MAX_TESTS; i++) | ||
{ | ||
uut_20_1[i] = new UInt160(base_20_1[i]); | ||
uut_20_2[i] = new UInt160(base_20_2[i]); | ||
} | ||
|
||
var checksum0 = Benchmark(() => | ||
{ | ||
var checksum = 0; | ||
for(var i=0; i<MAX_TESTS; i++) | ||
{ | ||
checksum += uut_20_1[i].CompareTo(uut_20_2[i]); | ||
} | ||
|
||
return checksum; | ||
}).Item2; | ||
|
||
var checksum1 = Benchmark(() => | ||
{ | ||
var checksum = 0; | ||
for(var i=0; i<MAX_TESTS; i++) | ||
{ | ||
checksum += code1_UInt160CompareTo(base_20_1[i], base_20_2[i]); | ||
} | ||
|
||
return checksum; | ||
}).Item2; | ||
|
||
var checksum2 = Benchmark(() => | ||
{ | ||
var checksum = 0; | ||
for(var i=0; i<MAX_TESTS; i++) | ||
{ | ||
checksum += code2_UInt160CompareTo(base_20_1[i], base_20_2[i]); | ||
} | ||
|
||
return checksum; | ||
}).Item2; | ||
|
||
var checksum3 = Benchmark(() => | ||
{ | ||
var checksum = 0; | ||
for(var i=0; i<MAX_TESTS; i++) | ||
{ | ||
checksum += code3_UInt160CompareTo(base_20_1[i], base_20_2[i]); | ||
} | ||
|
||
return checksum; | ||
}).Item2; | ||
|
||
checksum0.Should().Be(checksum1); | ||
checksum0.Should().Be(checksum2); | ||
checksum0.Should().Be(checksum3); | ||
} | ||
|
||
[TestMethod] | ||
public void Benchmark_UInt_IsCorrect_Self_CompareTo() | ||
{ | ||
for(var i=0; i<MAX_TESTS; i++) | ||
{ | ||
code1_UInt160CompareTo(base_20_1[i], base_20_1[i]).Should().Be(0); | ||
code2_UInt160CompareTo(base_20_1[i], base_20_1[i]).Should().Be(0); | ||
code3_UInt160CompareTo(base_20_1[i], base_20_1[i]).Should().Be(0); | ||
code1_UInt256CompareTo(base_32_1[i], base_32_1[i]).Should().Be(0); | ||
code2_UInt256CompareTo(base_32_1[i], base_32_1[i]).Should().Be(0); | ||
code3_UInt256CompareTo(base_32_1[i], base_32_1[i]).Should().Be(0); | ||
} | ||
} | ||
|
||
private int code1_UInt256CompareTo(byte[] b1, byte[] b2) | ||
{ | ||
byte[] x = b1; | ||
byte[] y = b2; | ||
for (int i = x.Length - 1; i >= 0; i--) | ||
{ | ||
if (x[i] > y[i]) | ||
return 1; | ||
if (x[i] < y[i]) | ||
return -1; | ||
} | ||
return 0; | ||
} | ||
|
||
private unsafe int code2_UInt256CompareTo(byte[] b1, byte[] b2) | ||
{ | ||
fixed (byte* px = b1, py = b2) | ||
{ | ||
uint* lpx = (uint*)px; | ||
uint* lpy = (uint*)py; | ||
for (int i = 7; i >= 0; i--) | ||
{ | ||
if (lpx[i] > lpy[i]) | ||
return 1; | ||
if (lpx[i] < lpy[i]) | ||
return -1; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
private unsafe int code3_UInt256CompareTo(byte[] b1, byte[] b2) | ||
{ | ||
fixed (byte* px = b1, py = b2) | ||
{ | ||
ulong* lpx = (ulong*)px; | ||
ulong* lpy = (ulong*)py; | ||
for (int i = 3; i >= 0; i--) | ||
{ | ||
if (lpx[i] > lpy[i]) | ||
return 1; | ||
if (lpx[i] < lpy[i]) | ||
return -1; | ||
} | ||
} | ||
return 0; | ||
} | ||
private int code1_UInt160CompareTo(byte[] b1, byte[] b2) | ||
{ | ||
byte[] x = b1; | ||
byte[] y = b2; | ||
for (int i = x.Length - 1; i >= 0; i--) | ||
{ | ||
if (x[i] > y[i]) | ||
return 1; | ||
if (x[i] < y[i]) | ||
return -1; | ||
} | ||
return 0; | ||
} | ||
|
||
private unsafe int code2_UInt160CompareTo(byte[] b1, byte[] b2) | ||
{ | ||
fixed (byte* px = b1, py = b2) | ||
{ | ||
uint* lpx = (uint*)px; | ||
uint* lpy = (uint*)py; | ||
for (int i = 4; i >= 0; i--) | ||
{ | ||
if (lpx[i] > lpy[i]) | ||
return 1; | ||
if (lpx[i] < lpy[i]) | ||
return -1; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
private unsafe int code3_UInt160CompareTo(byte[] b1, byte[] b2) | ||
{ | ||
// LSB -----------------> MSB | ||
// -------------------------- | ||
// | 8B | 8B | 4B | | ||
// -------------------------- | ||
// 0l 1l 4i | ||
// -------------------------- | ||
fixed (byte* px = b1, py = b2) | ||
{ | ||
uint* ipx = (uint*)px; | ||
uint* ipy = (uint*)py; | ||
if (ipx[4] > ipy[4]) | ||
return 1; | ||
if (ipx[4] < ipy[4]) | ||
return -1; | ||
|
||
ulong* lpx = (ulong*)px; | ||
ulong* lpy = (ulong*)py; | ||
if (lpx[1] > lpy[1]) | ||
return 1; | ||
if (lpx[1] < lpy[1]) | ||
return -1; | ||
if (lpx[0] > lpy[0]) | ||
return 1; | ||
if (lpx[0] < lpy[0]) | ||
return -1; | ||
} | ||
return 0; | ||
} | ||
|
||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Travis should not run this normally. Benchmarks should not be enabled normally in unit tests that run after each commit; there is no benefit also because the test doesn’t fail if the benchmark were to take longer than a threshold.