Skip to content

Commit

Permalink
Benchmark native
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim8y committed Oct 17, 2024
1 parent fbaf9e0 commit 254c609
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 1 deletion.
68 changes: 68 additions & 0 deletions benchmarks/Neo.VM.Benchmarks/NativeContract/Benchmark.Native.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// Benchmark.Native.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using BenchmarkDotNet.Attributes;
using Neo.SmartContract;
using Neo.VM.Benchmark.OpCode;

namespace Neo.VM.Benchmark.NativeContract;

public abstract class Benchmark_Native
{
private BenchmarkEngine _engine;

Check warning on line 20 in benchmarks/Neo.VM.Benchmarks/NativeContract/Benchmark.Native.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

Non-nullable field '_engine' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

protected abstract SmartContract.Native.NativeContract Native { get; }
protected abstract string Method { get; }

[ParamsSource(nameof(Params))]
private readonly object[] _args;
protected abstract object[][] Params { get; }

[IterationSetup]
public void Setup()
{

_engine = new BenchmarkEngine();
_engine.LoadScript(AppCall());
_engine.ExecuteUntil(VM.OpCode.SYSCALL);
}

[IterationCleanup]
public void Cleanup()
{
_engine.Dispose();
}

[Benchmark]
public void Bench()
{
_engine.ExecuteNext();
}

protected byte[] AppCall()
{
var builder = new ScriptBuilder();

foreach (var o in _args)
{
builder.EmitPush(o);
}

builder.EmitPush(_args.Length);
builder.Emit(VM.OpCode.PACK);

builder.EmitPush((byte)CallFlags.None);
builder.EmitPush(Method);
builder.EmitPush(Native.Hash);
builder.EmitSysCall(ApplicationEngine.System_Contract_Call);
return builder.ToArray();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// CryptoLib.RIPEMD160.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using BenchmarkDotNet.Attributes;
using Neo.SmartContract;

namespace Neo.VM.Benchmark.NativeContract.CryptoLib;

public class CryptoLib_RIPEMD160 : Benchmark_Native
{

protected override SmartContract.Native.NativeContract Native { get; }

Check warning on line 20 in benchmarks/Neo.VM.Benchmarks/NativeContract/CryptoLib/CryptoLib.RIPEMD160.cs

View workflow job for this annotation

GitHub Actions / Test-Everything

Non-nullable property 'Native' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 20 in benchmarks/Neo.VM.Benchmarks/NativeContract/CryptoLib/CryptoLib.RIPEMD160.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Non-nullable property 'Native' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 20 in benchmarks/Neo.VM.Benchmarks/NativeContract/CryptoLib/CryptoLib.RIPEMD160.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

Non-nullable property 'Native' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
protected override string Method { get; }

Check warning on line 21 in benchmarks/Neo.VM.Benchmarks/NativeContract/CryptoLib/CryptoLib.RIPEMD160.cs

View workflow job for this annotation

GitHub Actions / Test-Everything

Non-nullable property 'Method' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 21 in benchmarks/Neo.VM.Benchmarks/NativeContract/CryptoLib/CryptoLib.RIPEMD160.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Non-nullable property 'Method' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 21 in benchmarks/Neo.VM.Benchmarks/NativeContract/CryptoLib/CryptoLib.RIPEMD160.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

Non-nullable property 'Method' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

protected override object[][] Params
{
get
{
var random = new Random();
return
[
[RandomBytes(1, random)],
[RandomBytes(10, random)],
[RandomBytes(100, random)],
[RandomBytes(1000, random)],
[RandomBytes(10000, random)],
[RandomBytes(65535, random)],
[RandomBytes(100000, random)],
[RandomBytes(500000, random)],
[RandomBytes(1000000, random)],
[RandomBytes(ushort.MaxValue * 2, random)]
];
}
}

private object RandomBytes(int length, Random random)
{
byte[] buffer = new byte[length];
random.NextBytes(buffer);
return buffer;
}

// public static byte[] RIPEMD160(byte[] data)
// {
// return data.RIPEMD160();
// }

// methodConvert.CallContractMethod(NativeContract.StdLib.Hash, "itoa", 1, true);
// Loop start. Prepare arguments and call CryptoLib's verifyWithECDsa.
// vrf.EmitPush((byte)NamedCurveHash.secp256k1Keccak256); // push Koblitz curve identifier and Keccak256 hasher.
// vrf.Emit(OpCode.LDLOC0, // load signatures.
// OpCode.LDLOC3, // load sigCnt.
// OpCode.PICKITEM, // pick signature at index sigCnt.
// OpCode.LDLOC1, // load pubs.
// OpCode.LDLOC4, // load pubCnt.
// OpCode.PICKITEM, // pick pub at index pubCnt.
// OpCode.LDLOC2, // load msg.
// OpCode.PUSH4, OpCode.PACK); // pack 4 arguments for 'verifyWithECDsa' call.
// EmitAppCallNoArgs(vrf, CryptoLib.CryptoLib.Hash, "verifyWithECDsa", CallFlags.None); // emit the call to 'verifyWithECDsa' itself.


}
3 changes: 2 additions & 1 deletion benchmarks/Neo.VM.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using Neo.VM.Benchmark;
using Neo.VM.Benchmark.NativeContract.CryptoLib;
using Neo.VM.Benchmark.OpCode;
using System.Reflection;

// Define the benchmark or execute class
var benchmarkType = typeof(OpCode_Nop);
var benchmarkType = typeof(CryptoLib_RIPEMD160);

/*
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Expand Down

0 comments on commit 254c609

Please sign in to comment.