仿真平台内核初版 -tlib库 包含<sparc arm riscv powerPC>

This commit is contained in:
liuwb
2026-02-07 20:43:43 +08:00
parent de61f9e2b0
commit b3117648be
9748 changed files with 4309137 additions and 0 deletions

View File

@@ -0,0 +1,164 @@
/*
This file is part of PacketDotNet
PacketDotNet is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PacketDotNet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with PacketDotNet. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Copyright 2010 Chris Morgan <chmorgan@gmail.com>
*/
using System;
using System.Net;
using System.IO;
using NUnit.Framework;
using MiscUtil.IO;
using MiscUtil.Conversion;
namespace Test.Performance
{
[TestFixture]
public class BitConversionPerformance
{
[Test]
public void ArrayCopyBitConverterIpAddressPerformance()
{
// store the logging value
var oldThreshold = LoggingConfiguration.GlobalLoggingLevel;
// disable logging to improve performance
LoggingConfiguration.GlobalLoggingLevel = log4net.Core.Level.Off;
byte[] bytes;
int testRuns;
int startIndex;
int expectedValue;
ByteSetupMethods.Setup(out bytes, out testRuns, out startIndex,
out expectedValue);
var startTime = DateTime.Now;
for(int i = 0; i < testRuns; i++)
{
var actualValue = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(bytes, startIndex));
// NOTE: Assert.AreEqual() significantly slows, by a factor of ~6x
// the execution of this loop, so we perform ourself and
// then call Assert.AreEqual() if the comparison fails.
// This doesn't reduce performance by a noticable amount
if(actualValue != expectedValue)
{
Assert.AreEqual(expectedValue, actualValue);
}
}
var endTime = DateTime.Now;
// restore logging
LoggingConfiguration.GlobalLoggingLevel = oldThreshold;
var rate = new Rate(startTime, endTime, testRuns, "Test runs");
Console.WriteLine(rate.ToString());
}
[Test]
public void EndianReaderWriterPerformance()
{
// store the logging value
var oldThreshold = LoggingConfiguration.GlobalLoggingLevel;
// disable logging to improve performance
LoggingConfiguration.GlobalLoggingLevel = log4net.Core.Level.Off;
byte[] bytes;
int testRuns;
int startIndex;
int expectedValue;
ByteSetupMethods.Setup(out bytes, out testRuns, out startIndex,
out expectedValue);
var memStream = new MemoryStream(bytes);
var endianReader = new EndianBinaryReader(EndianBitConverter.Big, memStream);
var startTime = DateTime.Now;
for(int i = 0; i < testRuns; i++)
{
endianReader.Seek(startIndex, SeekOrigin.Begin);
var actualValue = endianReader.ReadInt32();
// NOTE: Assert.AreEqual() significantly slows, by a factor of ~6x
// the execution of this loop, so we perform ourself and
// then call Assert.AreEqual() if the comparison fails.
// This doesn't reduce performance by a noticable amount
if(actualValue != expectedValue)
{
Assert.AreEqual(expectedValue, actualValue);
}
}
var endTime = DateTime.Now;
// restore logging
LoggingConfiguration.GlobalLoggingLevel = oldThreshold;
var rate = new Rate(startTime, endTime, testRuns, "Test runs");
Console.WriteLine(rate.ToString());
}
[Test]
public void EndianBitConverterPerformance()
{
// store the logging value
var oldThreshold = LoggingConfiguration.GlobalLoggingLevel;
// disable logging to improve performance
LoggingConfiguration.GlobalLoggingLevel = log4net.Core.Level.Off;
byte[] bytes;
int testRuns;
int startIndex;
int expectedValue;
ByteSetupMethods.Setup(out bytes, out testRuns, out startIndex,
out expectedValue);
var startTime = DateTime.Now;
for(int i = 0; i < testRuns; i++)
{
var actualValue = MiscUtil.Conversion.EndianBitConverter.Big.ToInt32(bytes, startIndex);
// NOTE: Assert.AreEqual() significantly slows, by a factor of ~6x
// the execution of this loop, so we perform ourself and
// then call Assert.AreEqual() if the comparison fails.
// This doesn't reduce performance by a noticable amount
if(actualValue != expectedValue)
{
Assert.AreEqual(expectedValue, actualValue);
}
}
var endTime = DateTime.Now;
// restore logging
LoggingConfiguration.GlobalLoggingLevel = oldThreshold;
var rate = new Rate(startTime, endTime, testRuns, "Test runs");
Console.WriteLine(rate.ToString());
}
}
}

View File

@@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.Text;
using log4net.Core;
using NUnit.Framework;
using PacketDotNet;
namespace Test.Performance
{
[TestFixture]
public class ByteCopyPerformance
{
// The number of times the test is run
int testRuns = 40000;
[Test]
public void ArrayCopyPerformance()
{
// create a realistic packet for testing
var ethernetPacket = EthernetPacket.RandomPacket();
// create the array to store the copy result
byte[] hwAddress = new byte[EthernetFields.MacAddressLength];
// store the logging value
var oldThreshold = LoggingConfiguration.GlobalLoggingLevel;
// disable logging to improve performance
LoggingConfiguration.GlobalLoggingLevel = log4net.Core.Level.Off;
// Store the time before the processing starts
var startTime = DateTime.Now;
// run the test
for (int i = 0; i < testRuns; i++)
{
Array.Copy(ethernetPacket.Bytes, EthernetFields.SourceMacPosition,
hwAddress, 0, EthernetFields.MacAddressLength);
}
// store the time after the processing is finished
var endTime = DateTime.Now;
// restore logging
LoggingConfiguration.GlobalLoggingLevel = oldThreshold;
// calculate the statistics
var rate = new Rate(startTime, endTime, testRuns, "Test runs");
// output the statistics to the console
Console.WriteLine(rate.ToString());
}
[Test]
public void BufferCopyPerformance()
{
// create a realistic packet for testing
var ethernetPacket = EthernetPacket.RandomPacket();
// create the array to store the copy result
byte[] hwAddress = new byte[EthernetFields.MacAddressLength];
// store the logging value
var oldThreshold = LoggingConfiguration.GlobalLoggingLevel;
// disable logging to improve performance
LoggingConfiguration.GlobalLoggingLevel = log4net.Core.Level.Off;
// Store the time before the processing starts
var startTime = DateTime.Now;
// run the test
for (int i = 0; i < testRuns; i++)
{
Buffer.BlockCopy(ethernetPacket.Bytes, EthernetFields.SourceMacPosition,
hwAddress, 0, EthernetFields.MacAddressLength);
}
// store the time after the processing is finished
var endTime = DateTime.Now;
// restore logging
LoggingConfiguration.GlobalLoggingLevel = oldThreshold;
// calculate the statistics
var rate = new Rate(startTime, endTime, testRuns, "Test runs");
// output the statistics to the console
Console.WriteLine(rate.ToString());
}
}
}

View File

@@ -0,0 +1,147 @@
/*
This file is part of PacketDotNet
PacketDotNet is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PacketDotNet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with PacketDotNet. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Copyright 2010 Chris Morgan <chmorgan@gmail.com>
*/
using System;
using NUnit.Framework;
using log4net.Core;
using PacketDotNet;
namespace Test.Performance
{
/// <summary>
/// Compares retrieving a byte[] from a packet that is built from contiguous memory
/// vs. one that is built from several byte[]. This evaluates the performance cost
/// of having to build a contiguous byte[] from non-continguous packets
/// </summary>
[TestFixture]
public class ByteRetrievalPerformance
{
private EthernetPacket BuildNonContiguousEthernetPacket()
{
// build an ethernet packet
var ethernetPacket = EthernetPacket.RandomPacket();
// build an ip packet
var ipPacket = IpPacket.RandomPacket(IpVersion.IPv6);
ethernetPacket.PayloadPacket = ipPacket;
return ethernetPacket;
}
[Test]
public void TestOptimalByteRetrieval()
{
var ethernetPacket = BuildNonContiguousEthernetPacket();
// now extract a contiguous series of bytes
var contiguousBytes = ethernetPacket.Bytes;
// and re-parse the packet
var contiguousEthernetPacket = new EthernetPacket(new PacketDotNet.Utils.ByteArraySegment(contiguousBytes));
// used to make sure we get the same byte[] reference returned each time
// because thats what we expect
byte[] theByteArray = null;
// store the logging value
var oldThreshold = LoggingConfiguration.GlobalLoggingLevel;
// disable logging to improve performance
LoggingConfiguration.GlobalLoggingLevel = log4net.Core.Level.Off;
// now benchmark retrieving the byte[] for several seconds
var startTime = DateTime.Now;
var endTime = startTime.Add(new TimeSpan(0, 0, 2));
int testRuns = 0;
while(DateTime.Now < endTime)
{
var theBytes = contiguousEthernetPacket.Bytes;
// make sure that we always get back the same reference
// for the byte[]
if(theByteArray == null)
{
theByteArray = theBytes;
} else
{
Assert.AreSame(theByteArray, theBytes);
}
testRuns++;
}
// update the actual end of the loop
endTime = DateTime.Now;
// restore logging
LoggingConfiguration.GlobalLoggingLevel = oldThreshold;
var rate = new Rate(startTime, endTime, testRuns, "Test runs");
Console.WriteLine(rate.ToString());
}
[Test]
public void TestSubOptimalByteRetrieval()
{
var ethernetPacket = BuildNonContiguousEthernetPacket();
byte[] lastByteArray = null;
// store the logging value
var oldThreshold = LoggingConfiguration.GlobalLoggingLevel;
// disable logging to improve performance
LoggingConfiguration.GlobalLoggingLevel = log4net.Core.Level.Off;
// now benchmark retrieving the byte[] for several seconds
var startTime = DateTime.Now;
var endTime = startTime.Add(new TimeSpan(0, 0, 2));
int testRuns = 0;
while(DateTime.Now < endTime)
{
var theBytes = ethernetPacket.Bytes;
// make sure we don't get back the same reference
if(lastByteArray == null)
{
lastByteArray = theBytes;
} else
{
Assert.AreNotSame(lastByteArray, theBytes);
lastByteArray = theBytes;
}
testRuns++;
}
// update the actual end of the loop
endTime = DateTime.Now;
// restore logging
LoggingConfiguration.GlobalLoggingLevel = oldThreshold;
var rate = new Rate(startTime, endTime, testRuns, "Test runs");
Console.WriteLine(rate.ToString());
}
}
}

View File

@@ -0,0 +1,138 @@
/*
This file is part of PacketDotNet
PacketDotNet is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PacketDotNet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with PacketDotNet. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Copyright 2012 Chris Morgan <chmorgan@gmail.com>
*/
using System;
using NUnit.Framework;
using log4net.Core;
using PacketDotNet;
namespace Test.Performance
{
/// <summary>
/// Measures the speed at which we can retrieve sub-packets either through
/// the xxx.GetEncapsulated() methods or others
/// </summary>
[TestFixture]
public class GetSubPacketPerformance
{
static UInt16 tcpSourcePort = 60;
static UInt16 tcpDestinationPort = 90;
private EthernetPacket BuildTCPPacket()
{
// build an ethernet packet
var ethernetPacket = EthernetPacket.RandomPacket();
// build an ip packet
var ipPacket = IpPacket.RandomPacket(IpVersion.IPv6);
var tcpPacket = TcpPacket.RandomPacket();
tcpPacket.SourcePort = tcpSourcePort;
tcpPacket.DestinationPort = tcpDestinationPort;
ipPacket.PayloadPacket = tcpPacket;
ethernetPacket.PayloadPacket = ipPacket;
return ethernetPacket;
}
/// <summary>
/// Tests the performance of TcpPacket.GetEncapsulated()
/// </summary>
[Test]
public void TestGetEncapsulated()
{
var ethernetPacket = BuildTCPPacket();
// store the logging value
var oldThreshold = LoggingConfiguration.GlobalLoggingLevel;
// disable logging to improve performance
LoggingConfiguration.GlobalLoggingLevel = log4net.Core.Level.Off;
var startTime = DateTime.Now;
var endTime = startTime.Add(new TimeSpan(0, 0, 15));
int testRuns = 0;
while(DateTime.Now < endTime)
{
// Disable CS0618 (use of obsolete method), because we are testing the now obsolete
// methods for performance
#pragma warning disable 0618
var tcpPacket = TcpPacket.GetEncapsulated(ethernetPacket);
#pragma warning restore 0618
Assert.IsNotNull(tcpPacket);
Assert.AreEqual(tcpPacket.SourcePort, tcpSourcePort);
Assert.AreEqual(tcpPacket.DestinationPort, tcpDestinationPort);
testRuns++;
}
// update the actual end of the loop
endTime = DateTime.Now;
// restore logging
LoggingConfiguration.GlobalLoggingLevel = oldThreshold;
var rate = new Rate(startTime, endTime, testRuns, "Test runs");
Console.WriteLine(rate.ToString());
}
/// <summary>
/// Tests the performance of Packet.Extract(type)
/// </summary>
[Test]
public void TestPacketExtract()
{
var ethernetPacket = BuildTCPPacket();
// store the logging value
var oldThreshold = LoggingConfiguration.GlobalLoggingLevel;
// disable logging to improve performance
LoggingConfiguration.GlobalLoggingLevel = log4net.Core.Level.Off;
var startTime = DateTime.Now;
var endTime = startTime.Add(new TimeSpan(0, 0, 15));
int testRuns = 0;
while(DateTime.Now < endTime)
{
var tcpPacket = (TcpPacket)ethernetPacket.Extract(typeof(TcpPacket));
Assert.IsNotNull(tcpPacket);
Assert.AreEqual(tcpPacket.SourcePort, tcpSourcePort);
Assert.AreEqual(tcpPacket.DestinationPort, tcpDestinationPort);
testRuns++;
}
// update the actual end of the loop
endTime = DateTime.Now;
// restore logging
LoggingConfiguration.GlobalLoggingLevel = oldThreshold;
var rate = new Rate(startTime, endTime, testRuns, "Test runs");
Console.WriteLine(rate.ToString());
}
}
}