1
0

FormatMetricsTest.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System;
  2. using System.Globalization;
  3. using System.Threading;
  4. using OpenHardwareMonitor.Hardware;
  5. using Xunit;
  6. namespace OhmGraphite.Test
  7. {
  8. public class FormatMetricsTest
  9. {
  10. [Fact]
  11. public void FormatGraphiteIdentifier()
  12. {
  13. var writer = new GraphiteWriter("localhost", 2003, "MY-PC", false);
  14. var epoch = new DateTimeOffset(new DateTime(2001, 1, 13), TimeSpan.Zero).ToUnixTimeSeconds();
  15. var sensor = new ReportedValue("/my/cpu/identifier/1", "voltage", 1.06f, SensorType.Voltage, "cpu", HardwareType.CPU, 1);
  16. string actual = writer.FormatGraphiteData(epoch, sensor);
  17. Assert.Equal("ohm.MY-PC.my.cpu.identifier.voltage 1.06 979344000", actual);
  18. }
  19. [Fact]
  20. public void FormatCultureInvariant()
  21. {
  22. var writer = new GraphiteWriter("localhost", 2003, "MY-PC", false);
  23. CultureInfo original = Thread.CurrentThread.CurrentCulture;
  24. try
  25. {
  26. // de-DE culture will format 1.06 as 1,06 which graphite doesn't like
  27. Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("de-DE");
  28. var epoch = new DateTimeOffset(new DateTime(2001, 1, 13), TimeSpan.Zero).ToUnixTimeSeconds();
  29. var sensor = new ReportedValue("/my/cpu/identifier/1", "voltage", 1.06f, SensorType.Voltage, "cpu", HardwareType.CPU, 1);
  30. string actual = writer.FormatGraphiteData(epoch, sensor);
  31. Assert.Equal("ohm.MY-PC.my.cpu.identifier.voltage 1.06 979344000", actual);
  32. }
  33. finally
  34. {
  35. Thread.CurrentThread.CurrentCulture = original;
  36. }
  37. }
  38. [Fact]
  39. public void FormatGraphiteTags()
  40. {
  41. var writer = new GraphiteWriter("localhost", 2003, "MY-PC", true);
  42. var epoch = new DateTimeOffset(new DateTime(2001, 1, 13), TimeSpan.Zero).ToUnixTimeSeconds();
  43. var sensor = new ReportedValue("/my/cpu/identifier/1", "voltage", 1.06f, SensorType.Voltage, "cpu", HardwareType.CPU, 1);
  44. string actual = writer.FormatGraphiteData(epoch, sensor);
  45. Assert.Equal("ohm.MY-PC.my.cpu.identifier.voltage;host=MY-PC;app=ohm;hardware=cpu;hardware_type=CPU;sensor_type=Voltage;sensor_index=1;raw_name=voltage 1.06 979344000", actual);
  46. }
  47. }
  48. }