Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Order;
using System.Globalization;

namespace RestSharp.Benchmarks.Requests {
[MemoryDiagnoser, RankColumn, Orderer(SummaryOrderPolicy.FastestToSlowest)]
public partial class AddObjectToRequestParametersBenchmarks {
Data _data;

[GlobalSetup]
public void GlobalSetup() {
const string @string = "random string";
const int arraySize = 10_000;
var strings = new string[arraySize];
Array.Fill(strings, @string);
var ints = new int[arraySize];
Array.Fill(ints, int.MaxValue);

Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
var dateTime = DateTime.Parse("01/01/2013 03:03:12");

_data = new Data(@string, int.MaxValue, strings, ints, dateTime, strings);
}

[Benchmark(Baseline = true)]
public void AddObject() => new RestRequest().AddObject(_data);

[Benchmark]
public void AddObjectStatic() => new RestRequest().AddObjectStatic(_data);

}
}
9 changes: 9 additions & 0 deletions benchmarks/RestSharp.Benchmarks/Requests/Data.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace RestSharp.Benchmarks.Requests {
sealed record Data(
string String,
[property: RequestProperty(Name = "PropertyName")] int Int32,
string[] Strings,
[property: RequestProperty(Format = "00000", ArrayQueryType = RequestArrayQueryType.ArrayParameters)] int[] Ints,
[property: RequestProperty(Name = "DateTime", Format = "hh:mm tt")] object DateTime,
object StringArray);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) .NET Foundation and Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Reflection;

namespace RestSharp;

public static partial class RestRequestExtensions {
static partial class PropertyCache<T> where T : class {
sealed partial class Populator {
sealed record RequestProperty {
/// <summary>
/// Gets or sets the <see cref="RequestPropertyAttribute.Name"/> associated
/// with the property this object represents
/// </summary>
internal string Name { get; init; }
/// <summary>
/// Gets the <see cref="RequestPropertyAttribute.Format"/> associated with
/// the property this object represents
/// </summary>
internal string? Format { get; }
/// <summary>
/// Gets the <see cref="RequestPropertyAttribute.ArrayQueryType"/> associated
/// with the property this object represents
/// </summary>
internal RequestArrayQueryType ArrayQueryType { get; }
/// <summary>
/// Gets the return type of the property this object represents
/// </summary>
internal Type Type { get; }

private RequestProperty(string name, string? format, RequestArrayQueryType arrayQueryType, Type type) {
Name = name;
Format = format;
ArrayQueryType = arrayQueryType;
Type = type;
}

/// <summary>
/// Creates a new request property representation of the provided property
/// </summary>
/// <param name="property">The property to turn into a request property</param>
/// <returns></returns>
internal static RequestProperty From(PropertyInfo property) {
var requestPropertyAttribute =
property.GetCustomAttribute<RequestPropertyAttribute>() ??
new RequestPropertyAttribute();

var propertyName = requestPropertyAttribute.Name ?? property.Name;

return new RequestProperty(
propertyName,
requestPropertyAttribute.Format,
requestPropertyAttribute.ArrayQueryType,
property.PropertyType);
}
}

}
}
}
Loading