summaryrefslogtreecommitdiff
path: root/vb/demo/Ice/throughput/Client.vb
blob: 13be4fcd63a2a09f4fed5ae8f47c57e20d992061 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
' **********************************************************************
'
' Copyright (c) 2003-2009 ZeroC, Inc. All rights reserved.
'
' This copy of Ice is licensed to you under the terms described in the
' ICE_LICENSE file included in this distribution.
'
' **********************************************************************

Imports System
Imports Demo

Module ThroughputC

    Class Client
        Inherits Ice.Application

        Private Sub menu()
            Console.Out.WriteLine("usage:")
            Console.Out.WriteLine()
            Console.Out.WriteLine("toggle type of data to send:")
            Console.Out.WriteLine("1: sequence of bytes (default)")
            Console.Out.WriteLine("2: sequence of strings(""hello"")")
            Console.Out.WriteLine("3: sequence of structs with a string (""hello"") and a double")
            Console.Out.WriteLine("4: sequence of structs with two ints and a double")
            Console.Out.WriteLine()
            Console.Out.WriteLine("select test to run:")
            Console.Out.WriteLine("t: Send sequence as twoway")
            Console.Out.WriteLine("o: Send sequence as oneway")
            Console.Out.WriteLine("r: Receive sequence")
            Console.Out.WriteLine("e: Echo (send and receive) sequence")
            Console.Out.WriteLine()
            Console.Out.WriteLine("other commands")
            Console.Out.WriteLine("s: shutdown server")
            Console.Out.WriteLine("x: exit")
            Console.Out.WriteLine("?: help")
        End Sub

        Public Overloads Overrides Function run(ByVal args() As String) As Integer
            If args.Length > 0 Then
                Console.Error.WriteLine(appName() & ": too many arguments")
                Return 1
            End If

            Dim throughput As ThroughputPrx = ThroughputPrxHelper.checkedCast(communicator.propertyToProxy("Throughput.Throughput"))
            If throughput Is Nothing Then
                Console.Error.WriteLine("invalid proxy")
                Return 1
            End If
            Dim throughputOneway As ThroughputPrx = ThroughputPrxHelper.uncheckedCast(throughput.ice_oneway())

            Dim byteSeq() As Byte = New Byte(ByteSeqSize.value - 1) {}

            Dim stringSeq() As String = New String(StringSeqSize.value - 1) {}
            For i As Integer = 0 To StringSeqSize.value - 1
                stringSeq(i) = "hello"
            Next

            Dim structSeq() As StringDouble = New StringDouble(StringDoubleSeqSize.value - 1) {}
            For i As Integer = 0 To StringDoubleSeqSize.value - 1
                structSeq(i) = New StringDouble
                structSeq(i).s = "hello"
                structSeq(i).d = 3.14
            Next

            Dim fixedSeq() As Fixed = New Fixed(FixedSeqSize.value - 1) {}
            For i As Integer = 0 To FixedSeqSize.value - 1
                fixedSeq(i).i = 0
                fixedSeq(i).j = 0
                fixedSeq(i).d = 0
            Next

            '
            ' A method needs to be invoked thousands of times before the JIT compiler
            ' will convert it to native code. To ensure an accurate throughput measurement,
            ' we need to "warm up" the JIT compiler.
            '
            Dim emptyBytes() As Byte = New Byte(0) {}
            Dim emptyStrings() As String = New String(0) {}
            Dim emptyStructs() As StringDouble = New StringDouble(0) {}
            emptyStructs(0) = New StringDouble
            Dim emptyFixed() As Fixed = New Fixed(0) {}
            emptyFixed(0) = New Fixed

            Dim repetitions As Integer = 10000
            Console.Out.Write("warming up the JIT compiler...")
            Console.Out.Flush()
            For i As Integer = 0 To repetitions - 1
               throughput.sendByteSeq(emptyBytes)
               throughput.sendStringSeq(emptyStrings)
               throughput.sendStructSeq(emptyStructs)
               throughput.sendFixedSeq(emptyFixed)

               throughput.recvByteSeq()
               throughput.recvStringSeq()
               throughput.recvStructSeq()
               throughput.recvFixedSeq()

               throughput.echoByteSeq(emptyBytes)
               throughput.echoStringSeq(emptyStrings)
               throughput.echoStructSeq(emptyStructs)
               throughput.echoFixedSeq(emptyFixed)
            Next
            throughput.endWarmup()
            Console.Out.WriteLine("ok")

            menu()

            '
            ' By default use bytes sequence.
            '
            Dim currentType As Char = "1"
            Dim seqSize As Integer = ByteSeqSize.value

            Dim line As String = Nothing
            Do
                Try
                    Console.Out.Write("==> ")
                    Console.Out.Flush()
                    line = Console.In.ReadLine()
                    If line Is Nothing Then
                        Exit Do
                    End If

                    Dim tmsec As Long = System.DateTime.Now.Ticks / 10000

                    repetitions = 100

                    If line.Equals("1") Or line.Equals("2") Or line.Equals("3") Or line.Equals("4") Then
                        currentType = line.Chars(0)
                        Select Case currentType
                            Case "1"
                                Console.WriteLine("using byte sequences")
                                seqSize = ByteSeqSize.value
                            Case "2"
                                Console.WriteLine("using string sequences")
                                seqSize = StringSeqSize.value
                            Case "3"
                                Console.WriteLine("using variable-length struct sequences")
                                seqSize = StringDoubleSeqSize.value
                            Case "4"
                                Console.WriteLine("using fixed-length struct sequences")
                                seqSize = FixedSeqSize.value
                        End Select
                    ElseIf line.Equals("t") Or line.Equals("o") Or line.Equals("r") Or line.Equals("e") Then
                        Dim c As Char = line.Chars(0)

                        Select Case c
                            Case "t"
                                Console.Out.Write("sending")
                            Case "o"
                                Console.Out.Write("sending")
                            Case "r"
                                Console.Out.Write("receiving")
                            Case "e"
                                Console.Out.Write("sending and receiving")
                        End Select

                        Console.Out.Write(" " & repetitions)
                        Select Case currentType
                            Case "1"
                                Console.Write(" byte")
                            Case "2"
                                Console.Write(" string")
                            Case "3"
                                Console.Write(" variable-length struct")
                            Case "4"
                                Console.Write(" fixed-length struct")
                        End Select
                        Console.Write(" sequences of size " & seqSize)

                        If c = "o" Then
                            Console.Out.Write(" as oneway")
                        End If

                        Console.Out.WriteLine("...")

                        For i As Integer = 0 To repetitions - 1
                            Select Case currentType
                                Case "1"
                                    Select Case c
                                        Case "t"
                                            throughput.sendByteSeq(byteSeq)
                                        Case "o"
                                            throughputOneway.sendByteSeq(byteSeq)
                                        Case "r"
                                            throughput.recvByteSeq()
                                        Case "e"
                                            throughput.echoByteSeq(byteSeq)
                                    End Select

                                Case "2"
                                    Select Case c
                                        Case "t"
                                            throughput.sendStringSeq(stringSeq)
                                        Case "o"
                                            throughputOneway.sendStringSeq(stringSeq)
                                        Case "r"
                                            throughput.recvStringSeq()
                                        Case "e"
                                            throughput.echoStringSeq(stringSeq)
                                    End Select

                                Case "3"
                                    Select Case c
                                        Case "t"
                                            throughput.sendStructSeq(structSeq)
                                        Case "o"
                                            throughputOneway.sendStructSeq(structSeq)
                                        Case "r"
                                            throughput.recvStructSeq()
                                        Case "e"
                                            throughput.echoStructSeq(structSeq)
                                    End Select
                                Case "4"
                                    Select Case c
                                        Case "t"
                                            throughput.sendFixedSeq(fixedSeq)
                                        Case "o"
                                            throughputOneway.sendFixedSeq(fixedSeq)
                                        Case "r"
                                            throughput.recvFixedSeq()
                                        Case "e"
                                            throughput.echoFixedSeq(fixedSeq)
                                    End Select
                            End Select
                        Next

                        Dim dmsec As Double = System.DateTime.Now.Ticks / 10000 - tmsec
                        Console.Out.WriteLine("time for " & repetitions & " sequences: " & dmsec.ToString("F") & "ms")
                        Console.Out.WriteLine("time per sequence: " & CType(dmsec / repetitions, Double).ToString("F") & "ms")
                        Dim wireSize As Integer = 0
                        Select Case currentType
                            Case "1"
                                wireSize = 1
                            Case "2"
                                wireSize = stringSeq(0).Length
                            Case "3"
                                wireSize = structSeq(0).s.Length
                                wireSize += 8 ' Size of double on the wire.
                            Case "4"
                                wireSize = 16 ' Size of two ints and a double on the wire.
                        End Select
                        Dim mbit As Double = repetitions * seqSize * wireSize * 8.0 / dmsec / 1000.0
                        If c = "e" Then
                            mbit *= 2
                        End If
                        Console.Out.WriteLine("throughput: " & mbit.ToString("#.##") & "Mbps")
                    ElseIf line.Equals("s") Then
                        throughput.shutdown()
                    ElseIf line.Equals("x") Then
                        '  Nothing to do
                    ElseIf line.Equals("?") Then
                        menu()
                    Else
                        Console.Out.WriteLine("unknown command `" & line & "'")
                        menu()
                    End If
                Catch ex As System.Exception
                    Console.Error.WriteLine(ex)
                End Try
            Loop While Not line.Equals("x")

            Return 0
        End Function
    End Class

    Public Sub Main(ByVal args() As String)
        Dim app As Client = New Client
        Dim status As Integer = app.main(args, "config.client")
        System.Environment.Exit(status)
    End Sub

End Module