bson_struct.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright (C) MongoDB, Inc. 2017-present.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  6. package benchmark
  7. import (
  8. "context"
  9. "errors"
  10. "go.mongodb.org/mongo-driver/bson"
  11. )
  12. func BSONFlatStructDecoding(ctx context.Context, tm TimerManager, iters int) error {
  13. r, err := loadSourceRaw(getProjectRoot(), perfDataDir, bsonDataDir, flatBSONData)
  14. if err != nil {
  15. return err
  16. }
  17. tm.ResetTimer()
  18. for i := 0; i < iters; i++ {
  19. out := flatBSON{}
  20. err := bson.Unmarshal(r, &out)
  21. if err != nil {
  22. return err
  23. }
  24. }
  25. return nil
  26. }
  27. func BSONFlatStructEncoding(ctx context.Context, tm TimerManager, iters int) error {
  28. r, err := loadSourceRaw(getProjectRoot(), perfDataDir, bsonDataDir, flatBSONData)
  29. if err != nil {
  30. return err
  31. }
  32. doc := flatBSON{}
  33. err = bson.Unmarshal(r, &doc)
  34. if err != nil {
  35. return err
  36. }
  37. var buf []byte
  38. tm.ResetTimer()
  39. for i := 0; i < iters; i++ {
  40. buf, err = bson.Marshal(doc)
  41. if err != nil {
  42. return err
  43. }
  44. if len(buf) == 0 {
  45. return errors.New("encoding failed")
  46. }
  47. }
  48. return nil
  49. }
  50. func BSONFlatStructTagsEncoding(ctx context.Context, tm TimerManager, iters int) error {
  51. r, err := loadSourceRaw(getProjectRoot(), perfDataDir, bsonDataDir, flatBSONData)
  52. if err != nil {
  53. return err
  54. }
  55. doc := flatBSONTags{}
  56. err = bson.Unmarshal(r, &doc)
  57. if err != nil {
  58. return err
  59. }
  60. var buf []byte
  61. tm.ResetTimer()
  62. for i := 0; i < iters; i++ {
  63. buf, err = bson.MarshalAppend(buf[:0], doc)
  64. if err != nil {
  65. return err
  66. }
  67. if len(buf) == 0 {
  68. return errors.New("encoding failed")
  69. }
  70. }
  71. return nil
  72. }
  73. func BSONFlatStructTagsDecoding(ctx context.Context, tm TimerManager, iters int) error {
  74. r, err := loadSourceRaw(getProjectRoot(), perfDataDir, bsonDataDir, flatBSONData)
  75. if err != nil {
  76. return err
  77. }
  78. tm.ResetTimer()
  79. for i := 0; i < iters; i++ {
  80. out := flatBSONTags{}
  81. err := bson.Unmarshal(r, &out)
  82. if err != nil {
  83. return err
  84. }
  85. }
  86. return nil
  87. }