You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
3.7 KiB

  1. using BenchmarkDotNet.Attributes;
  2. using BenchmarkDotNet.Configs;
  3. using BenchmarkDotNet.Environments;
  4. using BenchmarkDotNet.Jobs;
  5. using BenchmarkDotNet.Toolchains.CsProj;
  6. using JT809.Protocol.Enums;
  7. using JT809.Protocol.MessageBody;
  8. using JT809.Protocol.SubMessageBody;
  9. using JT809.Protocol.Extensions;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Text;
  13. namespace JT809.Protocol.Benchmark
  14. {
  15. [Config(typeof(JT809SerializerContextConfig))]
  16. [MarkdownExporter]
  17. [MemoryDiagnoser]
  18. public class JT809SerializerContext
  19. {
  20. private byte[] bytes;
  21. private JT809Serializer JT809Serializer;
  22. [Params(100, 10000,100000)]
  23. public int N;
  24. [GlobalSetup]
  25. public void Setup()
  26. {
  27. JT809Serializer = new JT809Serializer();
  28. bytes = "5B 00 00 00 92 00 00 06 82 94 00 01 33 EF B8 01 00 00 00 00 00 27 0F D4 C1 41 31 32 33 34 35 00 00 00 00 00 00 00 00 00 00 00 00 00 02 94 01 00 00 00 5C 01 00 02 00 00 00 00 5A 01 AC 3F 40 12 3F FA A1 00 00 00 00 5A 01 AC 4D 50 03 73 6D 61 6C 6C 63 68 69 00 00 00 00 00 00 00 00 31 32 33 34 35 36 37 38 39 30 31 00 00 00 00 00 00 00 00 00 31 32 33 34 35 36 40 71 71 2E 63 6F 6D 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 BA D8 5D".ToHexBytes();
  29. }
  30. [Benchmark(Description = "JT809_0x9400_0x9401_Package_Deserialize")]
  31. public void JT809_0x9400_0x9401_Package_Deserialize_Test()
  32. {
  33. for (int i = 0; i < N; i++)
  34. {
  35. var result = JT809Serializer.Deserialize(bytes);
  36. }
  37. }
  38. [Benchmark(Description = "JT809_0x9400_0x9401_Package_Serialize")]
  39. public void JT809_0x9400_0x9401_Package_Serialize_Test()
  40. {
  41. for (int i = 0; i < N; i++)
  42. {
  43. JT809Package jT809Package = new JT809Package();
  44. jT809Package.Header = new JT809Header
  45. {
  46. MsgSN = 1666,
  47. EncryptKey = 9999,
  48. EncryptFlag = JT809Header_Encrypt.None,
  49. Version = new JT809Header_Version(1, 0, 0),
  50. BusinessType = (ushort)JT809BusinessType.从链路报警信息交互消息,
  51. MsgGNSSCENTERID = 20180920,
  52. };
  53. JT809_0x9400 bodies = new JT809_0x9400
  54. {
  55. VehicleNo = "粤A12345",
  56. VehicleColor = JT809VehicleColorType.黄色,
  57. SubBusinessType = JT809SubBusinessType.报警督办请求消息.ToUInt16Value(),
  58. };
  59. JT809_0x9400_0x9401 jT809_0x9400_0x9401 = new JT809_0x9400_0x9401
  60. {
  61. WarnSrc = JT809WarnSrc.车载终端,
  62. WarnType = JT809WarnType.疲劳驾驶报警.ToUInt16Value(),
  63. WarnTime = DateTime.Parse("2018-09-27 10:24:00"),
  64. SupervisionID = "123FFAA1",
  65. SupervisionEndTime = DateTime.Parse("2018-09-27 11:24:00"),
  66. SupervisionLevel = JT809_9401_SupervisionLevel.一般,
  67. Supervisor = "smallchi",
  68. SupervisorTel = "12345678901",
  69. SupervisorEmail = "123456@qq.com"
  70. };
  71. bodies.SubBodies = jT809_0x9400_0x9401;
  72. jT809Package.Bodies = bodies;
  73. var hex = JT809Serializer.Serialize(jT809Package);
  74. }
  75. }
  76. }
  77. public class JT809SerializerContextConfig : ManualConfig
  78. {
  79. public JT809SerializerContextConfig()
  80. {
  81. AddJob(Job.Default.WithGcServer(false).WithToolchain(CsProjCoreToolchain.NetCoreApp50).WithPlatform(Platform.AnyCpu));
  82. }
  83. }
  84. }