convert_bench_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package json
  15. import (
  16. "os"
  17. "testing"
  18. "github.com/lf-edge/ekuiper/pkg/ast"
  19. )
  20. func BenchmarkSimpleTuples(b *testing.B) {
  21. benchmarkByFiles("./testdata/simple.json", b, nil)
  22. }
  23. func BenchmarkSimpleTuplesWithSchema(b *testing.B) {
  24. schema := map[string]*ast.JsonStreamField{
  25. "key": {
  26. Type: "string",
  27. },
  28. }
  29. benchmarkByFiles("./testdata/simple.json", b, schema)
  30. }
  31. func BenchmarkSmallJSON(b *testing.B) {
  32. benchmarkByFiles("./testdata/small.json", b, nil)
  33. }
  34. func BenchmarkSmallJSONWithSchema(b *testing.B) {
  35. schema := map[string]*ast.JsonStreamField{
  36. "sid": {
  37. Type: "bigint",
  38. },
  39. }
  40. benchmarkByFiles("./testdata/small.json", b, schema)
  41. }
  42. func BenchmarkMediumJSON(b *testing.B) {
  43. benchmarkByFiles("./testdata/medium.json", b, nil)
  44. }
  45. func BenchmarkMediumJSONWithSchema(b *testing.B) {
  46. schema := map[string]*ast.JsonStreamField{
  47. "person": {
  48. Type: "struct",
  49. Properties: map[string]*ast.JsonStreamField{
  50. "id": {
  51. Type: "string",
  52. },
  53. },
  54. },
  55. }
  56. benchmarkByFiles("./testdata/medium.json", b, schema)
  57. }
  58. func BenchmarkLargeJSON(b *testing.B) {
  59. benchmarkByFiles("./testdata/large.json", b, nil)
  60. }
  61. func BenchmarkLargeJSONWithSchema(b *testing.B) {
  62. schema := map[string]*ast.JsonStreamField{
  63. "users": {
  64. Type: "array",
  65. Items: &ast.JsonStreamField{
  66. Type: "struct",
  67. Properties: map[string]*ast.JsonStreamField{
  68. "id": {
  69. Type: "bigint",
  70. },
  71. },
  72. },
  73. },
  74. }
  75. benchmarkByFiles("./testdata/large.json", b, schema)
  76. }
  77. func BenchmarkComplexTuples(b *testing.B) {
  78. benchmarkByFiles("./testdata/MDFD.json", b, nil)
  79. }
  80. func BenchmarkComplexTuplesWithSchema(b *testing.B) {
  81. schema := map[string]*ast.JsonStreamField{
  82. "STD_AbsoluteWindDirection": {
  83. Type: "float",
  84. },
  85. }
  86. benchmarkByFiles("./testdata/MDFD.json", b, schema)
  87. }
  88. func benchmarkByFiles(filePath string, b *testing.B, schema map[string]*ast.JsonStreamField) {
  89. payload, err := os.ReadFile(filePath)
  90. if err != nil {
  91. b.Fatalf(err.Error())
  92. }
  93. if schema != nil {
  94. f := NewFastJsonConverter(schema)
  95. b.ResetTimer()
  96. for i := 0; i < b.N; i++ {
  97. f.Decode(payload)
  98. }
  99. } else {
  100. b.ResetTimer()
  101. for i := 0; i < b.N; i++ {
  102. converter.Decode(payload)
  103. }
  104. }
  105. }