summaryrefslogtreecommitdiff
path: root/utilities/utilities.go
blob: b8e416c6fa755b95b0b7e32594a285d1c98523f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * This file is part of Go Responsiveness.
 *
 * Go Responsiveness is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software Foundation,
 * either version 3 of the License, or (at your option) any later version.
 * Go Responsiveness is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with Foobar. If not, see <https://www.gnu.org/licenses/>.
 */

package utilities

import (
	"context"
	"io"
	"math"
	"net/http"
	"os"
	"reflect"
	"time"

	"github.com/network-quality/goresponsiveness/constants"
)

func IsInterfaceNil(ifc interface{}) bool {
	return ifc == nil ||
		(reflect.ValueOf(ifc).Kind() == reflect.Ptr && reflect.ValueOf(ifc).IsNil())
}

func SignedPercentDifference(
	current float64,
	previous float64,
) (difference float64) {
	return ((current - previous) / (float64(current+previous) / 2.0)) * float64(
		100,
	)
}

func AbsPercentDifference(
	current float64,
	previous float64,
) (difference float64) {
	return (math.Abs(current-previous) / (float64(current+previous) / 2.0)) * float64(
		100,
	)
}

func Conditional(condition bool, t string, f string) string {
	if condition {
		return t
	}
	return f
}

func ToMbps(bytes float64) float64 {
	return ToMBps(bytes) * float64(8)
}

func ToMBps(bytes float64) float64 {
	return float64(bytes) / float64(1024*1024)
}

type GetLatency struct {
	Delay          time.Duration
	RoundTripCount uint16
	Err            error
}

func CalculateSequentialRTTsTime(
	ctx context.Context,
	saturated_client *http.Client,
	new_client *http.Client,
	url string,
) chan GetLatency {
	responseChannel := make(chan GetLatency)
	go func() {
		roundTripCount := uint16(0)
		before := time.Now()
		/*
			  TODO: We are not going to measure round-trip times on the load-generating connection
				right now because we are dealing with a massive amount of buffer bloat on the
				Apple CDN.

				c_a, err := saturated_client.Get(url)
				if err != nil {
					responseChannel <- GetLatency{Delay: 0, RTTs: 0, Err: err}
					return
				}
				// TODO: Make this interruptable somehow
				// by using _ctx_.
				_, err = io.ReadAll(c_a.Body)
				if err != nil {
					responseChannel <- GetLatency{Delay: 0, RTTs: 0, Err: err}
					return
				}
				roundTripCount += 5
				c_a.Body.Close()
		*/
		c_b, err := new_client.Get(url)
		if err != nil {
			responseChannel <- GetLatency{Delay: 0, RoundTripCount: 0, Err: err}
			return
		}
		// TODO: Make this interruptable somehow by using _ctx_.
		_, err = io.ReadAll(c_b.Body)
		if err != nil {
			responseChannel <- GetLatency{Delay: 0, Err: err}
			return
		}
		c_b.Body.Close()
		// We use 1 here according to the wording in 4.2.1.
		roundTripCount += 1
		responseChannel <- GetLatency{Delay: time.Since(before), RoundTripCount: roundTripCount, Err: nil}
	}()
	return responseChannel
}

func SeekForAppend(file *os.File) (err error) {
	_, err = file.Seek(0, 2)
	return
}

func IsDebug(level constants.DebugLevel) bool {
	if level <= constants.Debug {
		return true
	}
	return false
}

func IsWarn(level constants.DebugLevel) bool {
	if level <= constants.Warn {
		return true
	}
	return false
}

func IsError(level constants.DebugLevel) bool {
	if level <= constants.Error {
		return true
	}
	return false
}