예제 #1
0
        public ActorServerProxyTemplate(T actorImplementation, IPEndPoint bindEndPoint, ActorServerProxyOptions options)
        {
            isStopped                = false;
            executor                 = new ActionBlockExecutor();
            serializer               = new ProtoBufStacksSerializer();
            clients                  = new List <FramedClient>();
            handlers                 = new Dictionary <string, Action <FramedClient, long, MemoryStream> >();
            actorSessions            = new Dictionary <IFramedClient, IActorSession>();
            obsHandlers              = new Dictionary <string, object>();
            this.actorImplementation = actorImplementation;
            this.options             = options;

            clientActorConnected    = new Subject <IActorSession>();
            clientActorDisconnected = new Subject <ClientActorDisconnectedData>();

            clientTimestamps = new Dictionary <FramedClient, DateTime>();
            clientErrors     = new Dictionary <FramedClient, Exception>();
            pingTimer        = new Timer(OnPingTimer, null, 10000, 10000);

            protocolHandlers = new Dictionary <int, Action <FramedClient, MemoryStream> >();
            protocolHandlers[ActorProtocol.HandshakeId] = HandleHandshakeMessage;
            protocolHandlers[ActorProtocol.PingId]      = HandlePingMessage;

            server = new SocketServer(executor, bindEndPoint);
            server.Connected.Subscribe(ClientConnected);
            server.Start();
        }
예제 #2
0
        public async void If_Enabled_IActorSession_should_get_accessible_from_ActorSession_Current()
        {
            var opts = new ActorServerProxyOptions(actorSessionInjectionEnabled: true);
            Utils.CreateServerAndClient<MessageActor, IMessageActor>(opts, out server, out client);

            var client2 = ActorClientProxy.CreateActor<IMessageActor>("tcp://localhost:" + server.BindEndPoint.Port).Result;

            await client.PassDataForContext(1);
            await client.PassDataForContext(1);
            await client2.PassDataForContext(2);
            await client.PassDataForContext(1);
            await client2.PassDataForContext(2);

        }
예제 #3
0
        public void If_Enabled_StressTest_IActorSession_should_get_accessible_from_ActorSession_Current()
        {
            var opts = new ActorServerProxyOptions(actorSessionInjectionEnabled: true);
            IMessageActor[] clients = new IMessageActor[20];

            Utils.CreateServerAndClient<MessageActor, IMessageActor>(opts, out server, out clients[0]);

            for (int i = 1; i < 20; ++i)
                clients[i] = ActorClientProxy.CreateActor<IMessageActor>("tcp://localhost:" + server.BindEndPoint.Port).Result;

            var tasks = new List<Task>();

            for (int i = 0; i < 100; ++i)
            {
                int idx = i % 20;
                tasks.Add(clients[idx].StressTestSession(idx));
            }

            Task.WaitAll(tasks.ToArray());
        }
예제 #4
0
파일: Program.cs 프로젝트: wushian/Stacks
        private static async void Start(uint timesToRun)
        {
            const int repeatFactor = 500;
            const long repeat = 3000L * repeatFactor;

            var processorCount = Environment.ProcessorCount;
            if (processorCount == 0)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Failed to read processor count..");
                return;
            }
            if (System.Diagnostics.Debugger.IsAttached)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Warning: Debugger is attached. This has a major performance impact on this benchmark");
                Console.ResetColor();
            }

            var serverProxyOptions = new ActorServerProxyOptions(actorSessionInjectionEnabled: false);

            int workerThreads;
            int completionPortThreads;
            ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);

            Console.WriteLine("Worker threads:         {0}", workerThreads);
            Console.WriteLine("OSVersion:              {0}", Environment.OSVersion);
            Console.WriteLine("ProcessorCount:         {0}", processorCount);
            Console.WriteLine("ClockSpeed:             {0} MHZ", CpuSpeed());
            Console.WriteLine("Actor Count:            {0}", processorCount * 2);
            Console.WriteLine("Messages sent/received: {0}  ({0:0e0})", GetTotalMessagesReceived(repeat));
            Console.WriteLine();

            //Warm up
            Console.Write("Local     first start time: ");
            await Benchmark(1, 1, 1, PrintStats.StartTimeOnly, -1, -1,
                (idx, wfs) => Tuple.Create((IActorServerProxy)null, new Destination(wfs)),
                (idx, d) => d,
                (idx, wfs, dest, r, latch) => new PingPongActor(wfs, dest, r, latch));
            Console.WriteLine(" ms");
            Console.Write("Remote    first start time: ");
            await Benchmark(1, 1, 1, PrintStats.StartTimeOnly, -1, -1,
                (idx, wfs) =>
                {
                    var dest = new Destination(wfs);
                    return Tuple.Create(ActorServerProxy.Create("tcp://localhost:" + (54000 + idx), dest), dest);
                },
                (idx, d) => ActorClientProxy.CreateActor<IDestination>("tcp://localhost:" + (54000 + idx)).Result,
                (idx, wfs, dest, r, latch) => new PingPongActor(wfs, dest, r, latch));
            Console.WriteLine(" ms");
            Console.WriteLine();

            Console.WriteLine("            Local actor                        Remote actor");
            Console.WriteLine("Throughput, Msgs/sec, Start [ms], Total [ms],  Msgs/sec, Start [ms], Total [ms]");
            for (var i = 0; i < timesToRun; i++)
            {
                var redCountLocalActor = 0;
                var redCountRemoteActor = 0;
                var bestThroughputLocalActor = 0L;
                var bestThroughputRemoteActor = 0L;
                foreach (var throughput in GetThroughputSettings())
                {
                    var result1 = await Benchmark(throughput, processorCount, repeat, PrintStats.LineStart | PrintStats.Stats, bestThroughputLocalActor, redCountLocalActor,
                       (idx, wfs) => Tuple.Create((IActorServerProxy)null, new Destination(wfs)),
                       (idx, d) => d,
                       (idx, wfs, dest, r, latch) => new PingPongActor(wfs, dest, r, latch));
                    bestThroughputLocalActor = result1.Item2;
                    redCountLocalActor = result1.Item3;
                    Console.Write(",  ");

                    var result2 = await Benchmark(throughput, processorCount, repeat, PrintStats.Stats, bestThroughputRemoteActor, redCountRemoteActor,
                        (idx, wfs) =>
                        {
                            var dest = new Destination(wfs);
                            return Tuple.Create(ActorServerProxy.Create("tcp://localhost:" + (54000 + idx), dest, serverProxyOptions), dest);
                        },
                        (idx, d) => ActorClientProxy.CreateActor<IDestination>("tcp://localhost:" + (54000 + idx)).Result,
                        (idx, wfs, dest, r, latch) => new PingPongActor(wfs, dest, r, latch));
                    bestThroughputRemoteActor = result2.Item2;
                    redCountRemoteActor = result2.Item3;
                    Console.WriteLine();
                }
            }

            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine("Done..");
        }
예제 #5
0
        private static IActorServerProxy Create <T>(T actorImplementation, IPEndPoint bindEndPoint, ActorServerProxyOptions options)
        {
            var aType = actorImplementation.GetType();

            tBuilder = new ServerActorTypeBuilder("ActorServerProxy_ " + aType.FullName);

            tBuilder.DefineMessagesFromInterfaceType(aType);

            var actorImplType = tBuilder.CreateActorType(aType);

            tBuilder.SaveToFile();

            var actor = Activator.CreateInstance(actorImplType, new object[] { actorImplementation, bindEndPoint, options });

            return((ActorServerProxyTemplate <T>)actor);
        }
예제 #6
0
 public static IActorServerProxy Create <T>(string bindEndPoint, T actorImpl, ActorServerProxyOptions options)
 {
     return(Create(actorImpl, IPHelpers.Parse(bindEndPoint), options));
 }
예제 #7
0
 public static IActorServerProxy Create <T>(IPEndPoint bindEndPoint, T actorImpl, ActorServerProxyOptions options)
 {
     return(Create(actorImpl, bindEndPoint, options));
 }
예제 #8
0
 public static IActorServerProxy Create <T>(string bindEndPoint, ActorServerProxyOptions options)
     where T : new()
 {
     return(Create <T>(IPHelpers.Parse(bindEndPoint), new T(), options));
 }
예제 #9
0
 public static IActorServerProxy Create <T>(IPEndPoint bindEndPoint, ActorServerProxyOptions options)
     where T : new()
 {
     return(Create(bindEndPoint, new T(), options));
 }