-
-
Save eugene-bulkin/e8d690b4db144f468bc5 to your computer and use it in GitHub Desktop.
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" | |
"net" | |
"sync" | |
"log" | |
"time" | |
) | |
var ( | |
numBig = flag.Int("big", 4000, "Number of connections in big pulse") | |
bigIters = flag.Int("i", 3, "Number of iterations of big pulse") | |
bigSep = flag.Int("bs", 5, "Number of seconds between big pulses") | |
numSmall = flag.Int("small", 1000, "Number of connections in small pulse") | |
smallSep = flag.Int("ss", 20, "Number of seconds between small pulses") | |
linger = flag.Int("l", 4, "How long connections should linger before being disconnected") | |
) | |
var m sync.Mutex | |
var active_conns = 0 | |
var connections = make(map[net.Conn] bool) | |
func pulse(n int, linger int) { | |
var wg sync.WaitGroup | |
log.Printf("Connecting %d client(s)...\n", n) | |
for i := 0; i < n; i++ { | |
wg.Add(1) | |
go func() { | |
m.Lock() | |
defer m.Unlock() | |
defer wg.Done() | |
active_conns++ | |
conn, err := net.Dial("tcp", ":3033") | |
if err != nil { | |
log.Panicln("Unable to connect: ", err) | |
return | |
} | |
connections[conn] = true | |
}() | |
} | |
wg.Wait() | |
if len(connections) != n { | |
log.Fatalf("Unable to connect all %d client(s).\n", n) | |
} | |
log.Printf("Connected %d client(s).\n", n) | |
time.Sleep(time.Duration(linger) * time.Second) | |
for conn := range connections { | |
active_conns-- | |
err := conn.Close() | |
if err != nil { | |
log.Panicln("Unable to close connection:", err) | |
conn = nil | |
continue | |
} | |
delete(connections, conn) | |
conn = nil | |
} | |
if len(connections) > 0 { | |
log.Fatalf("Unable to disconnect all %d client(s) [%d remain].\n", n, len(connections)) | |
} | |
log.Printf("Disconnected %d client(s).\n", n) | |
} | |
func main() { | |
flag.Parse() | |
for i := 0; i < *bigIters; i++ { | |
pulse(*numBig, *linger) | |
time.Sleep(time.Duration(*bigSep) * time.Second) | |
} | |
for { | |
pulse(*numSmall, *linger) | |
time.Sleep(time.Duration(*smallSep) * time.Second) | |
} | |
} |
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 ( | |
"net" | |
"log" | |
"runtime" | |
"sync" | |
) | |
var m sync.Mutex | |
var num_clients = 0 | |
var cycle = 0 | |
func printMem() { | |
var ms runtime.MemStats | |
runtime.ReadMemStats(&ms) | |
log.Printf("Cycle #%3d: %5d clients | System: %8d Inuse: %8d Released: %8d Objects: %6d\n", cycle, num_clients, ms.HeapSys, ms.HeapInuse, ms.HeapReleased, ms.HeapObjects) | |
} | |
func handleConnection(conn net.Conn) { | |
//log.Println("Accepted connection:", conn.RemoteAddr()) | |
m.Lock() | |
num_clients++ | |
if num_clients % 500 == 0 { | |
printMem() | |
} | |
m.Unlock() | |
buffer := make([]byte, 256) | |
for { | |
_, err := conn.Read(buffer) | |
if err != nil { | |
//log.Println("Lost connection:", conn.RemoteAddr()) | |
err := conn.Close() | |
if err != nil { | |
log.Println("Connection close error:", err) | |
} | |
m.Lock() | |
num_clients-- | |
if num_clients % 500 == 0 { | |
printMem() | |
} | |
if num_clients == 0 { | |
cycle++ | |
} | |
m.Unlock() | |
break | |
} | |
} | |
} | |
func main() { | |
printMem() | |
cycle++ | |
listener, err := net.Listen("tcp", ":3033") | |
if err != nil { | |
log.Fatal("Could not listen.") | |
} | |
for { | |
conn, err := listener.Accept() | |
if err != nil { | |
log.Println("Could not listen to client:", err) | |
continue | |
} | |
go handleConnection(conn) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment