Skip to content

Commit

Permalink
attack: Force monotonic timestamp marshalling (tsenart#377)
Browse files Browse the repository at this point in the history
* attack: Force monotonic timestamp marshalling

This commit forces result timestamps to be monotonically increasing.

This is necessary because the built-in monotonic time reading of the standard
library time package is discarded when marshalling a time.Time instance.

This can lead to non-monotonic timestamps in vegeta results of a single
attack.

Although an explicit decision as stated in the time package documentation,
this assumption seems surprising to me.

From: https://golang.org/pkg/time/#hdr-Monotonic_Clocks

> Because the monotonic clock reading has no meaning outside the current
> process, the serialized forms generated by t.GobEncode, t.MarshalBinary,
> t.MarshalJSON, and t.MarshalText omit the monotonic clock reading,
> and t.Format provides no format for it. Similarly, the constructors
> time.Date, time.Parse, time.ParseInLocation, and time.Unix, as well as
> the unmarshalers t.GobDecode, t.UnmarshalBinary. t.UnmarshalJSON, and
> t.UnmarshalText always create times with no monotonic clock reading.

* Add github.com/gavv/monotime to Gopkg.*

* fixup! Remove redundant monotime dependency
  • Loading branch information
tsenart authored Mar 5, 2019
1 parent 3605d84 commit 892bb03
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/attack.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Attacker struct {
redirects int
seqmu sync.Mutex
seq uint64
began time.Time
}

const (
Expand Down Expand Up @@ -59,6 +60,7 @@ func NewAttacker(opts ...func(*Attacker)) *Attacker {
stopch: make(chan struct{}),
workers: DefaultWorkers,
maxBody: DefaultMaxBody,
began: time.Now(),
}

a.dialer = &net.Dialer{
Expand All @@ -69,8 +71,8 @@ func NewAttacker(opts ...func(*Attacker)) *Attacker {

a.client = http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: a.dialer.Dial,
Proxy: http.ProxyFromEnvironment,
Dial: a.dialer.Dial,
ResponseHeaderTimeout: DefaultTimeout,
TLSClientConfig: DefaultTLSConfig,
TLSHandshakeTimeout: 10 * time.Second,
Expand Down Expand Up @@ -291,7 +293,7 @@ func (a *Attacker) hit(tr Targeter, name string) *Result {
}()

a.seqmu.Lock()
res.Timestamp = time.Now()
res.Timestamp = a.began.Add(time.Since(a.began))
res.Seq = a.seq
a.seq++
a.seqmu.Unlock()
Expand Down

0 comments on commit 892bb03

Please sign in to comment.