Exemple #1
0
        public void BenchmarkICaptureDevice()
        {
            int packetsRead = 0;
            var startTime = DateTime.Now;
            while(packetsRead < packetsToRead)
            {
                ICaptureDevice captureDevice = new SharpPcap.LibPcap.CaptureFileReaderDevice("../../capture_files/10k_packets.pcap");
                captureDevice.Open();

                RawCapture rawCapture = null;
                do
                {
                    rawCapture = captureDevice.GetNextPacket();
                    packetsRead++;
                }
                while(rawCapture != null);

                captureDevice.Close();
            }

            var endTime = DateTime.Now;

            var rate = new Rate(startTime, endTime, packetsRead, "packets captured");

            Console.WriteLine("{0}", rate.ToString());
        }
Exemple #2
0
        public void Benchmark()
        {
            int packetsRead = 0;
            var startTime = DateTime.Now;
            while(packetsRead < packetsToRead)
            {
                var captureDevice = new SharpPcap.LibPcap.CaptureFileReaderDevice("../../capture_files/10k_packets.pcap");
                captureDevice.Open();

                RawCapture rawCapture = null;
                do
                {
                    rawCapture = captureDevice.GetNextPacket();

                    // Parse the packet using PacketDotNet
                    if(rawCapture != null)
                        Packet.ParsePacket(rawCapture.LinkLayerType, rawCapture.Data);

                    packetsRead++;
                }
                while(rawCapture != null);

                captureDevice.Close();
            }

            var endTime = DateTime.Now;

            var rate = new Rate(startTime, endTime, packetsRead, "packets parsed");

            Console.WriteLine("{0}", rate.ToString());
        }
        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());
        }
        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());
        }
Exemple #5
0
        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());
        }
        public void EndianBitConverterPerformance()
        {
            // store the logging value
            var oldThreshold = LoggingConfiguration.GlobalLoggingLevel;

            // disable logging to improve performance
            LoggingConfiguration.GlobalLoggingLevel = Level.Off;

            ByteSetupMethods.Setup(out var bytes,
                                   out var testRuns,
                                   out var startIndex,
                                   out var expectedValue);

            var startTime = DateTime.Now;

            for (var i = 0; i < testRuns; i++)
            {
                var actualValue = 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());
        }
        public void BufferCopyPerformance()
        {
            // create a realistic packet for testing
            var ethernetPacket = EthernetPacket.RandomPacket();
            // create the array to store the copy result
            var hwAddress = new byte[EthernetFields.MacAddressLength];

            // store the logging value
            var oldThreshold = LoggingConfiguration.GlobalLoggingLevel;

            // disable logging to improve performance
            LoggingConfiguration.GlobalLoggingLevel = Level.Off;

            // Store the time before the processing starts
            var startTime = DateTime.Now;

            // run the test
            for (var 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());
        }
Exemple #8
0
        public unsafe void BenchmarkICaptureDeviceUnsafe()
        {
            int packetsRead = 0;
            var startTime   = DateTime.Now;

            while (packetsRead < packetsToRead)
            {
                using var captureDevice = new CaptureFileReaderDevice(TestHelper.GetFile("10k_packets.pcap"));
                captureDevice.Open();

                RawCapture rawCapture = null;
                do
                {
                    rawCapture = captureDevice.GetNextPacket();
                    packetsRead++;
                }while (rawCapture != null);
            }

            var endTime = DateTime.Now;

            var rate = new Rate(startTime, endTime, packetsRead, "packets captured");

            Console.WriteLine("{0}", rate.ToString());
        }
        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());
        }
        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());
        }
        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());
        }
        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());
        }
        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());
        }