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