Skip to content

Commit 22ab6fc

Browse files
committed
(pbm-speed-test) convert strings to bytes before test
1 parent 94d2e31 commit 22ab6fc

File tree

2 files changed

+20
-37
lines changed

2 files changed

+20
-37
lines changed

cmd/pbm-speed-test/b_test.go

Lines changed: 0 additions & 18 deletions
This file was deleted.

cmd/pbm-speed-test/speedt.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,14 @@ import (
44
"context"
55
"fmt"
66
"io"
7-
"reflect"
87
"strings"
98
"time"
10-
"unsafe"
11-
12-
"github.com/percona/percona-backup-mongodb/pbm/compress"
139

1410
"go.mongodb.org/mongo-driver/bson"
1511
"go.mongodb.org/mongo-driver/mongo"
1612

13+
"github.com/percona/percona-backup-mongodb/pbm/compress"
1714
"github.com/percona/percona-backup-mongodb/pbm/errors"
18-
1915
"github.com/percona/percona-backup-mongodb/pbm/storage"
2016
)
2117

@@ -54,20 +50,25 @@ func (b Byte) String() string {
5450
}
5551

5652
type Rand struct {
57-
size Byte
53+
size Byte
54+
dataset [][]byte
5855
}
5956

6057
func NewRand(size Byte) *Rand {
6158
r := &Rand{
62-
size: size,
59+
size: size,
60+
dataset: make([][]byte, len(dataset)),
61+
}
62+
for i, s := range dataset {
63+
r.dataset[i] = []byte(s)
6364
}
6465
return r
6566
}
6667

6768
func (r *Rand) WriteTo(w io.Writer) (int64, error) {
6869
var written int64
6970
for i := 0; written < int64(r.size); i++ {
70-
n, err := w.Write(StringToBytes(dataset[i%len(dataset)]))
71+
n, err := w.Write(r.dataset[i%len(dataset)])
7172
if err != nil {
7273
return written, err
7374
}
@@ -79,6 +80,8 @@ func (r *Rand) WriteTo(w io.Writer) (int64, error) {
7980
type Collection struct {
8081
size Byte
8182
c *mongo.Collection
83+
84+
dataset [][]byte
8285
}
8386

8487
func NewCollection(size Byte, cn *mongo.Client, namespace string) (*Collection, error) {
@@ -87,10 +90,16 @@ func NewCollection(size Byte, cn *mongo.Client, namespace string) (*Collection,
8790
return nil, errors.New("namespace should be in format `database.collection`")
8891
}
8992

90-
return &Collection{
93+
r := &Collection{
9194
size: size,
9295
c: cn.Database(ns[0]).Collection(ns[1]),
93-
}, nil
96+
97+
dataset: make([][]byte, len(dataset)),
98+
}
99+
for i, s := range dataset {
100+
r.dataset[i] = []byte(s)
101+
}
102+
return r, nil
94103
}
95104

96105
func (c *Collection) WriteTo(w io.Writer) (int64, error) {
@@ -104,7 +113,7 @@ func (c *Collection) WriteTo(w io.Writer) (int64, error) {
104113

105114
var written int64
106115
for cur.Next(ctx) {
107-
n, err := w.Write([]byte(cur.Current))
116+
n, err := w.Write(cur.Current)
108117
if err != nil {
109118
return written, errors.Wrap(err, "write")
110119
}
@@ -149,11 +158,3 @@ func doTest(
149158

150159
return r, nil
151160
}
152-
153-
// StringToBytes converts given string to the slice of bytes
154-
// without allocations
155-
func StringToBytes(s string) []byte {
156-
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
157-
bh := reflect.SliceHeader{sh.Data, sh.Len, sh.Len}
158-
return *(*[]byte)(unsafe.Pointer(&bh)) //nolint:govet
159-
}

0 commit comments

Comments
 (0)