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:
parent
61b1013571
commit
ff7e5a7cdb
@ -144,6 +144,16 @@ func (b *Buffer) WriteBytes(bytes ...byte) (int, error) {
|
|||||||
return b.Write(bytes)
|
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.
|
// WriteString implements io.StringWriter.
|
||||||
func (b *Buffer) WriteString(s string) (int, error) {
|
func (b *Buffer) WriteString(s string) (int, error) {
|
||||||
return b.Write([]byte(s))
|
return b.Write([]byte(s))
|
||||||
|
@ -61,22 +61,21 @@ type FrameMetadata struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (f FrameMetadata) WriteTo(b *buf.Buffer) error {
|
func (f FrameMetadata) WriteTo(b *buf.Buffer) error {
|
||||||
common.Must2(b.WriteBytes(0x00, 0x00))
|
lenBytes := b.Extend(2)
|
||||||
lenBytes := b.Bytes()
|
|
||||||
|
|
||||||
len0 := b.Len()
|
len0 := b.Len()
|
||||||
if _, err := serial.WriteUint16(b, f.SessionID); err != nil {
|
sessionBytes := b.Extend(2)
|
||||||
return err
|
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 {
|
if f.SessionStatus == SessionStatusNew {
|
||||||
switch f.Target.Network {
|
switch f.Target.Network {
|
||||||
case net.Network_TCP:
|
case net.Network_TCP:
|
||||||
common.Must2(b.WriteBytes(byte(TargetNetworkTCP)))
|
common.Must(b.WriteByte(byte(TargetNetworkTCP)))
|
||||||
case net.Network_UDP:
|
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 {
|
if err := addrParser.WriteAddressPort(b, f.Target.Address, f.Target.Port); err != nil {
|
||||||
|
25
common/mux/frame_test.go
Normal file
25
common/mux/frame_test.go
Normal 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()
|
||||||
|
}
|
||||||
|
}
|
@ -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 {
|
if _, err := writer.Write([]byte{tb, byte(len(domain))}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if _, err := writer.Write([]byte(domain)); err != nil {
|
if _, err := io.WriteString(writer, domain); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
Loading…
Reference in New Issue
Block a user