1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-12 07:19:01 -05:00

benchmark mux frame

This commit is contained in:
Darien Raymond 2018-11-14 22:11:05 +01:00
parent 61b1013571
commit ff7e5a7cdb
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
4 changed files with 43 additions and 9 deletions

View File

@ -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))

View File

@ -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 {

25
common/mux/frame_test.go Normal file
View File

@ -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()
}
}

View File

@ -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: