EgorBot / runtime-utils

MIT License
0 stars 1 forks source link

EgorBot for EgorBo in #107044 #68

Open EgorBot opened 2 months ago

EgorBot commented 2 months ago

Processing https://github.com/dotnet/runtime/issues/107044#issuecomment-2329622530 command:

Command -amd -profiler -commit e96eaee7d7aaa589a7e50731b868322c1669c01d vs previous --envvars DOTNET_JitDisasm:WriteBasicUtf8 ```cs // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System.Buffers; using System.Text.Json.Tests; using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Running; namespace System.Text.Json.Tests { public class Perf_Basic { private ArrayBufferWriter _arrayBufferWriter; private int[] _numberArrayValues; private byte[] _extraArrayUtf8; private byte[] _firstUtf8; private byte[] _lastUtf8; private byte[] _ageUtf8; private byte[] _phoneNumbersUtf8; private byte[] _addressUtf8; private byte[] _streetUtf8; private byte[] _cityUtf8; private byte[] _zipUtf8; [Params(false)] public bool Formatted; [Params(false)] public bool SkipValidation; [Params(10)] public int DataSize; [GlobalSetup] public void Setup() { _arrayBufferWriter = new ArrayBufferWriter(); var random = new Random(42); _numberArrayValues = new int[DataSize]; for (int i = 0; i < DataSize; i++) { _numberArrayValues[i] = random.Next(-10000, 10000); } _extraArrayUtf8 = Encoding.UTF8.GetBytes("ExtraArray"); _firstUtf8 = Encoding.UTF8.GetBytes("first"); _lastUtf8 = Encoding.UTF8.GetBytes("last"); _ageUtf8 = Encoding.UTF8.GetBytes("age"); _phoneNumbersUtf8 = Encoding.UTF8.GetBytes("phoneNumbers"); _addressUtf8 = Encoding.UTF8.GetBytes("address"); _streetUtf8 = Encoding.UTF8.GetBytes("street"); _cityUtf8 = Encoding.UTF8.GetBytes("city"); _zipUtf8 = Encoding.UTF8.GetBytes("zip"); } [Benchmark] [MemoryRandomization] public void WriteBasicUtf8() { _arrayBufferWriter.Clear(); using (var json = new Utf8JsonWriter(_arrayBufferWriter, new JsonWriterOptions { Indented = Formatted, SkipValidation = SkipValidation })) { json.WriteStartObject(); json.WriteNumber(_ageUtf8, 42); json.WriteString(_firstUtf8, "John"); json.WriteString(_lastUtf8, "Smith"); json.WriteStartArray(_phoneNumbersUtf8); json.WriteStringValue("425-000-1212"); json.WriteStringValue("425-000-1213"); json.WriteEndArray(); json.WriteStartObject(_addressUtf8); json.WriteString(_streetUtf8, "1 Microsoft Way"); json.WriteString(_cityUtf8, "Redmond"); json.WriteNumber(_zipUtf8, 98052); json.WriteEndObject(); json.WriteStartArray(_extraArrayUtf8); for (int i = 0; i < DataSize; i++) { json.WriteNumberValue(_numberArrayValues[i]); } json.WriteEndArray(); json.WriteEndObject(); json.Flush(); } } } } ```

(EgorBot will reply in this issue)

EgorBot commented 2 months ago

Benchmark results on Amd

BenchmarkDotNet v0.14.0, Ubuntu 22.04.4 LTS (Jammy Jellyfish)
AMD EPYC 7763, 1 CPU, 16 logical and 8 physical cores
  Job-CBNBGQ : .NET 9.0.0 (42.42.42.42424), X64 RyuJIT AVX2
  Job-KQKJYN : .NET 9.0.0 (42.42.42.42424), X64 RyuJIT AVX2
OutlierMode=DontRemove  EnvironmentVariables=DOTNET_JitDisasm=WriteBasicUtf8  MemoryRandomization=True
Method Toolchain Formatted SkipValidation DataSize Mean Error Ratio
WriteBasicUtf8 Before False False 10 499.0 ns 3.52 ns 1.00
WriteBasicUtf8 After False False 10 448.7 ns 0.48 ns 0.90

BDN_Artifacts.zip

Flame graphs: Main vs PR 🔥 Hot asm: Main vs PR Hot functions: Main vs PR Counters: Main vs PR

For clean perf results, make sure you have just one [Benchmark] in your app.

EgorBot commented 2 months ago

cc @EgorBo (logs)