summaryrefslogtreecommitdiff
path: root/networkQuality.go
blob: f25470714a7a81d9a8942ea367ec1e1cc0b2c8bb (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
147
148
149
150
151
152
153
154
package main

import (
	"context"
	"encoding/json"
	"flag"
	"fmt"
	"github.com/hawkinsw/goresponsiveness/ma"
	"github.com/hawkinsw/goresponsiveness/mc"
	_ "io"
	"io/ioutil"
	_ "log"
	"net/http"
	"time"
)

type ConfigUrls struct {
	SmallUrl  string `json:"small_https_download_url"`
	LargeUrl  string `json:"large_https_download_url"`
	UploadUrl string `json:"https_upload_url"`
}

type Config struct {
	Version int
	Urls    ConfigUrls `json:"urls"`
}

func (c *Config) String() string {
	return fmt.Sprintf("Version: %d\nSmall URL: %s\nLarge URL: %s\nUpload URL: %s", c.Version, c.Urls.SmallUrl, c.Urls.LargeUrl, c.Urls.UploadUrl)
}

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

var (
	// Variables to hold CLI arguments.
	configHost = flag.String("config", "networkquality.example.com", "name/IP of responsiveness configuration server.")
	configPort = flag.Int("port", 4043, "port number on which to access responsiveness configuration server.")
	debug      = flag.Bool("debug", false, "Enable debugging.")
)

func saturate(ctx context.Context, saturated chan<- interface{}, lbcGenerator func() mc.MeasurableConnection) {
	mcs := make([]mc.MeasurableConnection, 4)
	mcsPreviousTransferred := make([]uint64, 4)
	for i := range mcs {
		//mcs[i] = &mc.LoadBearingUpload{Path: config.Urls.UploadUrl}
		mcs[i] = lbcGenerator()
		mcsPreviousTransferred[i] = 0
		if !mcs[i].Start(ctx) {
			fmt.Printf("Error starting %dth MC!\n", i)
			return
		}
	}

	previousMovingAverage := float64(0)
	movingAverage := ma.NewMovingAverage(4)
	//lastFlowIncrease := uint64(0)
	for currentIteration := uint64(0); true; currentIteration++ {

		// If we are cancelled, then stop.
		if ctx.Err() != nil {
			return
		}

		time.Sleep(time.Second)

		// 1. Calculate the most recent goodput.
		totalTransfer := uint64(0)
		for i := range mcs {
			previousTransferred := mcsPreviousTransferred[i]
			currentTransferred := mcs[i].Transferred()
			totalTransfer += (currentTransferred - previousTransferred)
			mcsPreviousTransferred[i] = currentTransferred
		}

		// 2. Calculate the delta
		movingAverage.AddMeasurement(totalTransfer)
		currentMovingAverage := movingAverage.CalculateAverage()
		movingAverageDelta := ((currentMovingAverage - previousMovingAverage) / (float64(currentMovingAverage+previousMovingAverage) / 2.0)) * float64(100)
		previousMovingAverage = currentMovingAverage

		fmt.Printf("Instantaneous goodput: %f MB.\n", toMBs(float64(totalTransfer)))
		fmt.Printf("Moving average: %f MB.\n", toMBs(currentMovingAverage))
		fmt.Printf("Moving average delta: %f.\n", movingAverageDelta)


		// 3. Are we stable or not?
		if currentIteration != 0 && movingAverageDelta < float64(5) {
			// We are stable!
			fmt.Printf("Stable\n")
			break
		} else {
			// We are unstable!
			fmt.Printf("Unstable\n")
		}
	}
	saturated <- struct{}{}
}

func main() {
	flag.Parse()

	configHostPort := fmt.Sprintf("%s:%d", *configHost, *configPort)
	configUrl := fmt.Sprintf("https://%s/config", configHostPort)

	configClient := &http.Client{}
	resp, err := configClient.Get(configUrl)
	if err != nil {
		fmt.Printf("Error connecting to %s: %v\n", configHostPort, err)
		return
	}

	jsonConfig, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Printf("Error reading content downloaded from %s: %v\n", configUrl, err)
		return
	}

	var config Config
	err = json.Unmarshal(jsonConfig, &config)
	if err != nil {
		fmt.Printf("Error parsing configuration returned from %s: %v\n", configUrl, err)
		return
	}

	if *debug {
		fmt.Printf("Configuration: %s\n", &config)
	}

	operatingCtx, cancelOperatingCtx := context.WithCancel(context.Background())

	uploadSaturationChannel := make(chan interface{})

	g := func() mc.MeasurableConnection {
					return &mc.LoadBearingDownload{Path: config.Urls.LargeUrl}
	}


	go saturate(operatingCtx, uploadSaturationChannel, g)

	select {
	case <-uploadSaturationChannel:
		{
			fmt.Printf("upload is saturated!\n")
		}
	}

	time.Sleep(10 * time.Second)

	cancelOperatingCtx()

	time.Sleep(4 * time.Second)
}