From ff7e5a7cdbb0b7876a6da47ad6c0dc3765b49acd Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Wed, 14 Nov 2018 22:11:05 +0100 Subject: [PATCH] benchmark mux frame --- common/buf/buffer.go | 10 ++++++++++ common/mux/frame.go | 15 +++++++-------- common/mux/frame_test.go | 25 +++++++++++++++++++++++++ common/protocol/address.go | 2 +- 4 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 common/mux/frame_test.go diff --git a/common/buf/buffer.go b/common/buf/buffer.go index 8806ac80..75d93b5a 100644 --- a/common/buf/buffer.go +++ b/common/buf/buffer.go @@ -144,6 +144,16 @@ func (b *Buffer) WriteBytes(bytes ...byte) (int, error) { return b.Write(bytes) } +// WriteByte writes a single byte into the buffer. +func (b *Buffer) WriteByte(v byte) error { + if b.IsFull() { + return newError("buffer full") + } + b.v[b.end] = v + b.end++ + return nil +} + // WriteString implements io.StringWriter. func (b *Buffer) WriteString(s string) (int, error) { return b.Write([]byte(s)) diff --git a/common/mux/frame.go b/common/mux/frame.go index 5bab6586..08812fc1 100644 --- a/common/mux/frame.go +++ b/common/mux/frame.go @@ -61,22 +61,21 @@ type FrameMetadata struct { } func (f FrameMetadata) WriteTo(b *buf.Buffer) error { - common.Must2(b.WriteBytes(0x00, 0x00)) - lenBytes := b.Bytes() + lenBytes := b.Extend(2) len0 := b.Len() - if _, err := serial.WriteUint16(b, f.SessionID); err != nil { - return err - } + sessionBytes := b.Extend(2) + binary.BigEndian.PutUint16(sessionBytes, f.SessionID) - common.Must2(b.WriteBytes(byte(f.SessionStatus), byte(f.Option))) + common.Must(b.WriteByte(byte(f.SessionStatus))) + common.Must(b.WriteByte(byte(f.Option))) if f.SessionStatus == SessionStatusNew { switch f.Target.Network { case net.Network_TCP: - common.Must2(b.WriteBytes(byte(TargetNetworkTCP))) + common.Must(b.WriteByte(byte(TargetNetworkTCP))) case net.Network_UDP: - common.Must2(b.WriteBytes(byte(TargetNetworkUDP))) + common.Must(b.WriteByte(byte(TargetNetworkUDP))) } if err := addrParser.WriteAddressPort(b, f.Target.Address, f.Target.Port); err != nil { diff --git a/common/mux/frame_test.go b/common/mux/frame_test.go new file mode 100644 index 00000000..afbb3a15 --- /dev/null +++ b/common/mux/frame_test.go @@ -0,0 +1,25 @@ +package mux_test + +import ( + "testing" + + "v2ray.com/core/common" + "v2ray.com/core/common/buf" + "v2ray.com/core/common/mux" + "v2ray.com/core/common/net" +) + +func BenchmarkFrameWrite(b *testing.B) { + frame := mux.FrameMetadata{ + Target: net.TCPDestination(net.DomainAddress("www.v2ray.com"), net.Port(80)), + SessionID: 1, + SessionStatus: mux.SessionStatusNew, + } + writer := buf.New() + defer writer.Release() + + for i := 0; i < b.N; i++ { + common.Must(frame.WriteTo(writer)) + writer.Clear() + } +} diff --git a/common/protocol/address.go b/common/protocol/address.go index 372d3702..51faf37e 100644 --- a/common/protocol/address.go +++ b/common/protocol/address.go @@ -246,7 +246,7 @@ func (p *addressParser) writeAddress(writer io.Writer, address net.Address) erro if _, err := writer.Write([]byte{tb, byte(len(domain))}); err != nil { return err } - if _, err := writer.Write([]byte(domain)); err != nil { + if _, err := io.WriteString(writer, domain); err != nil { return err } default: