public void BigEndianWritePerformanceTest()
        {
            // Arrange
            var endpoint = EndpointSource.GetNext();

            var server = new ModbusTcpServer();

            server.Start(endpoint);

            var buffer = server.GetHoldingRegisters();

            // Act
            var sw = Stopwatch.StartNew();

            for (int i = 0; i < 1_000_000; i++)
            {
                buffer.SetBigEndian(address: 1, 12334234.0);
            }

            // Assert
            var elapsed = sw.Elapsed;

            server.Stop();

            _logger.WriteLine($"Time per 1000 write operations: {elapsed.TotalMilliseconds / 1000} ms");
        }
Exemple #2
0
        static void DoServerWork(ModbusTcpServer server)
        {
            var random = new Random();

            // Option A: normal performance version, more flexibility

            /* get buffer in standard form (Span<short>) */
            var registers = server.GetHoldingRegisters();

            registers.SetLittleEndian <int>(address: 5, random.Next());

            // Option B: high performance version, less flexibility

            /* interpret buffer as array of bytes (8 bit) */
            var byte_buffer = server.GetHoldingRegisterBuffer <byte>();

            byte_buffer[20] = (byte)(random.Next() >> 24);

            /* interpret buffer as array of shorts (16 bit) */
            var short_buffer = server.GetHoldingRegisterBuffer <short>();

            short_buffer[30] = (short)(random.Next(0, 100) >> 16);

            /* interpret buffer as array of ints (32 bit) */
            var int_buffer = server.GetHoldingRegisterBuffer <int>();

            int_buffer[40] = random.Next(0, 100);
        }