diff options
Diffstat (limited to 'lgc')
| -rw-r--r-- | lgc/collection.go | 27 | ||||
| -rw-r--r-- | lgc/download.go | 4 | ||||
| -rw-r--r-- | lgc/lgc.go | 17 | ||||
| -rw-r--r-- | lgc/upload.go | 4 |
4 files changed, 47 insertions, 5 deletions
diff --git a/lgc/collection.go b/lgc/collection.go index 7560186..cd6a87d 100644 --- a/lgc/collection.go +++ b/lgc/collection.go @@ -16,6 +16,7 @@ package lgc import ( "fmt" + "math/rand" "sync" ) @@ -33,10 +34,10 @@ func (collection *LoadGeneratingConnectionCollection) Get(idx int) (*LoadGenerat collection.Lock.Unlock() return nil, fmt.Errorf("collection is unlocked") } + return collection.lockedGet(idx) +} - if idx > len(*collection.LGCs) { - return nil, fmt.Errorf("index too large") - } +func (collection *LoadGeneratingConnectionCollection) lockedGet(idx int) (*LoadGeneratingConnection, error) { return &(*collection.LGCs)[idx], nil } @@ -49,6 +50,22 @@ func (collection *LoadGeneratingConnectionCollection) Append(conn LoadGenerating return nil } -func (collection *LoadGeneratingConnectionCollection) Len() int { - return len(*collection.LGCs) +func (collection *LoadGeneratingConnectionCollection) Len() (int, error) { + if collection.Lock.TryLock() { + collection.Lock.Unlock() + return -1, fmt.Errorf("collection is unlocked") + } + return len(*collection.LGCs), nil +} + +func (collection *LoadGeneratingConnectionCollection) GetRandom() (*LoadGeneratingConnection, error) { + if collection.Lock.TryLock() { + collection.Lock.Unlock() + return nil, fmt.Errorf("collection is unlocked") + } + + idx := int(rand.Uint32()) + idx = idx % len(*collection.LGCs) + + return collection.lockedGet(idx) } diff --git a/lgc/download.go b/lgc/download.go index a73cc37..c13a1b1 100644 --- a/lgc/download.go +++ b/lgc/download.go @@ -64,6 +64,10 @@ func NewLoadGeneratingConnectionDownload(url string, keyLogger io.Writer, connec return lgd } +func (lgd *LoadGeneratingConnectionDownload) Direction() LgcDirection { + return LGC_DOWN +} + func (lgd *LoadGeneratingConnectionDownload) WaitUntilStarted(ctxt context.Context) bool { conditional := func() bool { return lgd.status != LGC_STATUS_NOT_STARTED } go utilities.ContextSignaler(ctxt, 500*time.Millisecond, &conditional, lgd.statusWaiter) @@ -31,6 +31,23 @@ type LoadGeneratingConnection interface { ClientId() uint64 Stats() *stats.TraceStats WaitUntilStarted(context.Context) bool + Direction() LgcDirection +} + +type LgcDirection int + +const ( + LGC_DOWN LgcDirection = iota + LGC_UP +) + +func (direction LgcDirection) String() string { + if direction == LGC_DOWN { + return "Download" + } else if direction == LGC_UP { + return "Upload" + } + return "Invalid load-generating connection direction" } type LgcStatus int diff --git a/lgc/upload.go b/lgc/upload.go index 5175fe0..e4518b8 100644 --- a/lgc/upload.go +++ b/lgc/upload.go @@ -90,6 +90,10 @@ func (lgu *LoadGeneratingConnectionUpload) Status() LgcStatus { return lgu.status } +func (lgd *LoadGeneratingConnectionUpload) Direction() LgcDirection { + return LGC_UP +} + type syntheticCountingReader struct { n *uint64 ctx context.Context |
