File: bench.cs

package info (click to toggle)
ampsharp 2.0.4-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 328 kB
  • ctags: 446
  • sloc: cs: 3,150; makefile: 10; ansic: 7
file content (92 lines) | stat: -rw-r--r-- 2,702 bytes parent folder | download
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
/* Copyright (c) 2008-2011 - Eric P. Mangold
 * Released under the terms of the MIT/X11 license - see LICENSE.txt */
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Threading;
using System.Net.Sockets;

namespace Benchmarks
{
    class EmptyCommand: AMP.Command
    {
        public EmptyCommand()
            : base("EmptyCommand")
        {}
    }

    class bench
    {
        static AMP.Command cmd;
        static int port;
        static TcpListener listener;

        static void Main(string[] args)
        {
            cmd = new EmptyCommand();
            
            listener = new TcpListener(0);
            listener.Start(5);

            port = ((IPEndPoint)listener.LocalEndpoint).Port;
            
            Console.WriteLine("Listening on port {0}", port);

            var s = new Thread(Server);
            s.Start();

            Client();
        }

        static void Server()
        {
            TcpClient client;
            AMP.Protocol protocol;

            // the AMP.Command's we're going to respond to
            while (true)
            {
                client = listener.AcceptTcpClient();
                protocol = new AMP.Protocol(client.GetStream());

                // we could also pass a 'state' argument to RegisterResponder
                // which becomes the 'state' argument in the responders (below).
                // Since we aren't passing anything, then state will always be null
                protocol.RegisterResponder(cmd, handleCmd);
                protocol.StartReading(); // start the async data-reading loop
            }
        }

        static Object handleCmd(AMP.Msg msg, Object state)
        {
            return new AMP.Msg {};
        }

        static void Client()
        {
            var client = new TcpClient("localhost", port);
            var protocol = new AMP.Protocol(client.GetStream());
            protocol.StartReading(); // start the async data-reading loop

            int block = 10000;
            int count = 0;
            var start = DateTime.Now;
            double howLong;
            var msg = new AMP.Msg { };
            while (true)
            {
                protocol.CallRemote(cmd, msg);
                count++;

                if (count == block)
                {
                    count = 0;
                    howLong = (DateTime.Now - start).TotalSeconds;
                    System.Console.WriteLine("{0:0} AMP/s", block / howLong);
                    start = DateTime.Now;
                }
            }
        }
    }
}