Skip to content

Commit 69a2fcf

Browse files
committed
test(bench): improve server and client
1 parent 8acb361 commit 69a2fcf

File tree

2 files changed

+57
-33
lines changed

2 files changed

+57
-33
lines changed

internal/cmd/ch-bench-numbers/main.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"runtime/trace"
1010
"time"
1111

12+
"github.com/dustin/go-humanize"
1213
"github.com/go-faster/errors"
1314
"go.uber.org/multierr"
1415

@@ -25,7 +26,7 @@ func run(ctx context.Context) (re error) {
2526
BlockSize int
2627
Address string
2728
}
28-
flag.IntVar(&arg.Count, "n", 20, "count")
29+
flag.IntVar(&arg.Count, "n", 1, "count")
2930
flag.IntVar(&arg.Numbers, "numbers", 500_000_000, "numbers count")
3031
flag.IntVar(&arg.BlockSize, "block-size", 65_536, "maximum row count in block")
3132
flag.StringVar(&arg.Profile, "profile", "", "cpu profile")
@@ -87,6 +88,12 @@ func run(ctx context.Context) (re error) {
8788
}); err != nil {
8889
return errors.Wrap(err, "query")
8990
}
91+
duration := time.Since(start)
92+
gotBytes := uint64(arg.Numbers * 8)
93+
fmt.Println(duration.Round(time.Millisecond), arg.Numbers, "rows",
94+
humanize.Bytes(gotBytes),
95+
humanize.Bytes(uint64(float64(gotBytes)/duration.Seconds()))+"/s",
96+
)
9097
fmt.Println(time.Since(start))
9198
}
9299

internal/cmd/ch-bench-server/main.go

+49-32
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ import (
99

1010
"github.com/dustin/go-humanize"
1111
"github.com/go-faster/errors"
12+
"golang.org/x/sync/errgroup"
1213

1314
"github.com/go-faster/ch/proto"
1415
)
1516

16-
func run(_ context.Context) (re error) {
17+
func run(ctx context.Context) error {
1718
ln, err := net.Listen("tcp4", "127.0.0.1:9001")
1819
if err != nil {
1920
return errors.Wrap(err, "listen")
@@ -54,45 +55,61 @@ func run(_ context.Context) (re error) {
5455

5556
fmt.Println("starting", "with chunk of", humanize.Bytes(uint64(len(raw))))
5657

57-
for {
58-
conn, err := ln.Accept()
59-
if err != nil {
60-
return errors.Wrap(err, "accept")
61-
}
62-
58+
process := func(conn net.Conn) error {
6359
go func() {
60+
// Drain input.
6461
_, _ = io.Copy(io.Discard, conn)
6562
}()
66-
go func() {
67-
defer func() { _ = conn.Close() }()
68-
b := new(proto.Buffer)
69-
b.EncodeAware(&proto.ServerHello{
70-
Name: "ch-bench-server",
71-
Major: 0,
72-
Minor: 12,
73-
Revision: proto.Version,
74-
Timezone: "UTC",
75-
DisplayName: "Bench",
76-
Patch: 1,
77-
}, proto.Version)
78-
if _, err := conn.Write(b.Buf); err != nil {
79-
return
80-
}
63+
defer func() { _ = conn.Close() }()
64+
b := new(proto.Buffer)
65+
b.EncodeAware(&proto.ServerHello{
66+
Name: "ch-bench-server",
67+
Major: 0,
68+
Minor: 12,
69+
Revision: proto.Version,
70+
Timezone: "UTC",
71+
DisplayName: "Bench",
72+
Patch: 1,
73+
}, proto.Version)
74+
if _, err := conn.Write(b.Buf); err != nil {
75+
return errors.Wrap(err, "write server hello")
76+
}
8177

82-
for i := 0; i < chunks; i++ {
83-
if _, err := conn.Write(raw); err != nil {
84-
return
85-
}
78+
for i := 0; i < chunks; i++ {
79+
if _, err := conn.Write(raw); err != nil {
80+
return errors.Wrap(err, "write chunk")
8681
}
82+
}
83+
84+
// End of data.
85+
b.Reset()
86+
proto.ServerCodeEndOfStream.Encode(b)
87+
if _, err := conn.Write(b.Buf); err != nil {
88+
return errors.Wrap(err, "write end of stream")
89+
}
90+
91+
return nil
92+
}
8793

88-
// End of data.
89-
b.Reset()
90-
proto.ServerCodeEndOfStream.Encode(b)
91-
if _, err := conn.Write(b.Buf); err != nil {
92-
return
94+
g, ctx := errgroup.WithContext(ctx)
95+
for i := 0; i < 10; i++ {
96+
g.Go(func() error {
97+
for {
98+
if ctx.Err() != nil {
99+
return ctx.Err()
100+
}
101+
conn, err := ln.Accept()
102+
if err != nil {
103+
return errors.Wrap(err, "accept")
104+
}
105+
if err := process(conn); err != nil {
106+
return errors.Wrap(err, "process")
107+
}
93108
}
94-
}()
109+
})
95110
}
111+
112+
return g.Wait()
96113
}
97114

98115
func main() {

0 commit comments

Comments
 (0)