home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / Remoting / MyObject / MyObjectApp.cs < prev    next >
Encoding:
Text File  |  2000-06-23  |  15.6 KB  |  479 lines

  1. //==========================================================================
  2. //  File:       MyObjectApp.cs
  3. //
  4. //  Summary:    This file implements client side of NGWS Remoting. 
  5. //            It illustrates NGWS Remoting 
  6. //           
  7. //  Classes:    MyObjectApp
  8. //
  9. //--------------------------------------------------------------------------
  10. //  This file is part of the Microsoft NGWS Samples.
  11. //
  12. //  Copyright (C) 1999 Microsoft Corporation.  All rights reserved.
  13. //==========================================================================
  14.  
  15. using System;
  16. using System.Text;
  17. using System.Security.Policy;
  18. using System.Runtime.Remoting;
  19. using System.IO;
  20.  
  21. using MyObjectLibrary;
  22.  
  23. public class MyObjectApp
  24. {
  25.     static int batchsize=10;
  26.  
  27.     public static int Main(string[] args)
  28.     {
  29.         int ret = Initialize(args);
  30.         Console.WriteLine("Stock Remoting Sample\n");
  31.  
  32.         if (ret != -1)
  33.         {
  34.             switch (scenario)
  35.             {
  36.             case 1: 
  37.                 Scenario1();
  38.                 break;
  39.             case 2: 
  40.                 Scenario2();
  41.                 break;
  42.             case 3: 
  43.                 Scenario3();
  44.                 break;
  45.             case 4: 
  46.                 Scenario4();
  47.                 break;
  48.             }
  49.         }
  50.  
  51.         Console.WriteLine("\nEnd\n");
  52.  
  53.         return ret;
  54.     }
  55.  
  56.     public static int Scenario1()
  57.     {
  58.         // Single new
  59.         int BatchNewStartTC=0;
  60.         int BatchNewDurTC=0;
  61.         BatchNewStartTC = Environment.TickCount;
  62.  
  63.         Foo foo;
  64.         foo = new Foo();
  65.  
  66.         BatchNewDurTC = Environment.TickCount - BatchNewStartTC;
  67.         if (BatchNewDurTC < 1) BatchNewDurTC = 1;
  68.         Console.WriteLine("Single New: {0} ms, {1} ms/call {2} calls/sec", BatchNewDurTC, BatchNewDurTC / batchsize,  batchsize / BatchNewDurTC);
  69.  
  70.         // Disconnect
  71.         //RemotingServices.Disconnect(foo);
  72.  
  73.         return 0;
  74.     }
  75.  
  76.     public static int Scenario2()
  77.     {
  78.         //
  79.         // Batch new
  80.         //
  81.  
  82.         int BatchNewStartTC=0;
  83.         int BatchNewDurTC=0;
  84.         BatchNewStartTC = Environment.TickCount;
  85.  
  86.         for (int m=0;m<batchsize;m++)
  87.         {
  88.             Foo foo;
  89.             foo = new Foo();
  90.             foo = null;
  91.         }
  92.  
  93.         BatchNewDurTC = Environment.TickCount - BatchNewStartTC;
  94.         if (BatchNewDurTC < 1) BatchNewDurTC = 1;
  95.         Console.WriteLine("Batch New: {0} ms, {1} ms/call {2} calls/sec", BatchNewDurTC, BatchNewDurTC / batchsize,  batchsize / BatchNewDurTC);
  96.  
  97.         return 0;
  98.     }
  99.  
  100.  
  101.     public static void Scenario3()  
  102.     {
  103.         bool result = true;
  104.  
  105.         Console.WriteLine("new Baz");
  106.  
  107.         Baz baz = new Baz("John Smith");
  108.  
  109.         Console.WriteLine("Name: " + baz.RetrieveName());
  110.  
  111.         for (int i=0; i < 10; i++)
  112.         {
  113.             Console.WriteLine("bazDoWorkWithNumber (100)");
  114.             result = baz.DoWorkWithNumber(100);
  115.             Console.WriteLine("After DoWorkWithNumber Total Number is " + baz.RetrieveTotalNumber());
  116.  
  117.             Console.WriteLine("bazDoWorkWithNumber (75)");
  118.             result = baz.DoWorkWithNumber(75);
  119.             Console.WriteLine("After DoWorkWithNumber Total Number is " + baz.RetrieveTotalNumber());
  120.  
  121.             Console.WriteLine("bazDoWorkWithNumber (-50)");
  122.             result = baz.DoWorkWithNumber(-50);
  123.             Console.WriteLine("After DoWorkWithNumber Total Number is " + baz.RetrieveTotalNumber());
  124.         }
  125.  
  126.         /*ILease lease = (ILease)baz.GetLifetimeService();
  127.  
  128.         Console.WriteLine("CurrentLeaseTime " + lease.CurrentLeaseTime);
  129.         Console.WriteLine("InitialLeaseTime " + lease.InitialLeaseTime);
  130.         Console.WriteLine("RenewOnCallTime " + lease.RenewOnCallTime);
  131.         Console.WriteLine("SponsorshipTimeout " + lease.SponsorshipTimeout);
  132.         
  133.         lease = null;
  134.         */
  135.  
  136.         // baz = null;
  137.  
  138.         // Console.WriteLine("Sleeping for 45 seconds");   
  139.         // System.Threading.Thread.Sleep(45000);
  140.  
  141.     }
  142.  
  143.     public static int Scenario4()
  144.     {
  145.         Foo foo;
  146.         foo = new Foo();
  147.  
  148.         return Scenario10(foo);
  149.     }
  150.  
  151.  
  152.     public static int Scenario10(Foo foo)
  153.     {
  154.         byte[] bytearray1 = new byte[1];
  155.         byte[] bytearray1024 = new byte[1024];
  156.         byte[] bytearray4096 = new byte[4096];
  157.  
  158.         String string1 = new StringBuilder(1).ToString();
  159.         String string1024 = new StringBuilder(1024).ToString();
  160.         String string4096 = new StringBuilder(4096).ToString();
  161.  
  162.         // Calls
  163.  
  164.         {
  165.             int batch = batchsize;
  166.  
  167.             int endpointStartTC=0;
  168.             int endpointDurTC=0;
  169.             endpointStartTC = Environment.TickCount;
  170.  
  171.             int startTC=0;
  172.             int durTC=0;
  173.  
  174.             startTC = Environment.TickCount;
  175.             foo.VoidVoid();
  176.             durTC = Environment.TickCount - startTC;
  177.             Console.WriteLine("VoidVoid : {0} ms, - first call", durTC);
  178.             Console.WriteLine("");
  179.  
  180.             startTC = Environment.TickCount;
  181.             for (int i=0;i<batch;i++)
  182.                 foo.VoidVoid();
  183.             durTC = Environment.TickCount - startTC;
  184.             if (durTC < 1) durTC = 1;
  185.             Console.WriteLine("VoidVoid : {0} ms, {1} ms/call {2} calls/sec", durTC, durTC / batch, (batch * 1000) / durTC);
  186.  
  187. /*
  188.  
  189.             int intField=1;
  190.             //double doubleField=2.3;
  191.             //String stringField="String";
  192.             Object objectField = new Object();
  193.  
  194.             //int Field Set
  195.             startTC = Environment.TickCount;
  196.             for (int i=0;i<batch;i++)
  197.                 foo.intField = intField;
  198.             durTC = Environment.TickCount - startTC;
  199.             Console.WriteLine("intField Set: {0} ms, {1} ms/call {2} calls/sec", durTC, durTC / batch, (batch * 1000) / durTC);
  200. */
  201.  
  202. /*
  203.       //int Field Get
  204.       startTC = Environment.TickCount;
  205.       for(int i=0;i<batch;i++)
  206.         intField = foo.intField;
  207.       durTC = Environment.TickCount - startTC;
  208.       if (durTC < 1) durTC = 1;
  209.       Console.WriteLine("intField Get: {0} ms, {1} ms/call {2} calls/sec", durTC, durTC / batch, (batch * 1000) / durTC);
  210.  
  211.  
  212.  
  213.       //double Field Set
  214.       startTC = Environment.TickCount;
  215.       for(int i=0;i<batch;i++)
  216.         foo.doubleField = doubleField;
  217.       durTC = Environment.TickCount - startTC;
  218.       if (durTC < 1) durTC = 1;
  219.       Console.WriteLine("doubleField Set: {0} ms, {1} ms/call {2} calls/sec", durTC, durTC / batch, (batch * 1000) / durTC);
  220.  
  221.       //double Field Get
  222.       startTC = Environment.TickCount;
  223.       for(int i=0;i<batch;i++)
  224.         doubleField = foo.doubleField;
  225.       durTC = Environment.TickCount - startTC;
  226.       if (durTC < 1) durTC = 1;
  227.       Console.WriteLine("doubleField Get: {0} ms, {1} ms/call {2} calls/sec", durTC, durTC / batch, (batch * 1000) / durTC);
  228.  
  229.       //String Field Set
  230.       startTC = Environment.TickCount;
  231.       for(int i=0;i<batch;i++)
  232.         foo.stringField = stringField;
  233.       durTC = Environment.TickCount - startTC;
  234.       if (durTC < 1) durTC = 1;
  235.       Console.WriteLine("stringField Set: {0} ms, {1} ms/call {2} calls/sec", durTC, durTC / batch, (batch * 1000) / durTC);
  236.  
  237.       //String Field Get
  238.       startTC = Environment.TickCount;
  239.       for(int i=0;i<batch;i++)
  240.         stringField = foo.stringField;
  241.       durTC = Environment.TickCount - startTC;
  242.       if (durTC < 1) durTC = 1;
  243.       Console.WriteLine("stringField Get: {0} ms, {1} ms/call {2} calls/sec", durTC, durTC / batch, (batch * 1000) / durTC);
  244.  
  245.       //Object Field Set
  246.       startTC = Environment.TickCount;
  247.       for(int i=0;i<batch;i++)
  248.         foo.objectField = objectField;
  249.       durTC = Environment.TickCount - startTC;
  250.       if (durTC < 1) durTC = 1;
  251.       Console.WriteLine("objectField Set: {0} ms, {1} ms/call {2} calls/sec", durTC, durTC / batch, (batch * 1000) / durTC);
  252.  
  253.       //Object Field Get
  254.       startTC = Environment.TickCount;
  255.       for(int i=0;i<batch;i++)
  256.         objectField = foo.objectField;
  257.       durTC = Environment.TickCount - startTC;
  258.       if (durTC < 1) durTC = 1;
  259.       Console.WriteLine("objectField Get: {0} ms, {1} ms/call {2} calls/sec", durTC, durTC / batch, (batch * 1000) / durTC);
  260. */
  261.  
  262.             startTC = Environment.TickCount;
  263.             for (int i=0;i<batch;i++)
  264.                 foo.VoidInt4(1);
  265.             durTC = Environment.TickCount - startTC;
  266.             if (durTC < 1) durTC = 1;
  267.             Console.WriteLine("VoidInt4 : {0} ms, {1} ms/call {2} calls/sec", durTC, durTC / batch, (batch * 1000) / durTC);
  268.  
  269.             startTC = Environment.TickCount;
  270.             for (int i=0;i<batch;i++)
  271.                 foo.VoidString("String");
  272.             durTC = Environment.TickCount - startTC;
  273.             if (durTC < 1) durTC = 1;
  274.             Console.WriteLine("VoidString : {0} ms, {1} ms/call {2} calls/sec", durTC, durTC / batch, (batch * 1000) / durTC);
  275.  
  276.             //
  277.             startTC = Environment.TickCount;
  278.             for (int i=0;i<batch;i++)
  279.                 foo.VoidByteArray(bytearray1);
  280.             durTC = Environment.TickCount - startTC;
  281.             if (durTC < 1) durTC = 1;
  282.             Console.WriteLine("VoidByteArray (1) : {0} ms, {1} ms/call {2} calls/sec", durTC, durTC / batch, (batch * 1000) / durTC);
  283.  
  284.             //
  285.             startTC = Environment.TickCount;
  286.             for (int i=0;i<batch;i++)
  287.                 foo.VoidByteArray(bytearray1024);
  288.             durTC = Environment.TickCount - startTC;
  289.             if (durTC < 1) durTC = 1;
  290.             Console.WriteLine("VoidByteArray (1024) : {0} ms, {1} ms/call {2} calls/sec", durTC, durTC / batch, (batch * 1000) / durTC);
  291.  
  292.             //
  293.             startTC = Environment.TickCount;
  294.             for (int i=0;i<batch;i++)
  295.                 foo.VoidByteArray(bytearray4096);
  296.             durTC = Environment.TickCount - startTC;
  297.             if (durTC < 1) durTC = 1;
  298.             Console.WriteLine("VoidByteArray (4096) : {0} ms, {1} ms/call {2} calls/sec", durTC, durTC / batch, (batch * 1000) / durTC);
  299.             //
  300.  
  301.             //
  302.             startTC = Environment.TickCount;
  303.             for (int i=0;i<batch;i++)
  304.             {
  305.                 String s = foo.StringVoid();
  306.             }
  307.             durTC = Environment.TickCount - startTC;
  308.             if (durTC < 1) durTC = 1;
  309.             Console.WriteLine("StringVoid : {0} ms, {1} ms/call {2} calls/sec", durTC, durTC / batch, (batch * 1000) / durTC);
  310.  
  311. //
  312.  
  313.             {
  314.                 startTC = Environment.TickCount;
  315.                 for (int i=0;i<batch;i++)
  316.                 {
  317.                     String StringRet = foo.StringString(string1);
  318.                 }
  319.                 durTC = Environment.TickCount - startTC;
  320.                 if (durTC < 1) durTC = 1;
  321.                 Console.WriteLine("String (1) String (1) : {0} ms, {1} ms/call {2} calls/sec", durTC, durTC / batch, (batch * 1000) / durTC);
  322.             }
  323.  
  324.             {
  325.                 startTC = Environment.TickCount;
  326.                 for (int i=0;i<batch;i++)
  327.                 {
  328.                     String StringRet = foo.StringString(string1024);
  329.                 }
  330.                 durTC = Environment.TickCount - startTC;
  331.                 if (durTC < 1) durTC = 1;
  332.                 Console.WriteLine("String1024 String1024 (2048) : {0} ms, {1} ms/call {2} calls/sec", durTC, durTC / batch, (batch * 1000) / durTC);
  333.             }
  334.  
  335.             //
  336.             {
  337.                 startTC = Environment.TickCount;
  338.                 for (int i=0;i<batch;i++)
  339.                 {
  340.                     String StringRet = foo.StringString(string4096);
  341.                 }
  342.                 durTC = Environment.TickCount - startTC;
  343.                 if (durTC < 1) durTC = 1;
  344.                 Console.WriteLine("String4096 String4096 (8192) : {0} ms, {1} ms/call {2} calls/sec", durTC, durTC / batch, (batch * 1000) / durTC);
  345.             }
  346.             //
  347.  
  348.  
  349.  
  350. /*
  351.  
  352.       {
  353.       startTC = Environment.TickCount;
  354.       for(int i=0;i<batch;i++)
  355.       {
  356.         byte[] bytearrayRet = foo.ByteArrayByteArray(bytearray1);
  357.       }
  358.       durTC = Environment.TickCount - startTC;
  359.       if (durTC < 1) durTC = 1;
  360.       Console.WriteLine("ByteArray (1) ByteArray (1) : {0} ms, {1} ms/call {2} calls/sec", durTC, durTC / batch, (batch * 1000) / durTC);
  361.       }
  362.  
  363.       {
  364.       startTC = Environment.TickCount;
  365.       for(int i=0;i<batch;i++)
  366.       {
  367.         byte[] bytearrayRet = foo.ByteArrayByteArray(bytearray1024);
  368.       }
  369.       durTC = Environment.TickCount - startTC;
  370.       if (durTC < 1) durTC = 1;
  371.       Console.WriteLine("ByteArray1024 ByteArray1024 (2048) : {0} ms, {1} ms/call {2} calls/sec", durTC, durTC / batch, (batch * 1000) / durTC);
  372.       }
  373.  
  374.       //
  375.       {
  376.       startTC = Environment.TickCount;
  377.       for(int i=0;i<batch;i++)
  378.       {
  379.         byte[] bytearrayRet = foo.ByteArrayByteArray(bytearray4096);
  380.       }
  381.       durTC = Environment.TickCount - startTC;
  382.       if (durTC < 1) durTC = 1;
  383.       Console.WriteLine("ByteArray4096 ByteArray4096 (8192) : {0} ms, {1} ms/call {2} calls/sec", durTC, durTC / batch, (batch * 1000) / durTC);
  384.       }
  385.       //
  386. */
  387.  
  388.             Console.WriteLine("");
  389.             endpointDurTC = Environment.TickCount - endpointStartTC;
  390.             Console.WriteLine("Endpoint time : {0} ms", 
  391.                               endpointDurTC);
  392.             Console.WriteLine("");
  393.         }
  394.  
  395.         return 0;
  396.     }
  397.  
  398.  
  399.  
  400.     //---------------------------------------------------
  401.     public static int scenario = 1;
  402.  
  403.     public static int Initialize(String[] args)
  404.     {
  405.         if (args.Length == 0)
  406.         {
  407.             RemotingServices.ConfigureRemoting("MyObjectApp.cfg");
  408.             return 0;
  409.         }
  410.  
  411.  
  412.         for (int i=0;i<args.Length;i++)
  413.         {
  414.             if (
  415.                String.Compare(args[i],"HELP", true) == 0 ||
  416.                String.Compare(args[i],"?", true) == 0 ||
  417.                String.Compare(args[i],"/h", true) == 0 ||
  418.                String.Compare(args[i],"-h", true) == 0 ||
  419.                String.Compare(args[i],"-?", true) == 0 ||
  420.                String.Compare(args[i],"/?", true) == 0
  421.                )
  422.             {
  423.                 Usage();
  424.                 return -1;
  425.             }
  426.  
  427.             if (args[i].CompareTo("-b")==0)
  428.             {
  429.                 batchsize = Int32.Parse(args[i+1]);
  430.                 i++;
  431.             }
  432.  
  433.             if (args[i].CompareTo("-s1")==0)
  434.             {
  435.                 scenario = 1;
  436.             }
  437.  
  438.  
  439.             if (args[i].CompareTo("-s2")==0)
  440.             {
  441.                 scenario = 2;
  442.             }
  443.  
  444.  
  445.             if (args[i].CompareTo("-s3")==0)
  446.             {
  447.                 scenario = 3;
  448.             }
  449.  
  450.             if (args[i].CompareTo("-s4")==0)
  451.             {
  452.                 scenario = 4;
  453.             }
  454.  
  455.  
  456.             if (args[i].CompareTo("-cfg")==0)
  457.             {
  458.                 RemotingServices.ConfigureRemoting(args[i+1]);
  459.             }
  460.         }
  461.  
  462.         return 0;
  463.     }
  464.  
  465.     public static void Usage()
  466.     {
  467.         Console.WriteLine("Usage: MyObjectApp [-b batchsize] [-s1/-s2/-s3/-s4] [-cfg Configfile.cfg]\n");
  468.         Console.WriteLine("Examples : MyObjectApp");
  469.         Console.WriteLine("Examples : MyObjectApp -b 10");
  470.         Console.WriteLine("Examples : MyObjectApp -s1 (single new)");
  471.         Console.WriteLine("Examples : MyObjectApp -b 10 -s2 (batch new)");
  472.         Console.WriteLine("Examples : MyObjectApp -s3 (Activation)");
  473.         Console.WriteLine("Examples : MyObjectApp -s4 (Coverage)");
  474.         Console.WriteLine("Examples : MyObjectApp -b 10 -cfg MyObjectApp.cfg");
  475.         Console.WriteLine("Examples : MyObjectApp -cfg MyObjectApp.cfg");
  476.     }
  477. }
  478.  
  479.