Skip to content

Instantly share code, notes, and snippets.

@mpenick
Last active December 4, 2020 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpenick/86eb128a03dca30d45081274b0cba403 to your computer and use it in GitHub Desktop.
Save mpenick/86eb128a03dca30d45081274b0cba403 to your computer and use it in GitHub Desktop.
package main
import (
"log"
"net"
"os"
"strconv"
"sync"
"sync/atomic"
"time"
)
var total int64 = 0
var slow int64 = 0
func dial(group *sync.WaitGroup, host string) {
defer group.Done()
dialer := net.Dialer{
Timeout: 60 * time.Second,
}
start := time.Now()
c, err := dialer.Dial("tcp", host)
if err != nil {
log.Printf("Dial error %s", err)
} else {
c.Close()
t := atomic.AddInt64(&total, 1);
elapsed := time.Since(start)
if elapsed >= time.Second {
s := atomic.AddInt64(&slow, 1);
log.Printf("Dial took %s (%f%% (%d out of %d) of connections are slow)", elapsed, 100.0 * float64(s) / float64(t), s, total)
}
}
}
func main() {
if len(os.Args) < 3 {
log.Fatalf("Requires three arguments, <num_connections> <sleep_duration> <host_with_port>")
}
numDials, err := strconv.Atoi(os.Args[1])
if err != nil || numDials <= 0 {
log.Fatalf("Invalid num dials argument")
}
sleepDuration, err := strconv.Atoi(os.Args[2])
if err != nil || sleepDuration < 0 {
log.Fatalf("Invalid sleep duration argument")
}
host := os.Args[3];
log.Printf("Using %d connections and a sleep duration of %dms. Host: %s", numDials, sleepDuration, host)
for {
var group sync.WaitGroup
group.Add(numDials)
for i := 0; i < numDials; i++ {
go dial(&group, host)
}
group.Wait()
if sleepDuration > 0 {
time.Sleep(time.Duration(sleepDuration) * time.Millisecond)
}
}
}
$ ./dial 20 250 13b24ccf-8680-47a2-a0fb-8ce30580e1f6-us-east1.db.astra-dev.datastax.com:31086
2020/12/04 13:10:08 Using 20 connections and a sleep duration of 250ms. Host: 13b24ccf-8680-47a2-a0fb-8ce30580e1f6-us-east1.db.astra-dev.datastax.com:31086
2020/12/04 13:10:16 Dial took 5.068498596s (1.098901% (2 out of 182) of connections are slow)
2020/12/04 13:10:16 Dial took 5.068479961s (0.552486% (1 out of 181) of connections are slow)
2020/12/04 13:10:16 Dial took 5.069615592s (1.639344% (3 out of 183) of connections are slow)
2020/12/04 13:10:16 Dial took 5.069603082s (2.173913% (4 out of 184) of connections are slow)
2020/12/04 13:10:16 Dial took 5.069618694s (2.702703% (5 out of 185) of connections are slow)
2020/12/04 13:10:16 Dial took 5.069612365s (3.225806% (6 out of 187) of connections are slow)
2020/12/04 13:10:16 Dial took 5.069637481s (4.761905% (9 out of 189) of connections are slow)
2020/12/04 13:10:16 Dial took 5.06962521s (4.255319% (8 out of 188) of connections are slow)
2020/12/04 13:10:16 Dial took 5.069679533s (5.263158% (10 out of 190) of connections are slow)
2020/12/04 13:10:16 Dial took 5.069657643s (3.743316% (7 out of 187) of connections are slow)
2020/12/04 13:10:16 Dial took 5.069681015s (5.759162% (11 out of 191) of connections are slow)
2020/12/04 13:10:16 Dial took 5.069703653s (6.250000% (12 out of 192) of connections are slow)
2020/12/04 13:10:16 Dial took 5.070746397s (6.735751% (13 out of 193) of connections are slow)
2020/12/04 13:10:16 Dial took 5.083477312s (7.216495% (14 out of 194) of connections are slow)
2020/12/04 13:10:16 Dial took 5.083472065s (8.629442% (17 out of 197) of connections are slow)
2020/12/04 13:10:16 Dial took 5.083452149s (7.692308% (15 out of 195) of connections are slow)
2020/12/04 13:10:16 Dial took 5.083462372s (8.163265% (16 out of 196) of connections are slow)
2020/12/04 13:10:16 Dial took 5.083482534s (9.090909% (18 out of 198) of connections are slow)
2020/12/04 13:10:16 Dial took 5.084426279s (9.547739% (19 out of 199) of connections are slow)
2020/12/04 13:10:16 Dial took 5.084422575s (10.000000% (20 out of 200) of connections are slow)
2020/12/04 13:10:59 Dial took 5.066268102s (0.853312% (21 out of 2461) of connections are slow)
2020/12/04 13:10:59 Dial took 5.066255849s (0.974026% (24 out of 2464) of connections are slow)
2020/12/04 13:10:59 Dial took 5.06623038s (0.893582% (22 out of 2462) of connections are slow)
2020/12/04 13:10:59 Dial took 5.066268469s (1.014199% (25 out of 2465) of connections are slow)
2020/12/04 13:10:59 Dial took 5.06630881s (1.054339% (26 out of 2466) of connections are slow)
2020/12/04 13:10:59 Dial took 5.066275548s (1.094447% (27 out of 2467) of connections are slow)
2020/12/04 13:10:59 Dial took 5.066253383s (0.933821% (23 out of 2463) of connections are slow)
2020/12/04 13:10:59 Dial took 5.067122023s (1.134522% (28 out of 2468) of connections are slow)
2020/12/04 13:10:59 Dial took 5.067111253s (1.174565% (29 out of 2469) of connections are slow)
2020/12/04 13:10:59 Dial took 5.067170556s (1.214575% (30 out of 2470) of connections are slow)
2020/12/04 13:10:59 Dial took 5.067126852s (1.254553% (31 out of 2471) of connections are slow)
2020/12/04 13:10:59 Dial took 5.067139108s (1.294498% (32 out of 2472) of connections are slow)
2020/12/04 13:10:59 Dial took 5.06715963s (1.334412% (33 out of 2473) of connections are slow)
2020/12/04 13:10:59 Dial took 5.067175364s (1.374293% (34 out of 2474) of connections are slow)
2020/12/04 13:10:59 Dial took 5.067210313s (1.414141% (35 out of 2475) of connections are slow)
2020/12/04 13:10:59 Dial took 5.08185442s (1.453958% (36 out of 2476) of connections are slow)
2020/12/04 13:10:59 Dial took 5.081867758s (1.533495% (38 out of 2478) of connections are slow)
2020/12/04 13:10:59 Dial took 5.081871326s (1.573215% (39 out of 2479) of connections are slow)
2020/12/04 13:10:59 Dial took 5.081839238s (1.493742% (37 out of 2477) of connections are slow)
2020/12/04 13:10:59 Dial took 5.082146701s (1.612903% (40 out of 2480) of connections are slow)
2020/12/04 13:11:48 Dial took 1.073058481s (0.756179% (41 out of 5422) of connections are slow)
2020/12/04 13:11:48 Dial took 1.07307193s (0.792773% (43 out of 5424) of connections are slow)
2020/12/04 13:11:48 Dial took 1.073102091s (0.774479% (42 out of 5423) of connections are slow)
2020/12/04 13:11:48 Dial took 1.074546354s (0.811060% (44 out of 5425) of connections are slow)
2020/12/04 13:11:48 Dial took 1.07458834s (0.847614% (46 out of 5427) of connections are slow)
2020/12/04 13:11:48 Dial took 1.074548073s (0.829340% (45 out of 5426) of connections are slow)
2020/12/04 13:11:48 Dial took 1.074578726s (0.865881% (47 out of 5428) of connections are slow)
2020/12/04 13:11:48 Dial took 1.075875907s (0.884141% (48 out of 5429) of connections are slow)
2020/12/04 13:11:48 Dial took 1.07589693s (0.902394% (49 out of 5431) of connections are slow)
2020/12/04 13:11:48 Dial took 1.075903688s (0.938881% (51 out of 5432) of connections are slow)
2020/12/04 13:11:48 Dial took 1.07588626s (0.920641% (50 out of 5431) of connections are slow)
2020/12/04 13:11:48 Dial took 1.075957193s (0.957114% (52 out of 5433) of connections are slow)
2020/12/04 13:11:48 Dial took 1.082143124s (0.975340% (53 out of 5434) of connections are slow)
2020/12/04 13:11:48 Dial took 1.082147756s (1.011773% (55 out of 5436) of connections are slow)
2020/12/04 13:11:48 Dial took 1.082154165s (1.029980% (56 out of 5437) of connections are slow)
2020/12/04 13:11:48 Dial took 1.082142908s (0.993560% (54 out of 5435) of connections are slow)
2020/12/04 13:11:48 Dial took 1.083137293s (1.048179% (57 out of 5438) of connections are slow)
2020/12/04 13:11:48 Dial took 1.083171741s (1.066372% (58 out of 5439) of connections are slow)
2020/12/04 13:11:48 Dial took 1.084280643s (1.084559% (59 out of 5440) of connections are slow)
2020/12/04 13:11:53 Dial took 1.097165773s (1.048401% (60 out of 5723) of connections are slow)
2020/12/04 13:11:53 Dial took 1.097153244s (1.065688% (61 out of 5724) of connections are slow)
2020/12/04 13:11:53 Dial took 1.09726099s (1.186529% (68 out of 5731) of connections are slow)
2020/12/04 13:11:53 Dial took 1.097311823s (1.203768% (69 out of 5732) of connections are slow)
2020/12/04 13:11:53 Dial took 1.097304853s (1.238228% (71 out of 5734) of connections are slow)
2020/12/04 13:11:53 Dial took 1.097130737s (1.082969% (62 out of 5725) of connections are slow)
2020/12/04 13:11:53 Dial took 1.097185499s (1.117514% (64 out of 5727) of connections are slow)
2020/12/04 13:11:53 Dial took 1.097162111s (1.100244% (63 out of 5726) of connections are slow)
2020/12/04 13:11:53 Dial took 1.097230017s (1.134777% (65 out of 5728) of connections are slow)
2020/12/04 13:11:53 Dial took 1.097206321s (1.152034% (66 out of 5729) of connections are slow)
2020/12/04 13:11:53 Dial took 1.097220224s (1.169284% (67 out of 5730) of connections are slow)
2020/12/04 13:11:53 Dial took 1.097257804s (1.221001% (70 out of 5733) of connections are slow)
2020/12/04 13:11:53 Dial took 1.097734254s (1.255449% (72 out of 5735) of connections are slow)
2020/12/04 13:11:53 Dial took 1.097726311s (1.272664% (73 out of 5736) of connections are slow)
2020/12/04 13:11:53 Dial took 1.097738324s (1.289873% (74 out of 5737) of connections are slow)
2020/12/04 13:11:53 Dial took 1.097733191s (1.307076% (75 out of 5738) of connections are slow)
2020/12/04 13:11:53 Dial took 1.097746318s (1.324273% (76 out of 5739) of connections are slow)
2020/12/04 13:11:54 Dial took 1.100068453s (1.341463% (77 out of 5740) of connections are slow)
2020/12/04 13:11:57 Dial took 1.095624541s (1.330831% (78 out of 5861) of connections are slow)
2020/12/04 13:11:57 Dial took 1.09564261s (1.364489% (80 out of 5863) of connections are slow)
2020/12/04 13:11:57 Dial took 1.095653087s (1.381310% (81 out of 5864) of connections are slow)
2020/12/04 13:11:57 Dial took 1.095660112s (1.347663% (79 out of 5862) of connections are slow)
2020/12/04 13:11:57 Dial took 1.095698155s (1.398124% (82 out of 5865) of connections are slow)
2020/12/04 13:11:57 Dial took 1.095735764s (1.414934% (83 out of 5866) of connections are slow)
2020/12/04 13:11:57 Dial took 1.095747485s (1.431737% (84 out of 5867) of connections are slow)
2020/12/04 13:11:57 Dial took 1.096610696s (1.448534% (85 out of 5868) of connections are slow)
2020/12/04 13:11:57 Dial took 1.096669428s (1.465326% (86 out of 5869) of connections are slow)
2020/12/04 13:11:57 Dial took 1.096658793s (1.482112% (87 out of 5870) of connections are slow)
2020/12/04 13:11:57 Dial took 1.096659413s (1.498893% (88 out of 5871) of connections are slow)
2020/12/04 13:11:57 Dial took 1.096696953s (1.515668% (89 out of 5872) of connections are slow)
2020/12/04 13:11:57 Dial took 1.096712103s (1.532437% (90 out of 5873) of connections are slow)
2020/12/04 13:11:57 Dial took 1.096726763s (1.549200% (91 out of 5874) of connections are slow)
2020/12/04 13:11:57 Dial took 1.096731129s (1.565957% (92 out of 5875) of connections are slow)
2020/12/04 13:11:57 Dial took 1.096766225s (1.582709% (93 out of 5876) of connections are slow)
2020/12/04 13:11:57 Dial took 1.097418724s (1.599456% (94 out of 5877) of connections are slow)
2020/12/04 13:11:57 Dial took 1.097433963s (1.616196% (95 out of 5878) of connections are slow)
2020/12/04 13:11:57 Dial took 1.097449451s (1.632931% (96 out of 5879) of connections are slow)
2020/12/04 13:11:57 Dial took 1.097476355s (1.649660% (97 out of 5880) of connections are slow)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment