forked from Cloud-Foundations/Dominator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testBandwidth.go
139 lines (133 loc) · 3.59 KB
/
testBandwidth.go
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
package main
import (
"fmt"
"io"
"math/rand"
"os"
"time"
"github.com/Cloud-Foundations/Dominator/lib/format"
"github.com/Cloud-Foundations/Dominator/lib/log"
"github.com/Cloud-Foundations/Dominator/lib/srpc"
proto "github.com/Cloud-Foundations/Dominator/proto/objectserver"
)
func testBandwidthFromServerSubcommand(args []string,
logger log.DebugLogger) error {
if err := testBandwidthFromServer(); err != nil {
return fmt.Errorf("error testing bandwidth: %s", err)
}
return nil
}
func testBandwidthToServerSubcommand(args []string,
logger log.DebugLogger) error {
if err := testBandwidthToServer(); err != nil {
return fmt.Errorf("error testing bandwidth: %s", err)
}
return nil
}
func testBandwidthFromServer() error {
client, err := srpc.DialHTTP("tcp", fmt.Sprintf("%s:%d",
*objectServerHostname, *objectServerPortNum), 0)
if err != nil {
return err
}
defer client.Close()
conn, err := client.Call("ObjectServer.TestBandwidth")
if err != nil {
return err
}
defer conn.Close()
request := proto.TestBandwidthRequest{
Duration: *testDuration,
ChunkSize: *chunkSize,
}
if err := conn.Encode(&request); err != nil {
return err
}
if err := conn.Flush(); err != nil {
return nil
}
buffer := make([]byte, *chunkSize+1)
startTime := time.Now()
var totalBytes uint64
for {
if _, err := io.ReadAtLeast(conn, buffer, len(buffer)); err != nil {
return err
}
totalBytes += uint64(len(buffer))
if buffer[len(buffer)-1] == 0 {
break
}
}
localDuration := time.Since(startTime)
var response proto.TestBandwidthResponse
if err := conn.Decode(&response); err != nil {
return err
}
localSpeed := totalBytes / uint64(localDuration.Seconds())
serverSpeed := totalBytes / uint64(response.ServerDuration.Seconds())
fmt.Fprintf(os.Stderr,
"Received %s from server in %s (%s/s), at server: %s (%s/s)\n",
format.FormatBytes(totalBytes),
format.Duration(localDuration), format.FormatBytes(localSpeed),
format.Duration(response.ServerDuration),
format.FormatBytes(serverSpeed))
return nil
}
func testBandwidthToServer() error {
client, err := srpc.DialHTTP("tcp", fmt.Sprintf("%s:%d",
*objectServerHostname, *objectServerPortNum), 0)
if err != nil {
return err
}
defer client.Close()
conn, err := client.Call("ObjectServer.TestBandwidth")
if err != nil {
return err
}
defer conn.Close()
request := proto.TestBandwidthRequest{
Duration: *testDuration,
ChunkSize: *chunkSize,
SendToServer: true,
}
if err := conn.Encode(&request); err != nil {
return err
}
if err := conn.Flush(); err != nil {
return nil
}
buffer := make([]byte, *chunkSize+1)
rand.Read(buffer[:request.ChunkSize])
var totalBytes uint64
startTime := time.Now()
stopTime := startTime.Add(request.Duration)
buffer[len(buffer)-1] = 1
for time.Until(stopTime) > 0 {
if _, err := conn.Write(buffer); err != nil {
return err
}
totalBytes += uint64(len(buffer))
}
buffer[len(buffer)-1] = 0
if _, err := conn.Write(buffer); err != nil {
return err
}
totalBytes += uint64(len(buffer))
if err := conn.Flush(); err != nil {
return nil
}
localDuration := time.Since(startTime)
var response proto.TestBandwidthResponse
if err := conn.Decode(&response); err != nil {
return err
}
localSpeed := totalBytes / uint64(localDuration.Seconds())
serverSpeed := totalBytes / uint64(response.ServerDuration.Seconds())
fmt.Fprintf(os.Stderr,
"Sent %s to server in %s (%s/s), at server: %s (%s/s)\n",
format.FormatBytes(totalBytes),
format.Duration(localDuration), format.FormatBytes(localSpeed),
format.Duration(response.ServerDuration),
format.FormatBytes(serverSpeed))
return nil
}