Last active
March 16, 2017 09:38
-
-
Save disksing/187fa1c34180eac72d076edddd3331aa to your computer and use it in GitHub Desktop.
network test.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"log" | |
"strings" | |
"sync/atomic" | |
"time" | |
"github.com/pingcap/network" | |
"golang.org/x/net/context" | |
"google.golang.org/grpc" | |
) | |
var server = flag.String("s", "127.0.0.1:3000", "server addr.") | |
var payloadSize = flag.Int("p", 100, "payload size.") | |
var thread = flag.Int("t", 1, "thread num.") | |
func main() { | |
flag.Parse() | |
var maxElapse int64 | |
for i := 0; i < *thread; i++ { | |
go func() { | |
conn, err := grpc.Dial(*server, grpc.WithInsecure()) | |
if err != nil { | |
log.Fatalf("did not connect: %v", err) | |
} | |
defer conn.Close() | |
c := network.NewNetworkTestClient(conn) | |
msg := &network.Ping{Text: strings.Repeat("A", *payloadSize)} | |
for { | |
start := time.Now() | |
_, err := c.Echo(context.Background(), msg) | |
if err != nil { | |
log.Fatal(err) | |
} | |
if elapsed := int64(time.Since(start)); elapsed > atomic.LoadInt64(&maxElapse) { | |
atomic.StoreInt64(&maxElapse, elapsed) | |
} | |
} | |
}() | |
} | |
for { | |
time.Sleep(time.Second * 10) | |
log.Println(time.Duration(atomic.LoadInt64(&maxElapse))) | |
atomic.StoreInt64(&maxElapse, 0) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"log" | |
"net" | |
"github.com/pingcap/network" | |
"golang.org/x/net/context" | |
"google.golang.org/grpc" | |
) | |
var port = flag.Int("p", 3000, "port") | |
type server struct{} | |
func (s server) Echo(ctx context.Context, in *network.Ping) (*network.Pong, error) { | |
return &network.Pong{Text: in.Text}, nil | |
} | |
func main() { | |
flag.Parse() | |
lis, err := net.Listen("tcp", fmt.Sprintf(":%v", *port)) | |
if err != nil { | |
log.Fatalf("failed to listen: %v", err) | |
} | |
s := grpc.NewServer() | |
network.RegisterNetworkTestServer(s, &server{}) | |
if err := s.Serve(lis); err != nil { | |
log.Fatalf("failed to serve: %v", err) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Code generated by protoc-gen-go. | |
// source: message.proto | |
// DO NOT EDIT! | |
/* | |
Package network is a generated protocol buffer package. | |
It is generated from these files: | |
message.proto | |
It has these top-level messages: | |
Ping | |
Pong | |
*/ | |
package network | |
import proto "github.com/golang/protobuf/proto" | |
import fmt "fmt" | |
import math "math" | |
import ( | |
context "golang.org/x/net/context" | |
grpc "google.golang.org/grpc" | |
) | |
// Reference imports to suppress errors if they are not otherwise used. | |
var _ = proto.Marshal | |
var _ = fmt.Errorf | |
var _ = math.Inf | |
// This is a compile-time assertion to ensure that this generated file | |
// is compatible with the proto package it is being compiled against. | |
// A compilation error at this line likely means your copy of the | |
// proto package needs to be updated. | |
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package | |
type Ping struct { | |
Text string `protobuf:"bytes,1,opt,name=text" json:"text,omitempty"` | |
} | |
func (m *Ping) Reset() { *m = Ping{} } | |
func (m *Ping) String() string { return proto.CompactTextString(m) } | |
func (*Ping) ProtoMessage() {} | |
func (*Ping) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } | |
func (m *Ping) GetText() string { | |
if m != nil { | |
return m.Text | |
} | |
return "" | |
} | |
type Pong struct { | |
Text string `protobuf:"bytes,2,opt,name=text" json:"text,omitempty"` | |
} | |
func (m *Pong) Reset() { *m = Pong{} } | |
func (m *Pong) String() string { return proto.CompactTextString(m) } | |
func (*Pong) ProtoMessage() {} | |
func (*Pong) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} } | |
func (m *Pong) GetText() string { | |
if m != nil { | |
return m.Text | |
} | |
return "" | |
} | |
func init() { | |
proto.RegisterType((*Ping)(nil), "network.Ping") | |
proto.RegisterType((*Pong)(nil), "network.Pong") | |
} | |
// Reference imports to suppress errors if they are not otherwise used. | |
var _ context.Context | |
var _ grpc.ClientConn | |
// This is a compile-time assertion to ensure that this generated file | |
// is compatible with the grpc package it is being compiled against. | |
const _ = grpc.SupportPackageIsVersion4 | |
// Client API for NetworkTest service | |
type NetworkTestClient interface { | |
Echo(ctx context.Context, in *Ping, opts ...grpc.CallOption) (*Pong, error) | |
} | |
type networkTestClient struct { | |
cc *grpc.ClientConn | |
} | |
func NewNetworkTestClient(cc *grpc.ClientConn) NetworkTestClient { | |
return &networkTestClient{cc} | |
} | |
func (c *networkTestClient) Echo(ctx context.Context, in *Ping, opts ...grpc.CallOption) (*Pong, error) { | |
out := new(Pong) | |
err := grpc.Invoke(ctx, "/network.NetworkTest/Echo", in, out, c.cc, opts...) | |
if err != nil { | |
return nil, err | |
} | |
return out, nil | |
} | |
// Server API for NetworkTest service | |
type NetworkTestServer interface { | |
Echo(context.Context, *Ping) (*Pong, error) | |
} | |
func RegisterNetworkTestServer(s *grpc.Server, srv NetworkTestServer) { | |
s.RegisterService(&_NetworkTest_serviceDesc, srv) | |
} | |
func _NetworkTest_Echo_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { | |
in := new(Ping) | |
if err := dec(in); err != nil { | |
return nil, err | |
} | |
if interceptor == nil { | |
return srv.(NetworkTestServer).Echo(ctx, in) | |
} | |
info := &grpc.UnaryServerInfo{ | |
Server: srv, | |
FullMethod: "/network.NetworkTest/Echo", | |
} | |
handler := func(ctx context.Context, req interface{}) (interface{}, error) { | |
return srv.(NetworkTestServer).Echo(ctx, req.(*Ping)) | |
} | |
return interceptor(ctx, in, info, handler) | |
} | |
var _NetworkTest_serviceDesc = grpc.ServiceDesc{ | |
ServiceName: "network.NetworkTest", | |
HandlerType: (*NetworkTestServer)(nil), | |
Methods: []grpc.MethodDesc{ | |
{ | |
MethodName: "Echo", | |
Handler: _NetworkTest_Echo_Handler, | |
}, | |
}, | |
Streams: []grpc.StreamDesc{}, | |
Metadata: "message.proto", | |
} | |
func init() { proto.RegisterFile("message.proto", fileDescriptor0) } | |
var fileDescriptor0 = []byte{ | |
// 121 bytes of a gzipped FileDescriptorProto | |
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xe2, 0xcd, 0x4d, 0x2d, 0x2e, | |
0x4e, 0x4c, 0x4f, 0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0xcf, 0x4b, 0x2d, 0x29, 0xcf, | |
0x2f, 0xca, 0x56, 0x92, 0xe2, 0x62, 0x09, 0xc8, 0xcc, 0x4b, 0x17, 0x12, 0xe2, 0x62, 0x29, 0x49, | |
0xad, 0x28, 0x91, 0x60, 0x54, 0x60, 0xd4, 0xe0, 0x0c, 0x02, 0xb3, 0xc1, 0x72, 0xf9, 0x48, 0x72, | |
0x4c, 0x08, 0x39, 0x23, 0x53, 0x2e, 0x6e, 0x3f, 0x88, 0x11, 0x21, 0xa9, 0xc5, 0x25, 0x42, 0x6a, | |
0x5c, 0x2c, 0xae, 0xc9, 0x19, 0xf9, 0x42, 0xbc, 0x7a, 0x50, 0x83, 0xf5, 0x40, 0xa6, 0x4a, 0x21, | |
0x71, 0xf3, 0xf3, 0xd2, 0x95, 0x18, 0x92, 0xd8, 0xc0, 0xd6, 0x1b, 0x03, 0x02, 0x00, 0x00, 0xff, | |
0xff, 0x25, 0x15, 0x89, 0x8f, 0x8f, 0x00, 0x00, 0x00, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
syntax = "proto3"; | |
package network; | |
message Ping { | |
string text = 1; | |
} | |
message Pong { | |
string text = 2; | |
} | |
service NetworkTest { | |
rpc Echo(Ping) returns (Pong) {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate byteorder; | |
use byteorder::{ByteOrder, BigEndian}; | |
use std::io::{Write, Read}; | |
use std::thread; | |
use std::io::prelude::*; | |
use std::net::TcpStream; | |
use std::time::{Duration, Instant}; | |
use std::thread::sleep; | |
use std::env; | |
fn main() { | |
let mut addr = "127.0.0.1:3000".to_string(); | |
let args: Vec<_> = env::args().collect(); | |
if args.len() > 1 { | |
addr = args[1].clone(); | |
} | |
let mut stream = TcpStream::connect(&addr).unwrap(); | |
stream.set_nodelay(true).unwrap(); | |
let mut data = vec![b'A'; 102]; | |
BigEndian::write_u16(&mut data[0..2], 100); | |
let mut stat = Instant::now(); | |
let mut max = 0f64; | |
loop { | |
let start = Instant::now(); | |
stream.write(&data).unwrap(); | |
stream.read_exact(&mut data).unwrap(); | |
let elapsed = { | |
let elapsed = start.elapsed(); | |
elapsed.as_secs() as f64 * 1e3 + elapsed.subsec_nanos() as f64 / 1e6 | |
}; | |
if elapsed > max { | |
max = elapsed; | |
} | |
if stat.elapsed().as_secs() > 10 { | |
println!("{} ms", max); | |
stat = Instant::now(); | |
max = 0f64; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate byteorder; | |
use byteorder::{ByteOrder, BigEndian}; | |
use std::io::{Write, Read}; | |
use std::net::TcpListener; | |
use std::thread; | |
fn main() { | |
let listener = TcpListener::bind("0.0.0.0:3000").unwrap(); | |
println!("listening started, ready to accept"); | |
for stream in listener.incoming() { | |
thread::spawn(|| { | |
let mut stream = stream.unwrap(); | |
stream.set_nodelay(true).unwrap(); | |
let mut data = vec![b'\0'; 10240]; | |
loop { | |
stream.read_exact(&mut data[0..2]).unwrap(); | |
let payload = BigEndian::read_u16(&data); | |
stream.read_exact(&mut data[2..2+payload as usize]).unwrap(); | |
stream.write(&data[..2+payload as usize]).unwrap(); | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"flag" | |
"log" | |
"net" | |
"runtime" | |
"sync/atomic" | |
"time" | |
"bytes" | |
"encoding/binary" | |
"io" | |
) | |
var server = flag.String("s", "127.0.0.1:3000", "server addr.") | |
var bufferSize = flag.Int("b", 8, "buffer size, in KB") | |
var payloadSize = flag.Int("p", 100, "payload size.") | |
var thread = flag.Int("t", 1, "thread num.") | |
var maxProc = flag.Int("c", 0, "max proc.") | |
func main() { | |
flag.Parse() | |
runtime.GOMAXPROCS(*maxProc) | |
var maxElapse int64 | |
for i := 0; i < *thread; i++ { | |
go func() { | |
conn, err := net.Dial("tcp", *server) | |
if err != nil { | |
log.Fatal(err) | |
} | |
payload := bytes.Repeat([]byte{'a'}, 2+*payloadSize) | |
binary.BigEndian.PutUint16(payload, uint16(*payloadSize)) | |
reader := bufio.NewReaderSize(conn, *bufferSize) | |
writer := bufio.NewWriterSize(conn, *bufferSize) | |
for { | |
start := time.Now() | |
writer.Write(payload) | |
if err := writer.Flush(); err != nil { | |
log.Fatal(err) | |
} | |
if elapse := time.Since(start); elapse > time.Millisecond*10 { | |
log.Println("writeMessage too slow:", elapse) | |
} | |
readStart := time.Now() | |
_, err = io.ReadFull(reader, payload) | |
if err != nil { | |
log.Fatal(err) | |
} | |
if elapse := time.Since(readStart); elapse > time.Millisecond*10 { | |
log.Println("readMessage too slow:", elapse) | |
} | |
if elapsed := int64(time.Since(start)); elapsed > atomic.LoadInt64(&maxElapse) { | |
atomic.StoreInt64(&maxElapse, elapsed) | |
} | |
} | |
}() | |
} | |
for { | |
time.Sleep(time.Second * 10) | |
log.Println(time.Duration(atomic.LoadInt64(&maxElapse))) | |
atomic.StoreInt64(&maxElapse, 0) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"flag" | |
"fmt" | |
"log" | |
"net" | |
"runtime" | |
"time" | |
"encoding/binary" | |
"io" | |
) | |
var port = flag.Int("p", 3000, "port") | |
var bufferSize = flag.Int("b", 8, "buffer size, in KB") | |
var maxProc = flag.Int("c", 0, "max proc.") | |
func main() { | |
flag.Parse() | |
runtime.GOMAXPROCS(*maxProc) | |
l, err := net.Listen("tcp", fmt.Sprintf(":%v", *port)) | |
if err != nil { | |
log.Fatal(err) | |
} | |
for { | |
conn, _ := l.Accept() | |
go func() { | |
defer conn.Close() | |
reader := bufio.NewReaderSize(conn, *bufferSize) | |
writer := bufio.NewWriterSize(conn, *bufferSize) | |
data := make([]byte, 10240) | |
for { | |
readStart := time.Now() | |
if _, err := io.ReadFull(reader, data[:2]); err != nil { | |
log.Println(err) | |
return | |
} | |
payloadSize := int(binary.BigEndian.Uint16(data)) | |
if _, err := io.ReadFull(reader, data[2:2+payloadSize]); err != nil { | |
log.Println(err) | |
return | |
} | |
if elapse := time.Since(readStart); elapse > time.Millisecond*10 { | |
log.Println("readMessage too slow:", elapse) | |
} | |
writeStart := time.Now() | |
writer.Write(data[:2+payloadSize]) | |
if err := writer.Flush(); err != nil { | |
log.Fatal(err) | |
} | |
if elapse := time.Since(writeStart); elapse > time.Millisecond*10 { | |
log.Println("writeMessage too slow:", elapse) | |
} | |
} | |
}() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"log" | |
"net" | |
"strings" | |
"sync/atomic" | |
"time" | |
"github.com/golang/protobuf/proto" | |
"github.com/pingcap/network" | |
) | |
var server = flag.String("s", "127.0.0.1:3000", "server addr.") | |
var payloadSize = flag.Int("p", 100, "payload size.") | |
var thread = flag.Int("t", 1, "thread num.") | |
func main() { | |
flag.Parse() | |
rmtAddr, _ := net.ResolveUDPAddr("udp", *server) | |
srcAddr := &net.UDPAddr{IP: net.IPv4zero, Port: 0} | |
var maxElapse int64 | |
for i := 0; i < *thread; i++ { | |
go func() { | |
conn, err := net.DialUDP("udp", srcAddr, rmtAddr) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer conn.Close() | |
msg := &network.Ping{Text: strings.Repeat("A", *payloadSize)} | |
data := make([]byte, 1024) | |
for { | |
start := time.Now() | |
b, _ := proto.Marshal(msg) | |
_, err = conn.Write(b) | |
if err != nil { | |
log.Println(err) | |
continue | |
} | |
var pong network.Pong | |
n, err := conn.Read(data) | |
if err != nil { | |
log.Println(err) | |
continue | |
} | |
if err = proto.Unmarshal(data[:n], &pong); err != nil { | |
log.Println(err) | |
continue | |
} | |
if elapsed := int64(time.Since(start)); elapsed > atomic.LoadInt64(&maxElapse) { | |
atomic.StoreInt64(&maxElapse, elapsed) | |
} | |
} | |
}() | |
} | |
for { | |
time.Sleep(time.Second * 10) | |
log.Println(time.Duration(atomic.LoadInt64(&maxElapse))) | |
atomic.StoreInt64(&maxElapse, 0) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"log" | |
"net" | |
"github.com/golang/protobuf/proto" | |
"github.com/pingcap/network" | |
) | |
var port = flag.Int("p", 3000, "port") | |
func main() { | |
flag.Parse() | |
listener, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.ParseIP("0.0.0.0"), Port: *port}) | |
if err != nil { | |
log.Fatal(err) | |
} | |
data := make([]byte, 1024) | |
for { | |
n, addr, err := listener.ReadFromUDP(data) | |
if err != nil { | |
log.Println(err) | |
continue | |
} | |
var ping network.Ping | |
if err = proto.Unmarshal(data[:n], &ping); err != nil { | |
log.Println(err) | |
continue | |
} | |
b, _ := proto.Marshal(&network.Pong{Text: ping.Text}) | |
_, err = listener.WriteToUDP(b, addr) | |
if err != nil { | |
log.Println(err) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment