1
0

FormatMetricsTest.cs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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, "identifier", 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 FormatGraphiteWithSpecialCharacters()
  21. {
  22. var writer = new GraphiteWriter("localhost", 2003, "MY-PC", false);
  23. var epoch = new DateTimeOffset(new DateTime(2001, 1, 13), TimeSpan.Zero).ToUnixTimeSeconds();
  24. var sensor = new ReportedValue("/nic/{my-guid}/throughput/7", "Bluetooth Network Connection 2", 1.06f, SensorType.Throughput, "cpu", HardwareType.NIC, "{my-guid}", 7);
  25. string actual = writer.FormatGraphiteData(epoch, sensor);
  26. Assert.Equal("ohm.MY-PC.nic.my-guid.throughput.bluetoothnetworkconnection2 1.06 979344000", actual);
  27. }
  28. [Fact]
  29. public void FormatCultureInvariant()
  30. {
  31. var writer = new GraphiteWriter("localhost", 2003, "MY-PC", false);
  32. CultureInfo original = Thread.CurrentThread.CurrentCulture;
  33. try
  34. {
  35. // de-DE culture will format 1.06 as 1,06 which graphite doesn't like
  36. Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("de-DE");
  37. var epoch = new DateTimeOffset(new DateTime(2001, 1, 13), TimeSpan.Zero).ToUnixTimeSeconds();
  38. var sensor = new ReportedValue("/my/cpu/identifier/1", "voltage", 1.06f, SensorType.Voltage, "cpu", HardwareType.CPU, "identifier", 1);
  39. string actual = writer.FormatGraphiteData(epoch, sensor);
  40. Assert.Equal("ohm.MY-PC.my.cpu.identifier.voltage 1.06 979344000", actual);
  41. }
  42. finally
  43. {
  44. Thread.CurrentThread.CurrentCulture = original;
  45. }
  46. }
  47. [Fact]
  48. public void FormatGraphiteTags()
  49. {
  50. var writer = new GraphiteWriter("localhost", 2003, "MY-PC", true);
  51. var epoch = new DateTimeOffset(new DateTime(2001, 1, 13), TimeSpan.Zero).ToUnixTimeSeconds();
  52. var sensor = new ReportedValue("/my/cpu/identifier/1", "voltage", 1.06f, SensorType.Voltage, "cpu", HardwareType.CPU, "identifier", 1);
  53. string actual = writer.FormatGraphiteData(epoch, sensor);
  54. 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);
  55. }
  56. [Fact]
  57. public void FormatTagsCultureInvariant()
  58. {
  59. var writer = new GraphiteWriter("localhost", 2003, "MY-PC", true);
  60. CultureInfo original = Thread.CurrentThread.CurrentCulture;
  61. try
  62. {
  63. // de-DE culture will format 1.06 as 1,06 which graphite doesn't like
  64. Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("de-DE");
  65. var epoch = new DateTimeOffset(new DateTime(2001, 1, 13), TimeSpan.Zero).ToUnixTimeSeconds();
  66. var sensor = new ReportedValue("/my/cpu/identifier/1", "voltage", 1.06f, SensorType.Voltage, "cpu", HardwareType.CPU, "identifier", 1);
  67. string actual = writer.FormatGraphiteData(epoch, sensor);
  68. Assert.Contains("1.06", actual);
  69. }
  70. finally
  71. {
  72. Thread.CurrentThread.CurrentCulture = original;
  73. }
  74. }
  75. }
  76. }