2022-06-21 08:09:38 +02:00
|
|
|
using System;
|
2018-03-27 16:42:52 +02:00
|
|
|
using System.Collections.Generic;
|
2018-11-08 15:33:14 +00:00
|
|
|
using System.Linq;
|
2018-03-27 16:42:52 +02:00
|
|
|
using BenchmarkDotNet.Attributes;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Extensions;
|
2018-03-27 16:42:52 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
namespace Umbraco.Tests.Benchmarks;
|
|
|
|
|
|
|
|
|
|
[MemoryDiagnoser]
|
|
|
|
|
public class TryConvertToBenchmarks
|
2018-03-27 16:42:52 +02:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
private static readonly List<string> List = new() { "hello", "world", "awesome" };
|
|
|
|
|
private static readonly string Date = "Saturday 10, November 2012";
|
2018-03-27 16:42:52 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Benchmark(Description = "List<string> to IEnumerable<string>")]
|
|
|
|
|
public IList<string> TryConvertToEnumerable() => List.TryConvertTo<IEnumerable<string>>().Result.ToList();
|
2018-03-27 16:42:52 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Benchmark(Description = "Int to Double")]
|
|
|
|
|
public double TryConvertToDouble() => 1.TryConvertTo<double>().Result;
|
2018-03-27 16:42:52 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Benchmark(Description = "Float to Decimal")]
|
|
|
|
|
public decimal TryConvertToDecimal() => 1F.TryConvertTo<decimal>().Result;
|
2018-03-27 16:42:52 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Benchmark(Description = "String to Boolean")]
|
|
|
|
|
public bool TryConvertToBoolean() => "1".TryConvertTo<bool>().Result;
|
2018-03-27 16:42:52 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Benchmark(Description = "String to DateTime")]
|
|
|
|
|
public DateTime TryConvertToDateTime() => Date.TryConvertTo<DateTime>().Result;
|
2018-11-08 15:33:14 +00:00
|
|
|
}
|