Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions data/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ type NDTResult struct {
// ndt7
Upload *model.ArchivalData `json:",omitempty"`
Download *model.ArchivalData `json:",omitempty"`
Ping *model.ArchivalData `json:",omitempty"`
}
22 changes: 22 additions & 0 deletions html/ndt7-ping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* jshint esversion: 6, asi: true, worker: true */
// WebWorker that runs the ndt7 ping test
onmessage = function (ev) {
'use strict'
let url = new URL(ev.data.href)
url.protocol = (url.protocol === 'https:') ? 'wss:' : 'ws:'
url.pathname = '/ndt/v7/ping'
const sock = new WebSocket(url.toString(), 'net.measurementlab.ndt.v7')
sock.onclose = function () {
postMessage(null)
}
sock.onopen = function () {
sock.onmessage = function (ev) {
if (!(ev.data instanceof Blob)) {
let m = JSON.parse(ev.data)
m.Origin = 'server'
m.Test = 'ping'
postMessage(m)
}
}
}
}
25 changes: 24 additions & 1 deletion html/ndt7.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
</head>
<body>
<div>
<div id='ping' class='result row'>[Ping]</div>
<div id='download' class='result row'>[Download]</div>
<div id='upload' class='result row'>[Upload]</div>
<div id='done' class='result row'></div>
</div>
<script type='text/javascript'>
/* jshint esversion: 6, asi: true */
Expand All @@ -49,18 +51,35 @@
}

function runSomething(testName, callback) {
let ws = Number.NaN;
let tcp = Number.NaN;
ndt7core.run(location.href, testName, function(ev, val) {
console.log(ev, val)
if (ev === 'complete') {
if (callback !== undefined) {
callback()
} else {
withElementDo('done', function (elem) {
elem.innerHTML = 'Done.'
})
}
return
}
if (ev === 'measurement' && val.AppInfo !== undefined &&
val.Origin === 'client') {
updateView(testName, val.AppInfo)
}
if (ev === 'measurement' && val.Origin === 'server' && testName === 'ping') {
if (val.WSPingInfo !== undefined) {
ws = val.WSPingInfo.MinRTT / 1e3
}
if (val.TCPInfo !== undefined) {
tcp = val.TCPInfo.MinRTT / 1e3
}
withElementDo('ping', function (elem) {
elem.innerHTML = '⓻ ' + ws.toFixed(1) + ' / ⓸ ' + tcp.toFixed(1) + ' ms'
})
}
})
}

Expand All @@ -72,7 +91,11 @@
runSomething('upload', callback)
}

runDownload(function() { runUpload(); })
function runPing(callback) {
runSomething('ping', callback)
}

runPing(function() { runDownload(function() { runUpload(); }); })
</script>
</body>
</html>
1 change: 1 addition & 0 deletions ndt-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ func main() {
}
ndt7Mux.Handle(spec.DownloadURLPath, http.HandlerFunc(ndt7Handler.Download))
ndt7Mux.Handle(spec.UploadURLPath, http.HandlerFunc(ndt7Handler.Upload))
ndt7Mux.Handle(spec.PingURLPath, http.HandlerFunc(ndt7Handler.Ping))
ndt7Server := &http.Server{
Addr: *ndt7Addr,
Handler: logging.MakeAccessLogHandler(ndt7Mux),
Expand Down
11 changes: 7 additions & 4 deletions ndt7/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package download

import (
"context"
"time"

"github.com/gorilla/websocket"
"github.com/m-lab/ndt-server/ndt7/download/sender"
Expand All @@ -15,13 +16,15 @@ import (
// Do implements the download subtest. The ctx argument is the parent
// context for the subtest. The conn argument is the open WebSocket
// connection. The resultfp argument is the file where to save results. Both
// arguments are owned by the caller of this function.
func Do(ctx context.Context, conn *websocket.Conn, resultfp *results.File) {
// arguments are owned by the caller of this function. The start argument is
// the test start time used to calculate ElapsedTime and deadlines.
func Do(ctx context.Context, conn *websocket.Conn, resultfp *results.File, start time.Time) {
// Implementation note: use child context so that, if we cannot save the
// results in the loop below, we terminate the goroutines early
wholectx, cancel := context.WithCancel(ctx)
defer cancel()
senderch := sender.Start(conn, measurer.Start(wholectx, conn, resultfp.Data.UUID))
receiverch := receiver.StartDownloadReceiver(wholectx, conn)
senderch := sender.Start(conn, measurer.Start(wholectx, conn, resultfp.Data.UUID, start), start)
receiverch := receiver.StartDownloadReceiver(wholectx, conn, start)

saver.SaveAll(resultfp, senderch, receiverch)
}
22 changes: 14 additions & 8 deletions ndt7/download/sender/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/m-lab/ndt-server/logging"
"github.com/m-lab/ndt-server/ndt7/closer"
"github.com/m-lab/ndt-server/ndt7/model"
"github.com/m-lab/ndt-server/ndt7/ping"
"github.com/m-lab/ndt-server/ndt7/ping/message"
"github.com/m-lab/ndt-server/ndt7/spec"
)

Expand All @@ -22,7 +22,7 @@ func makePreparedMessage(size int) (*websocket.PreparedMessage, error) {
return websocket.NewPreparedMessage(websocket.BinaryMessage, data)
}

func loop(conn *websocket.Conn, src <-chan model.Measurement, dst chan<- model.Measurement) {
func loop(conn *websocket.Conn, src <-chan model.Measurement, dst chan<- model.Measurement, start time.Time) {
logging.Logger.Debug("sender: start")
defer logging.Logger.Debug("sender: stop")
defer close(dst)
Expand All @@ -38,12 +38,18 @@ func loop(conn *websocket.Conn, src <-chan model.Measurement, dst chan<- model.M
logging.Logger.WithError(err).Warn("sender: makePreparedMessage failed")
return
}
deadline := time.Now().Add(spec.MaxRuntime)
deadline := start.Add(spec.MaxRuntime)
err = conn.SetWriteDeadline(deadline) // Liveness!
if err != nil {
logging.Logger.WithError(err).Warn("sender: conn.SetWriteDeadline failed")
return
}
// One RTT sample is taken before flooding the connection with data.
// That sample is not affected by HOL, so it has additional value and is treated specially.
if err := message.SendTicks(conn, start, deadline); err != nil {
logging.Logger.WithError(err).Warn("sender: ping.message.SendTicks failed")
return
}
var totalSent int64
for {
select {
Expand All @@ -57,8 +63,8 @@ func loop(conn *websocket.Conn, src <-chan model.Measurement, dst chan<- model.M
return
}
dst <- m // Liveness: this is blocking
if err := ping.SendTicks(conn, deadline); err != nil {
logging.Logger.WithError(err).Warn("sender: ping.SendTicks failed")
if err := message.SendTicks(conn, start, deadline); err != nil {
logging.Logger.WithError(err).Warn("sender: ping.message.SendTicks failed")
return
}
default:
Expand Down Expand Up @@ -99,9 +105,9 @@ func loop(conn *websocket.Conn, src <-chan model.Measurement, dst chan<- model.M
// Liveness guarantee: the sender will not be stuck sending for more then
// the MaxRuntime of the subtest, provided that the consumer will
// continue reading from the returned channel. This is enforced by
// setting the write deadline to Time.Now() + MaxRuntime.
func Start(conn *websocket.Conn, src <-chan model.Measurement) <-chan model.Measurement {
// setting the write deadline to |start| + MaxRuntime.
func Start(conn *websocket.Conn, src <-chan model.Measurement, start time.Time) <-chan model.Measurement {
dst := make(chan model.Measurement)
go loop(conn, src, dst)
go loop(conn, src, dst, start)
return dst
}
21 changes: 15 additions & 6 deletions ndt7/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/m-lab/ndt-server/ndt7/results"
"github.com/m-lab/ndt-server/ndt7/spec"
"github.com/m-lab/ndt-server/ndt7/upload"
"github.com/m-lab/ndt-server/ndt7/ping"
"github.com/m-lab/ndt-server/version"
)

Expand All @@ -36,14 +37,15 @@ func warnAndClose(writer http.ResponseWriter, message string) {
// testerFunc is the function implementing a subtest. The first argument
// is the subtest context. The second argument is the connected websocket. The
// third argument is the open file where to write results. This function does
// not own the second or the third argument.
type testerFunc = func(context.Context, *websocket.Conn, *results.File)
// not own the second or the third argument. The fourth argument is the base
// start time of the test.
type testerFunc = func(context.Context, *websocket.Conn, *results.File, time.Time)

// downloadOrUpload implements both download and upload. The writer argument
// is the HTTP response writer. The request argument is the HTTP request
// that we received. The kind argument must be spec.SubtestDownload or
// spec.SubtestUpload. The tester is a function actually implementing the
// requested ndt7 subtest.
// that we received. The kind argument must be spec.SubtestDownload,
// spec.SubtestUpload, or SubtestPing. The tester is a function actually
// implementing the requested ndt7 subtest.
func (h Handler) downloadOrUpload(writer http.ResponseWriter, request *http.Request, kind spec.SubtestKind, tester testerFunc) {
logging.Logger.Debug("downloadOrUpload: upgrading to WebSockets")
if request.Header.Get("Sec-WebSocket-Protocol") != spec.SecWebSocketProtocol {
Expand Down Expand Up @@ -106,6 +108,8 @@ func (h Handler) downloadOrUpload(writer http.ResponseWriter, request *http.Requ
result.Download = resultfp.Data
} else if kind == spec.SubtestUpload {
result.Upload = resultfp.Data
} else if kind == spec.SubtestPing {
result.Ping = resultfp.Data
} else {
logging.Logger.Warn(string(kind) + ": data not saved")
}
Expand All @@ -114,7 +118,7 @@ func (h Handler) downloadOrUpload(writer http.ResponseWriter, request *http.Requ
}
warnonerror.Close(resultfp, string(kind)+": ignoring resultfp.Close error")
}()
tester(request.Context(), conn, resultfp)
tester(request.Context(), conn, resultfp, result.StartTime)
}

// Download handles the download subtest.
Expand All @@ -126,3 +130,8 @@ func (h Handler) Download(writer http.ResponseWriter, request *http.Request) {
func (h Handler) Upload(writer http.ResponseWriter, request *http.Request) {
h.downloadOrUpload(writer, request, spec.SubtestUpload, upload.Do)
}

// Ping handles the ping subtest.
func (h Handler) Ping(writer http.ResponseWriter, request *http.Request) {
h.downloadOrUpload(writer, request, spec.SubtestPing, ping.Do)
}
7 changes: 3 additions & 4 deletions ndt7/measurer/measurer.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func measure(measurement *model.Measurement, sockfp *os.File, elapsed time.Durat
}
}

func loop(ctx context.Context, conn *websocket.Conn, UUID string, dst chan<- model.Measurement) {
func loop(ctx context.Context, conn *websocket.Conn, UUID string, dst chan<- model.Measurement, start time.Time) {
logging.Logger.Debug("measurer: start")
defer logging.Logger.Debug("measurer: stop")
defer close(dst)
Expand All @@ -66,7 +66,6 @@ func loop(ctx context.Context, conn *websocket.Conn, UUID string, dst chan<- mod
return
}
defer sockfp.Close()
start := time.Now()
connectionInfo := &model.ConnectionInfo{
Client: conn.RemoteAddr().String(),
Server: conn.LocalAddr().String(),
Expand Down Expand Up @@ -104,9 +103,9 @@ func loop(ctx context.Context, conn *websocket.Conn, UUID string, dst chan<- mod
// a timeout of DefaultRuntime seconds, provided that the consumer
// continues reading from the returned channel.
func Start(
ctx context.Context, conn *websocket.Conn, UUID string,
ctx context.Context, conn *websocket.Conn, UUID string, start time.Time,
) <-chan model.Measurement {
dst := make(chan model.Measurement)
go loop(ctx, conn, UUID, dst)
go loop(ctx, conn, UUID, dst, start)
return dst
}
1 change: 1 addition & 0 deletions ndt7/model/measurement.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ type Measurement struct {
ConnectionInfo *ConnectionInfo `json:",omitempty" bigquery:"-"`
BBRInfo *BBRInfo `json:",omitempty"`
TCPInfo *TCPInfo `json:",omitempty"`
WSPingInfo *WSPingInfo `json:",omitempty"`
}
9 changes: 9 additions & 0 deletions ndt7/model/wspinginfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package model

// WSPingInfo contains an application level (websocket) ping measurement data.
// This structure is described in the ndt7 specification.
type WSPingInfo struct {
ElapsedTime int64
LastRTT int64 // TCPInfo.RTT is smoothed RTT, LastRTT is just a sample.
MinRTT int64
}
47 changes: 47 additions & 0 deletions ndt7/ping/message/message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Package message implements operations with WebSocket PING messages.
package message

import (
"encoding/json"
"errors"
"time"

"github.com/gorilla/websocket"
)

// The json object is used as a namespace to avoid erratic interpretation of
// unsolicited pong frames. Ping and pong frames are not a part of
// Sec-WebSocket-Protocol, they're part of RFC6455. Section 5.5.3 of the RFC
// allows unsolicited pong frames. Some browsers are known to send unsolicited
// pong frames, see golang/go#6377 <https://github.com/golang/go/issues/6377>.
type pingMessage struct {
Ndt7TS int64
}

// SendTicks sends the current ticks as a ping message.
func SendTicks(conn *websocket.Conn, start time.Time, deadline time.Time) error {
msg := pingMessage{
Ndt7TS: time.Since(start).Nanoseconds(),
}
data, err := json.Marshal(msg)
if err == nil {
err = conn.WriteControl(websocket.PingMessage, data, deadline)
}
return err
}

func ParseTicks(s string, start time.Time) (elapsed time.Duration, d time.Duration, err error) {
elapsed = time.Since(start)
var msg pingMessage
err = json.Unmarshal([]byte(s), &msg)
if err != nil {
return
}
prev := msg.Ndt7TS
if 0 <= prev && prev <= elapsed.Nanoseconds() {
d = time.Duration(elapsed.Nanoseconds() - prev)
} else {
err = errors.New("RTT is negative")
}
return
}
Loading