' Collection of code snippets by Arne Vajhøj 
' (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.ServiceModel

Imports WCF.Common

Namespace WCF.Client
    Public Class Program
        Private Const SERVER As String = "localhost"
        Private Const SERVER_PORT As Integer = 12345
        Private Const NAME1 As String = "Test1"
        Private Const NAME2 As String = "Test2"
        Private Shared Sub TestFunctional(name As String)
            Dim tst As ITest = ChannelFactory(Of ITest).CreateChannel(New NetTcpBinding(), New EndpointAddress(String.Format("net.tcp://{0}:{1}/{2}", SERVER, SERVER_PORT, name)))
            Dim a As Integer = 123
            Dim b As Integer = 456
            Dim c As Integer = tst.Add(a, b)
            Console.WriteLine(c)
            Dim s As String = "ABC"
            Dim s2 As String = tst.Dup(s)
            Console.WriteLine(s2)
            Dim d As New Data(123, "ABC")
            Dim d2 As Data = tst.Process(d)
            Console.WriteLine("{0} {1}", d2.Iv, d2.Sv)
        End Sub
        Private Shared Sub TestInstantiation(name As String)
            Dim tst1 As ITest = ChannelFactory(Of ITest).CreateChannel(New NetTcpBinding(), New EndpointAddress(String.Format("net.tcp://{0}:{1}/{2}", SERVER, SERVER_PORT, name)))
            For i As Integer = 0 To 1
                Dim n As Integer = tst1.GetCounter()
                Console.WriteLine(n)
            Next
            Dim tst2 As ITest = ChannelFactory(Of ITest).CreateChannel(New NetTcpBinding(), New EndpointAddress(String.Format("net.tcp://{0}:{1}/{2}", SERVER, SERVER_PORT, name)))
            For i As Integer = 0 To 1
                Dim n As Integer = tst2.GetCounter()
                Console.WriteLine(n)
            Next
        End Sub
        Private Const REP As Integer = 10000
        Private Shared Sub TestPerformance(name As String)
            Dim tst As ITest = ChannelFactory(Of ITest).CreateChannel(New NetTcpBinding(), New EndpointAddress(String.Format("net.tcp://{0}:{1}/{2}", SERVER, SERVER_PORT, name)))
            Dim dt1 As DateTime = DateTime.Now
            Enumerable.Range(0, REP).AsParallel().ForAll(Sub(i) 
                                                             tst.Noop()
                                                         End Sub)
            Dim dt2 As DateTime = DateTime.Now
            Console.WriteLine("{0} requests per second", CInt(REP / (dt2 - dt1).TotalSeconds))
        End Sub
        Public Shared Sub Test(label As String, name As String)
            Console.WriteLine(label & ":")
            TestFunctional(name)
            TestInstantiation(name)
            TestPerformance(name)
        End Sub
        Public Shared Sub Main(args As String())
            Test("Single", NAME1)
            Test("PerCall", NAME2)
        End Sub
    End Class
End Namespace
