Skip to content

Instantly share code, notes, and snippets.

@stevenguh
Created August 24, 2017 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevenguh/870e2c3fbc5af4ae44c7e45045e9b971 to your computer and use it in GitHub Desktop.
Save stevenguh/870e2c3fbc5af4ae44c7e45045e9b971 to your computer and use it in GitHub Desktop.
Benchmarks for nodatime.serialization #12 (TypeDescriptor vs ConfigureForNodaTime)
[MemoryDiagnoser]
public class ConfigureForNodaTimeBenchmarks
{
static readonly Instant _instant = Instant.FromUtc(2017, 6, 7, 0, 0);
static readonly DateTime _dateTime = new DateTime(2017, 6, 7);
static readonly JsonSerializerSettings _settings = new JsonSerializerSettings();
static ConfigureForNodaTimeBenchmarks()
{
_settings.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
}
[Benchmark(Baseline = true)]
public void SerializeDateTimeWithoutSettings()
{
JsonConvert.SerializeObject(new { tm = _dateTime });
}
[Benchmark]
public void SerializeInstantWithoutSettings()
{
JsonConvert.SerializeObject(new { tm = _instant });
}
[Benchmark]
public void SerializeDateTimeWithSettings()
{
JsonConvert.SerializeObject(new { tm = _dateTime }, _settings);
}
[Benchmark]
public void SerializeInstantWithSettings()
{
JsonConvert.SerializeObject(new { tm = _instant }, _settings);
}
}
[MemoryDiagnoser]
public class TypeDescriptorBenchmarks
{
static readonly Instant _instant = Instant.FromUtc(2017, 6, 7, 0, 0);
static readonly DateTime _dateTime = new DateTime(2017, 6, 7);
static readonly JsonSerializerSettings _settings = new JsonSerializerSettings();
static TypeDescriptorBenchmarks()
{
var provider = DateTimeZoneProviders.Tzdb;
_settings.DateParseHandling = DateParseHandling.None;
TypeDescriptor.AddAttributes(typeof(Instant), new JsonConverterAttribute(typeof(NodaPatternConverter<Instant>), InstantPattern.ExtendedIso));
TypeDescriptor.AddAttributes(typeof(Interval), new JsonConverterAttribute(typeof(NodaIntervalConverter), LocalTimePattern.ExtendedIso));
TypeDescriptor.AddAttributes(typeof(LocalDate), new JsonConverterAttribute(typeof(NodaPatternConverter<LocalDate>), LocalDatePattern.Iso, CreateIsoValidator<LocalDate>(x => x.Calendar)));
TypeDescriptor.AddAttributes(typeof(LocalDateTime), new JsonConverterAttribute(typeof(NodaPatternConverter<LocalDateTime>), LocalDateTimePattern.ExtendedIso, CreateIsoValidator<LocalDateTime>(x => x.Calendar)));
TypeDescriptor.AddAttributes(typeof(LocalTime), new JsonConverterAttribute(typeof(NodaPatternConverter<LocalTime>), LocalTimePattern.ExtendedIso));
TypeDescriptor.AddAttributes(typeof(Offset), new JsonConverterAttribute(typeof(NodaPatternConverter<Offset>), OffsetPattern.GeneralInvariant));
TypeDescriptor.AddAttributes(typeof(DateTimeZone), new JsonConverterAttribute(typeof(NodaDateTimeZoneConverter), provider));
TypeDescriptor.AddAttributes(typeof(Duration), new JsonConverterAttribute(typeof(NodaPatternConverter<Duration>), DurationPattern.CreateWithInvariantCulture("-H:mm:ss.FFFFFFFFF")));
TypeDescriptor.AddAttributes(typeof(Period), new JsonConverterAttribute(typeof(NodaPatternConverter<Period>), PeriodPattern.Roundtrip));
TypeDescriptor.AddAttributes(typeof(OffsetDateTime), new JsonConverterAttribute(typeof(NodaPatternConverter<OffsetDateTime>), OffsetDateTimePattern.Rfc3339, CreateIsoValidator<OffsetDateTime>(x => x.Calendar)));
TypeDescriptor.AddAttributes(
typeof(ZonedDateTime),
new JsonConverterAttribute(
typeof(NodaPatternConverter<OffsetDateTime>),
ZonedDateTimePattern.CreateWithInvariantCulture("uuuu'-'MM'-'dd'T'HH':'mm':'ss;FFFFFFFFFo<G> z", provider),
CreateIsoValidator<ZonedDateTime>(x => x.Calendar)));
}
[Benchmark(Baseline = true)]
public void SerializeDateTimeWithoutSettings()
{
JsonConvert.SerializeObject(new { tm = _dateTime });
}
[Benchmark]
public void SerializeInstantWithoutSettings()
{
JsonConvert.SerializeObject(new { tm = _instant });
}
[Benchmark]
public void SerializeDateTimeWithSettings()
{
JsonConvert.SerializeObject(new { tm = _dateTime }, _settings);
}
[Benchmark]
public void SerializeInstantWithSettings()
{
JsonConvert.SerializeObject(new { tm = _instant }, _settings);
}
private static Action<T> CreateIsoValidator<T>(Func<T, CalendarSystem> calendarProjection) => value =>
{
var calendar = calendarProjection(value);
// We rely on CalendarSystem.Iso being a singleton here.
if (calendar != CalendarSystem.Iso)
{
throw new ArgumentException($"Values of type {typeof(T).Name} must (currently) use the ISO calendar in order to be serialized.");
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment