/* ========================================================================
* Copyright (c) 2005-2025 The OPC Foundation, Inc. All rights reserved.
*
* OPC Foundation MIT License 1.00
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* The complete license agreement can be found here:
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/
using System.IO;
using System.Text;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Diagnosers;
using NUnit.Framework;
using Opc.Ua.Bindings;
namespace Opc.Ua.Core.Encoders.Tests
{
[TestFixture]
[Category("JsonEncoder")]
[SetCulture("en-us")]
[SetUICulture("en-us")]
[NonParallelizable]
[MemoryDiagnoser]
[DisassemblyDiagnoser]
public class JsonEncoderBenchmarks : EncoderBenchmarks
{
[Params(128, 512, 1024, 4096, 8192, 65536)]
public int StreamSize { get; set; } = 1024;
///
/// Benchmark overhead to create StreamWriter, MemoryStream is kept open.
///
[Test]
public void StreamWriter()
{
using var memoryStream = new MemoryStream();
using var test = new StreamWriter(memoryStream, Encoding.UTF8, StreamSize, true);
test.Flush();
}
///
/// Benchmark overhead to create StreamWriter and MemoryStream.
///
[Test]
public void StreamWriterMemoryStream()
{
using var memoryStream = new MemoryStream();
using var test = new StreamWriter(memoryStream, Encoding.UTF8, StreamSize);
test.Flush();
}
///
/// Benchmark encoding with internal memory stream.
///
[Test]
public void JsonEncoderConstructor()
{
using var jsonEncoder = new JsonEncoder(m_context, JsonEncoderOptions.Compact);
TestEncoding(jsonEncoder);
_ = jsonEncoder.CloseAndReturnText();
}
///
/// Test encoding with ArrayPool memory stream.
///
[Theory]
public void JsonEncoderArraySegmentStreamTest(bool toText)
{
using var memoryStream = new ArraySegmentStream(
m_bufferManager,
StreamBufferSize,
0,
StreamBufferSize);
TestStreamEncode(memoryStream, toText);
}
///
/// Test encoding with memory stream kept open.
///
[Theory]
public void JsonEncoderMemoryStreamTest(bool toText)
{
using var memoryStream = new MemoryStream(StreamBufferSize);
TestStreamEncode(memoryStream, toText);
}
///
/// Benchmark encoding with memory stream kept open.
///
[Benchmark]
[Test]
public void JsonEncoderMemoryStream()
{
using var memoryStream = new MemoryStream(StreamBufferSize);
JsonEncoder_StreamLeaveOpen(memoryStream);
// get buffer for write
_ = memoryStream.ToArray();
}
///
/// Benchmark encoding with recyclable memory stream kept open.
///
[Benchmark]
[Test]
public void JsonEncoderArraySegmentStream()
{
using var arraySegmentStream = new ArraySegmentStream(m_bufferManager);
JsonEncoder_StreamLeaveOpen(arraySegmentStream);
// get buffers and return them to buffer manager
foreach (System.ArraySegment buffer in arraySegmentStream.GetBuffers("writer"))
{
m_bufferManager.ReturnBuffer(buffer.Array, "testreturn");
}
}
///
/// Benchmark encoding with recyclable memory stream kept open.
///
[Benchmark]
[Test]
public void JsonEncoderArraySegmentStreamNoSpan()
{
#if NET6_0_OR_GREATER
using var arraySegmentStream = new ArraySegmentStreamNoSpan(m_bufferManager);
#else
using var arraySegmentStream = new ArraySegmentStream(m_bufferManager);
#endif
JsonEncoder_StreamLeaveOpen(arraySegmentStream);
// get buffers and return them to buffer manager
foreach (System.ArraySegment buffer in arraySegmentStream.GetBuffers("writer"))
{
m_bufferManager.ReturnBuffer(buffer.Array, "testreturn");
}
}
private void TestStreamEncode(MemoryStream memoryStream, bool toArray)
{
int length1 = 0;
using (var jsonEncoder = new JsonEncoder(memoryStream, m_context, JsonEncoderOptions.Compact))
{
TestEncoding(jsonEncoder);
length1 = jsonEncoder.Close();
}
using (var jsonEncoder = new JsonEncoder(memoryStream, m_context, JsonEncoderOptions.Compact))
{
TestEncoding(jsonEncoder);
if (toArray)
{
int length2 = jsonEncoder.Close();
Assert.That(memoryStream.Position, Is.EqualTo(length2 + length1));
byte[] result = memoryStream.ToArray();
Assert.That(result, Is.Not.Null);
Assert.That(result, Has.Length.EqualTo(length2 + length1));
}
else
{
string result = jsonEncoder.CloseAndReturnText();
Assert.That(result, Is.Not.Null);
}
}
}
private void JsonEncoder_StreamLeaveOpen(MemoryStream stream, bool testResult = false)
{
int length1;
int length2;
stream.Position = 0;
using (var jsonEncoder = new JsonEncoder(
stream,
m_context,
JsonEncoderOptions.Compact))
{
TestEncoding(jsonEncoder);
length1 = jsonEncoder.Close();
}
using (var jsonEncoder = new JsonEncoder(
stream,
m_context,
JsonEncoderOptions.Compact))
{
TestEncoding(jsonEncoder);
length2 = jsonEncoder.Close();
}
if (testResult)
{
string result = Encoding.UTF8.GetString(stream.ToArray());
Assert.That(result, Is.Not.Null);
Assert.That(length2, Is.EqualTo(length1 * 2));
Assert.That(result, Has.Length.EqualTo(length2));
}
}
[OneTimeSetUp]
public override void OneTimeSetUp()
{
base.OneTimeSetUp();
}
[OneTimeTearDown]
public override void OneTimeTearDown()
{
base.OneTimeTearDown();
}
///
/// Set up some variables for benchmarks.
///
[GlobalSetup]
public override void GlobalSetup()
{
base.GlobalSetup();
}
///
/// Tear down benchmark variables.
///
[GlobalCleanup]
public override void GlobalCleanup()
{
base.GlobalCleanup();
}
}
}