Skip to content

Commit

Permalink
fix: fix something
Browse files Browse the repository at this point in the history
  • Loading branch information
EngRajabi authored and raman-m committed Dec 20, 2024
1 parent 885ee5b commit bd2da05
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 161 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ public Task<Response<FileConfiguration>> Get()
jsonConfiguration = FileSys.ReadAllText(_environmentFile.FullName);
}

var fileConfiguration = JsonSerializer.Deserialize<FileConfiguration>(jsonConfiguration, JsonSerializerOptionsFactory.Web);
var fileConfiguration = JsonSerializer.Deserialize<FileConfiguration>(jsonConfiguration, JsonSerializerOptionsFactory.Web);

return Task.FromResult<Response<FileConfiguration>>(new OkResponse<FileConfiguration>(fileConfiguration));
}

public Task<Response> Set(FileConfiguration fileConfiguration)
{
var jsonConfiguration = JsonSerializer.Serialize(fileConfiguration, JsonSerializerOptionsFactory.WebWriteIndented);
public Task<Response> Set(FileConfiguration fileConfiguration)
{
var jsonConfiguration = JsonSerializer.Serialize(fileConfiguration, JsonSerializerOptionsFactory.WebWriteIndented);

lock (_lock)
{
Expand Down
18 changes: 9 additions & 9 deletions src/Ocelot/Configuration/Repository/FileConfigurationPoller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ private async Task Poll()
_logger.LogInformation("Finished polling");
}

/// <summary>
/// We could do object comparison here but performance isnt really a problem. This might be an issue one day!.
/// </summary>
/// <returns>hash of the config.</returns>
private static string ToJson(FileConfiguration config)
{
var currentHash = JsonSerializer.Serialize(config, JsonSerializerOptionsFactory.Web);
return currentHash;
}
/// <summary>
/// We could do object comparison here but performance isnt really a problem. This might be an issue one day!.
/// </summary>
/// <returns>hash of the config.</returns>
private static string ToJson(FileConfiguration config)
{
var currentHash = JsonSerializer.Serialize(config, JsonSerializerOptionsFactory.Web);
return currentHash;
}

public void Dispose()
{
Expand Down
14 changes: 7 additions & 7 deletions src/Ocelot/DependencyInjection/ConfigurationBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Memory;
using Microsoft.Extensions.Configuration.Memory;
using Ocelot.Configuration.File;
using Ocelot.Infrastructure;
using System.Text;
Expand Down Expand Up @@ -92,8 +92,8 @@ public static IConfigurationBuilder AddOcelot(this IConfigurationBuilder builder
private static IConfigurationBuilder ApplyMergeOcelotJsonOption(IConfigurationBuilder builder, MergeOcelotJson mergeTo, string json,
string primaryConfigFile, bool? optional, bool? reloadOnChange)
{
return mergeTo == MergeOcelotJson.ToMemory ?
builder.AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes(json))) :
return mergeTo == MergeOcelotJson.ToMemory ?
builder.AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes(json))) :
AddOcelotJsonFile(builder, json, primaryConfigFile, optional, reloadOnChange);
}

Expand Down Expand Up @@ -135,7 +135,7 @@ private static string GetMergedOcelotJson(string folder, IWebHostEnvironment env

var lines = File.ReadAllText(file.FullName);
var config = JsonSerializer.Deserialize<FileConfiguration>(lines, JsonSerializerOptionsFactory.Web);
if (file.Name.Equals(globalFileInfo.Name, StringComparison.OrdinalIgnoreCase) &&
if (file.Name.Equals(globalFileInfo.Name, StringComparison.OrdinalIgnoreCase) &&
file.FullName.Equals(globalFileInfo.FullName, StringComparison.OrdinalIgnoreCase))
{
fileConfiguration.GlobalConfiguration = config.GlobalConfiguration;
Expand All @@ -159,11 +159,11 @@ private static string GetMergedOcelotJson(string folder, IWebHostEnvironment env
/// <param name="optional">The 2nd argument of the AddJsonFile.</param>
/// <param name="reloadOnChange">The 3rd argument of the AddJsonFile.</param>
/// <returns>An <see cref="IConfigurationBuilder"/> object.</returns>
public static IConfigurationBuilder AddOcelot(this IConfigurationBuilder builder, FileConfiguration fileConfiguration,
public static IConfigurationBuilder AddOcelot(this IConfigurationBuilder builder, FileConfiguration fileConfiguration,
string primaryConfigFile = null, bool? optional = null, bool? reloadOnChange = null) // optional injections
{
var json = JsonSerializer.Serialize(fileConfiguration, JsonSerializerOptionsFactory.WebWriteIndented);
return AddOcelotJsonFile(builder, json, primaryConfigFile, optional, reloadOnChange);
var json = JsonSerializer.Serialize(fileConfiguration, JsonSerializerOptionsFactory.WebWriteIndented);
return AddOcelotJsonFile(builder, json, primaryConfigFile, optional, reloadOnChange);
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions src/Ocelot/DependencyInjection/OcelotBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public OcelotBuilder(IServiceCollection services, IConfiguration configurationRo
Services.TryAddSingleton<IHttpHandlerOptionsCreator, HttpHandlerOptionsCreator>();
Services.TryAddSingleton<IDownstreamAddressesCreator, DownstreamAddressesCreator>();
Services.TryAddSingleton<IDelegatingHandlerHandlerFactory, DelegatingHandlerHandlerFactory>();

Services.TryAddSingleton<IOcelotConfigurationChangeTokenSource, OcelotConfigurationChangeTokenSource>();
Services.TryAddSingleton<IOptionsMonitor<IInternalConfiguration>, OcelotConfigurationMonitor>();

Expand Down Expand Up @@ -165,10 +165,10 @@ public OcelotBuilder(IServiceCollection services, IConfiguration configurationRo
/// - <see cref="AnalysisServiceCollectionExtensions.AddMiddlewareAnalysis(IServiceCollection)"/><br/>
/// - <see cref="EncoderServiceCollectionExtensions.AddWebEncoders(IServiceCollection)"/>.
/// <para>
/// Warning! The following <see cref="IMvcCoreBuilder"/> extensions being called:<br/>
/// Warning! The following <see cref="IMvcCoreBuilder"/> extensions being called<br/>
/// - <see cref="MvcCoreMvcCoreBuilderExtensions.AddApplicationPart(IMvcCoreBuilder, Assembly)"/><br/>
/// - <see cref="MvcCoreMvcCoreBuilderExtensions.AddControllersAsServices(IMvcCoreBuilder)"/><br/>
/// - <see cref="MvcCoreMvcCoreBuilderExtensions.AddAuthorization(IMvcCoreBuilder)"/><br/>
/// - <see cref="MvcCoreMvcCoreBuilderExtensions.AddAuthorization(IMvcCoreBuilder)"/>.
/// </para>
/// </remarks>
/// <param name="builder">The default builder being returned by <see cref="MvcCoreServiceCollectionExtensions.AddMvcCore(IServiceCollection)"/> extension-method.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,28 +194,28 @@ public FakeEurekaService(string serviceId, string host, int port, bool isSecure,
public IDictionary<string, string> Metadata { get; }
}

public class Port
{
[JsonPropertyName("$")]
public int value { get; set; }
public class Port
{
[JsonPropertyName("$")]
public int value { get; set; }

[JsonPropertyName("@enabled")]
public string enabled { get; set; }
}
[JsonPropertyName("@enabled")]
public string enabled { get; set; }
}

public class SecurePort
{
[JsonPropertyName("$")]
public int value { get; set; }
public class SecurePort
{
[JsonPropertyName("$")]
public int value { get; set; }

[JsonPropertyName("@enabled")]
public string enabled { get; set; }
}
[JsonPropertyName("@enabled")]
public string enabled { get; set; }
}

public class DataCenterInfo
{
[JsonPropertyName("@class")]
public string value { get; set; }
public class DataCenterInfo
{
[JsonPropertyName("@class")]
public string value { get; set; }

public string name { get; set; }
}
Expand All @@ -235,11 +235,11 @@ public class LeaseInfo
public long serviceUpTimestamp { get; set; }
}

public class ValueMetadata
{
[JsonPropertyName("@class")]
public string value { get; set; }
}
public class ValueMetadata
{
[JsonPropertyName("@class")]
public string value { get; set; }
}

public class Instance
{
Expand Down
122 changes: 61 additions & 61 deletions test/Ocelot.Benchmarks/JsonSerializerBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,80 +3,80 @@
using Newtonsoft.Json;
using JsonSerializer = System.Text.Json.JsonSerializer;

namespace Ocelot.Benchmarks
namespace Ocelot.Benchmarks;

[SimpleJob(RuntimeMoniker.Net80)]
[Config(typeof(JsonSerializerBenchmark))]
public class JsonSerializerBenchmark : ManualConfig
{
[SimpleJob(RuntimeMoniker.Net80)]
[Config(typeof(JsonSerializerBenchmark))]
public class JsonSerializerBenchmark : ManualConfig
{
private string _serializedTestUsers;
private string _serializedTestUsers;

private List<User> _testUsers = new();
private List<User> _testUsers = new();

[Params(1000)]
public int Count { get; set; }
[Params(1000)]
public int Count { get; set; }

[GlobalSetup]
public void GlobalSetup()
[GlobalSetup]
public void GlobalSetup()
{
Faker<User> faker = new Faker<User>().CustomInstantiator(f => new User
{
Faker<User> faker = new Faker<User>().CustomInstantiator(f => new User
{
UserId = Guid.NewGuid(),
FirstName = f.Name.FirstName(),
LastName = f.Name.LastName(),
FullName = f.Name.FullName(),
Username = f.Internet.UserName(f.Name.FirstName(), f.Name.LastName()),
Email = f.Internet.Email(f.Name.FirstName(), f.Name.LastName()),
});
_testUsers = faker.Generate(Count);
_serializedTestUsers = JsonSerializer.Serialize(_testUsers);
}
UserId = Guid.NewGuid(),
FirstName = f.Name.FirstName(),
LastName = f.Name.LastName(),
FullName = f.Name.FullName(),
Username = f.Internet.UserName(f.Name.FirstName(), f.Name.LastName()),
Email = f.Internet.Email(f.Name.FirstName(), f.Name.LastName()),
});
_testUsers = faker.Generate(Count);
_serializedTestUsers = JsonSerializer.Serialize(_testUsers);
}

[Benchmark]
[BenchmarkCategory("Serialize", "Newtonsoft")]
public void NewtonsoftSerializeBigData()
{
_ = JsonConvert.SerializeObject(_testUsers);
}
[Benchmark]
[BenchmarkCategory("Serialize", "Newtonsoft")]
public void NewtonsoftSerializeBigData()
{
_ = JsonConvert.SerializeObject(_testUsers);
}

[Benchmark]
[BenchmarkCategory("Serialize", "Microsoft")]
public void MicrosoftSerializeBigData()
{
_ = JsonSerializer.Serialize(_testUsers);
}
[Benchmark]
[BenchmarkCategory("Serialize", "Microsoft")]
public void MicrosoftSerializeBigData()
{
_ = JsonSerializer.Serialize(_testUsers);
}

[Benchmark]
[BenchmarkCategory("Deserialize", "Newtonsoft")]
public void NewtonsoftDeserializeBigData()
{
_ = JsonConvert.DeserializeObject<List<User>>(_serializedTestUsers);
}
[Benchmark]
[BenchmarkCategory("Deserialize", "Newtonsoft")]
public void NewtonsoftDeserializeBigData()
{
_ = JsonConvert.DeserializeObject<List<User>>(_serializedTestUsers);
}

[Benchmark]
[BenchmarkCategory("Deserialize", "Microsoft")]
public void MicrosoftDeserializeBigData()
{
_ = JsonSerializer.Deserialize<List<User>>(_serializedTestUsers);
}
[Benchmark]
[BenchmarkCategory("Deserialize", "Microsoft")]
public void MicrosoftDeserializeBigData()
{
_ = JsonSerializer.Deserialize<List<User>>(_serializedTestUsers);
}
}

//BenchmarkDotNet v0.13.11, Windows 11 (10.0.22631.3880/23H2/2023Update/SunValley3)
//Intel Core i7-10870H CPU 2.20GHz, 1 CPU, 16 logical and 8 physical cores
// .NET SDK 8.0.303
// [Host] : .NET 6.0.32 (6.0.3224.31407), X64 RyuJIT AVX2[AttachedDebugger]
// .NET 8.0 : .NET 8.0.7 (8.0.724.31311), X64 RyuJIT AVX2
//BenchmarkDotNet v0.13.11, Windows 11 (10.0.22631.3880/23H2/2023Update/SunValley3)
//Intel Core i7-10870H CPU 2.20GHz, 1 CPU, 16 logical and 8 physical cores
// .NET SDK 8.0.303
// [Host] : .NET 6.0.32 (6.0.3224.31407), X64 RyuJIT AVX2[AttachedDebugger]
// .NET 8.0 : .NET 8.0.7 (8.0.724.31311), X64 RyuJIT AVX2

// Job =.NET 8.0 Runtime=.NET 8.0
// Job =.NET 8.0 Runtime=.NET 8.0

// | Method | Count | Mean | Error | StdDev | Median | Op/s | Gen0 | Gen1 | Gen2 | Allocated |
// |----------------------------- |------ |-----------:|---------:|----------:|-----------:|--------:|---------:|---------:|---------:|----------:|
// | MicrosoftDeserializeBigData | 1000 | 856.3 us | 53.98 us | 157.47 us | 797.1 us | 1,167.8 | 39.0625 | 13.6719 | - | 328.78 KB |
// | NewtonsoftDeserializeBigData | 1000 | 1,137.2 us | 18.74 us | 17.53 us | 1,132.8 us | 879.4 | 54.6875 | 17.5781 | - | 457.94 KB |
// |==============================================================================================================================================|
// | MicrosoftSerializeBigData | 1000 | 646.4 us | 12.72 us | 20.90 us | 645.7 us | 1,546.9 | 110.3516 | 110.3516 | 110.3516 | 350.02 KB |
// | NewtonsoftSerializeBigData | 1000 | 1,033.4 us | 19.37 us | 42.53 us | 1,022.8 us | 967.7 | 109.3750 | 109.3750 | 109.3750 | 837.82 KB |

// | Method | Count | Mean | Error | StdDev | Median | Op/s | Gen0 | Gen1 | Gen2 | Allocated |
// |----------------------------- |------ |-----------:|---------:|----------:|-----------:|--------:|---------:|---------:|---------:|----------:|
// | MicrosoftDeserializeBigData | 1000 | 856.3 us | 53.98 us | 157.47 us | 797.1 us | 1,167.8 | 39.0625 | 13.6719 | - | 328.78 KB |
// | NewtonsoftDeserializeBigData | 1000 | 1,137.2 us | 18.74 us | 17.53 us | 1,132.8 us | 879.4 | 54.6875 | 17.5781 | - | 457.94 KB |
// |==============================================================================================================================================|
// | MicrosoftSerializeBigData | 1000 | 646.4 us | 12.72 us | 20.90 us | 645.7 us | 1,546.9 | 110.3516 | 110.3516 | 110.3516 | 350.02 KB |
// | NewtonsoftSerializeBigData | 1000 | 1,033.4 us | 19.37 us | 42.53 us | 1,022.8 us | 967.7 | 109.3750 | 109.3750 | 109.3750 | 837.82 KB |
}

public class User
{
Expand Down
1 change: 0 additions & 1 deletion test/Ocelot.IntegrationTests/AdministrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Tokens;
using Ocelot.Administration;
using Ocelot.Cache;
using Ocelot.Configuration.ChangeTracking;
using Ocelot.Configuration.File;
using Ocelot.DependencyInjection;
Expand Down
Loading

0 comments on commit bd2da05

Please sign in to comment.