summaryrefslogtreecommitdiff
path: root/utilities/utilities.go
diff options
context:
space:
mode:
Diffstat (limited to 'utilities/utilities.go')
-rw-r--r--utilities/utilities.go50
1 files changed, 49 insertions, 1 deletions
diff --git a/utilities/utilities.go b/utilities/utilities.go
index 54f0f4a..16585d6 100644
--- a/utilities/utilities.go
+++ b/utilities/utilities.go
@@ -1,6 +1,12 @@
package utilities
-import "math"
+import (
+ "context"
+ "io"
+ "math"
+ "net/http"
+ "time"
+)
func SignedPercentDifference(current float64, previous float64) (difference float64) {
return ((current - previous) / (float64(current+previous) / 2.0)) * float64(100)
@@ -15,3 +21,45 @@ func Conditional(condition bool, t string, f string) string {
}
return f
}
+
+type GetLatency struct {
+ Delay time.Duration
+ Err error
+}
+
+func TimedSequentialGets(ctx context.Context, client_a *http.Client, client_b *http.Client, url string) chan GetLatency {
+ responseChannel := make(chan GetLatency)
+ go func() {
+ before := time.Now()
+ c_a, err := client_a.Get(url)
+ if err != nil {
+ responseChannel <- GetLatency{Delay: 0, Err: err}
+ }
+ // TODO: Make this interruptable somehow by using _ctx_.
+ _, err = io.ReadAll(c_a.Body)
+ if err != nil {
+ responseChannel <- GetLatency{Delay: 0, Err: err}
+ }
+ c_b, err := client_b.Get(url)
+ if err != nil {
+ responseChannel <- GetLatency{Delay: 0, Err: err}
+ }
+ // TODO: Make this interruptable somehow by using _ctx_.
+ _, err = io.ReadAll(c_b.Body)
+ if err != nil {
+ responseChannel <- GetLatency{Delay: 0, Err: err}
+ }
+ responseChannel <- GetLatency{Delay: time.Now().Sub(before), Err: nil}
+ }()
+ return responseChannel
+}
+
+type RandZeroSource struct{}
+
+func (RandZeroSource) Read(b []byte) (n int, err error) {
+ for i := range b {
+ b[i] = 0
+ }
+
+ return len(b), nil
+}