1
0

FormatMetricsTest.cs 3.9 KB

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