0

特定のメソッドを単独で呼び出すと、平均(10000回の呼び出しの場合)11523ナノ秒かかりますが、別のクラスのコンテキストから呼び出すと、約95721かかります

メソッド本体は

public static byte [] validateRequest(KARPPacket karpPacket)
{

try {
    long before,time;
    before =System.nanoTime();


    byte [] encryptedTicket=karpPacket.getTicket();
    byte[] decryptedTicket=null;
    if(encryptedTicket==null)
        return null;
    if(encryptedTicket.length%16==0)
    {



        decryptedTicket = Encryptor.decrypt(encryptedTicket, ClientWindow.getSecretKey());
        time=System.nanoTime()-before;
        System.out.println(time);

         if(karpPacket.getSenderProtocolAddressAsString().equals(getSrcAddressFromTicket(decryptedTicket)))
         {
            ClientTicketManager.getArpCash().put(karpPacket.getSenderProtocolAddressAsString(), karpPacket.getSenderHardwareAddressFormatted());


            return decryptedTicket;
         }


       return decryptedTicket;


    }

    return null;

    } catch (Exception e) {

        e.printStackTrace();
        return null;
    } 

その理由とそのパフォーマンスを改善する方法。これで十分なコードだと思います

これがencryptor.decryptメソッドです

public static byte[] decrypt(byte [] encryptedByteArray,String keyString)throws NoSuchAlgorithmException, 
                                                            NoSuchPaddingException, InvalidKeyException,
                                                            IllegalBlockSizeException, BadPaddingException
{
    SecretKey key=loadKey(keyString);

byte[] clearByteArray;

Cipher dCipher=Cipher.getInstance("AES");
dCipher.init(Cipher.DECRYPT_MODE,key );
clearByteArray=dCipher.doFinal(encryptedByteArray);
return clearByteArray;

}

そしてロードキー

public static SecretKey loadKey(String  keyString)  {


            byte[] encoded = keyString.getBytes();

            SecretKey key = new SecretKeySpec(encoded, "AES");
            return key;
        }

検証要求メソッドを実行する他のコンテキスト

package karp.client;

import java.awt.Color;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.sql.Timestamp;
import java.util.Date;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.swing.JOptionPane;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

import karp.client.presentation.ClientWindow;
import karp.client.util.Constants;
import karp.generalutil.common.Encryptor;
import karp.generalutil.destination.TicketDestination;
import karp.packet.KARPPacket;

public class KARPMessageHandlerP {

    String localIpAddress=null;
    ClientTicketManager clientTicketManager;
    KARPSender karpRequestSender=new KARPSender();  

byte [] srcMac;
NetworkInterface network;

    public KARPMessageHandlerP(KARPPacket karpPacket)
    {


        try
        {

            long before,time;


            localIpAddress=KARPReciever.localIpAddress;
            clientTicketManager=KARPReciever.clientTicketManager;

             srcMac = KARPReciever.srcMac;

//          



        if(karpPacket.getOperation()==KARPPacket.ARP_REPLY)
        {

            if(karpPacket.getSenderProtocolAddressAsString().equals(localIpAddress))//request sent by client
            {

                // if the reply was sent by the client no action must be taken.

            }
            else
            {

         if(karpPacket.getTargetProtocolAddressAsString().equals(localIpAddress))
         {
         byte [] ticket=karpPacket.getTicket();
         if(ticket==null)
         {   

         //delete the new entry from cash  and refresh the cash
          deleteEntry(karpPacket.getSenderProtocolAddressAsString());





         }
         else
         {


             if(validateReply(karpPacket) )
             {

                  }  
             else
             {


                 deleteEntry(karpPacket.getSenderProtocolAddressAsString());

                 //delete entry and refresh cash
             }

         }

            }
        }
        }

        else
            if(karpPacket.getOperation()==KARPPacket.ARP_REQUEST)
            {


                if(karpPacket.getSenderProtocolAddressAsString().equals(localIpAddress))//request sent by client
                    {
    //1             
                    if(karpPacket.getTicket()!=null)
                    {
                    //custom request no need to add;    


                    }
    //2         
                    else //new request need to add ticket 
                    {
                        String destinationId=(karpPacket.getTargetProtocolAddressAsString());
                        // if the map contain ticket to destination add the ticket directly
    //2-1               
                        KARPPacket customKarpPacket;
                        byte [] ticketDestinationByte=null;
                        if(ClientTicketManager.getDestinationTicketMap().containsKey(destinationId))
                        {
                             ticketDestinationByte=ClientTicketManager.getDestinationTicketMap().get(destinationId);
                                                    }
    //2-2                send ticket request
                        else
                        {   
                            ticketDestinationByte=clientTicketManager.getDestinationTicket(destinationId);



                        }
                        if(ticketDestinationByte!=null)
                        {

                        customKarpPacket= karpRequestSender.createKARPPacket(karpPacket.getDstAddress(), karpPacket.getSrcAddress(), 
                                                                            InetAddress.getLocalHost().getAddress(), karpPacket.getTargetProtoAddr(),
                                                                            karpPacket.getTargetHardAddr(),
                                                                            ticketDestinationByte, KARPPacket.ARP_REQUEST);


                        karpRequestSender.sendKARPPacket(customKarpPacket);
                        }

                    }
                    }
                else
                {
                    if(karpPacket.getTargetProtocolAddressAsString().equals(localIpAddress))//check to see if the request is for the client
                    {

                    byte [] ticketRequest=validateRequest(karpPacket);
                    if(ticketRequest!=null)
                    {

                        //reply to the request;

                        String sessionKey=getSessionKeyFromTicket(ticketRequest);

                        TicketDestination ticketDestination=new TicketDestination();

                        ticketDestination.setDestinationId(karpPacket.getSenderProtocolAddressAsString());
                        ticketDestination.setSourceId(karpPacket.getTargetProtocolAddressAsString());
                        ticketDestination.setSourceDestinationKey(sessionKey);
                        byte [] ticketDestinationByte=ticketDestination.getAsByte();
                        byte [] encryptedTicketDestination=Encryptor.encrypt(ticketDestinationByte, sessionKey);


                        ///need to review target hardware address

                        KARPPacket karpPacketReply=karpRequestSender.createKARPPacket(karpPacket.getSrcAddress(), srcMac, InetAddress.getLocalHost().getAddress(), karpPacket.getSenderProtoAddr(),karpPacket.getSrcAddress(), encryptedTicketDestination, KARPPacket.ARP_REPLY);

                        karpRequestSender.sendKARPPacket(karpPacketReply);

                    }   
                    else //delete ticket invalid request
                    {
                         deleteEntry(karpPacket.getSenderProtocolAddressAsString());

                    }


                }
                }
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        //System.out.println(timeConsumed);
    }


    public static byte [] validateRequest(KARPPacket karpPacket)
    {
        try {
            long before,time;
            before =System.nanoTime();


            byte [] encryptedTicket=karpPacket.getTicket();
            byte[] decryptedTicket=null;
            if(encryptedTicket==null)
                return null;
            if(encryptedTicket.length%16==0)
            {



                decryptedTicket = Encryptor.decrypt(encryptedTicket, ClientWindow.getSecretKey());
                time=System.nanoTime()-before;
                System.out.println(time);

                 if(karpPacket.getSenderProtocolAddressAsString().equals(getSrcAddressFromTicket(decryptedTicket)))
                 {
                    ClientTicketManager.getArpCash().put(karpPacket.getSenderProtocolAddressAsString(), karpPacket.getSenderHardwareAddressFormatted());


                    return decryptedTicket;
                 }


               return decryptedTicket;


            }

            return null;

            } catch (Exception e) {

                e.printStackTrace();
                return null;
            } 


    }


    public boolean validateReply(KARPPacket karpPacket) throws InvalidKeyException, NoSuchAlgorithmException,
    NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException
    {


//      KARPReciever.stop=true;
//      for(int i=0;i<10000;i++)
//          {

        String sessionKey;
         byte[] encryptedTicket=karpPacket.getTicket();

            if(ClientTicketManager.getDestinationKeyMap().containsKey(karpPacket.getSenderProtocolAddressAsString()))
            {
        sessionKey=ClientTicketManager.getDestinationKeyMap().get(karpPacket.getSenderProtocolAddressAsString());
         byte[] decryptedTicket=Encryptor.decrypt(encryptedTicket, sessionKey);

         if(karpPacket.getSenderProtocolAddressAsString().equals(getSrcAddressFromTicket(decryptedTicket)))
         {
            ClientTicketManager.getArpCash().put(karpPacket.getSenderProtocolAddressAsString(), karpPacket.getSenderHardwareAddressFormatted());

//          after=System.nanoTime();
//           timeConsumed=(after-before);
//          System.out.print("kl"+timeConsumed);
            return true;
         }
            }
            //}


        return false;
    }

    public void deleteEntry(String entryIpAddress)
    {
        try
        {



        if(!ClientTicketManager.getAuthenticatedUser().contains(entryIpAddress))
        {
        if(ClientTicketManager.getArpCash().containsKey(entryIpAddress))
        {
        String updateCommand="arp -s "+entryIpAddress+" "+clientTicketManager.getArpCash().get(entryIpAddress);

        //printCash();
        Runtime.getRuntime().exec(updateCommand);   
        }
        else
        {   
         String deleteCommand ="arp -d "+entryIpAddress;
         Runtime.getRuntime().exec(deleteCommand); 
        }
        }
        }
        catch(Exception e)
        {

        }



    }

    public static String getSrcAddressFromTicket(byte [] ticket)
    {

        byte [] srcByte=new byte[4];
        System.arraycopy(ticket, 16, srcByte, 0, 4);


        String srcString=TicketDestination.getIpAddressAsString(srcByte);
        return srcString;
    }

    public String getSessionKeyFromTicket(byte [] ticket)
    {
        byte [] sessionKeyByte=new byte[16];
        System.arraycopy(ticket, 0, sessionKeyByte,0, 16);
        return new String(sessionKeyByte);
    }

    public void printCash()
    {
        for(Object entry:ClientTicketManager.getArpCash().entrySet().toArray())
        {
            System.out.println(entry+" "+clientTicketManager.getArpCash().get(entry)+" entry in cash ");
        }
    }

}
4

2 に答える 2

5

コードをウォームアップすると、使用方法に基づいて高速になります。

初めて実行するときは非常に遅く、クラスをロードして初期化する必要があります。

コードを繰り返し実行すると、コードがウォームアップされ、コンパイルする部分がトリガーされるだけでなく、キャッシュ内のウォームアップも進みます。たとえば、L1 アクセスはメイン メモリ アクセスよりも 100 倍速くなります。

コードを 10,000 回または 20,000 回実行すると、すべてのコードがコンパイルされ、キャッシュ内でウォームされ、分岐予測が適切に機能します。

それでも、コンテキストを切り替えて他の操作を実行すると、キャッシュがそれほど暖かくないため、再び速度が低下します。2 ~ 5 倍の速度低下が見られます。

ところで、リソースのボトルネック、電力を節約するためにCPUの電源を落とし、より低い周波数で実行するなど、他の要因があります。

于 2013-06-16T14:31:45.213 に答える