globalsign / mgo

The MongoDB driver for Go
Other
1.97k stars 230 forks source link

socket: amortise cost of querying OS time counter #149

Closed domodwyer closed 6 years ago

domodwyer commented 6 years ago

116 adds a much needed ability to shrink the connection pool, but requires

tracking the last-used timestamp for each socket after every operation. Frequent calls to time.Now() in the hot-path reduced read throughput by ~6% and increased the latency (and variance) of socket operations as a whole.

This PR adds a periodically updated time value to amortise the cost of the last- used bookkeeping, restoring the original throughput at the cost of approximate last-used values (configured to be ~25ms of potential skew).

On some systems (currently including FreeBSD) querying the time counter also requires a syscall/context switch.

Fixes #142.


x => 860240ea96bd411b6f6165bb634ac6209981f2c5-select-zipfian-throughput.log
y => coarsetime-select-zipfian-throughput.log

     n   min   max  median  average    stddev      p99
x 3600 49235 64286 59562.5 59583.09 2117.0586 63637.03
y 3600 60846 64790 63199.0 63203.38  373.3961 64129.00

  54000        56000         58000        60000         62000        64000
 |--+------------+-------------+------------+-------------+------------+-------|
                                +--------+---------+
1    ---------------------------|        |         |----------------------
                                +--------+---------+
                                                                +-++
2                                                           ----| ||-----
                                                                +-++
Legend: 1=data$x, 2=data$y

At 95% probablitiy:
===> average is statistically significant     (p=0.000000, diff ~3620.294167)
===> variance is statistically significant    (p=0.000000)