2016-02-18 97 views
0

我在JAVA中使用連接到數據庫的球衣有一個REST WS。我不知道應該是什麼時候執行理想的時間,但我覺得所花費的時間太多了。我可以改進此代碼以獲得更好的性能嗎?

對DB的實際調用在0-3毫秒範圍內完成,但完成REST請求的總時間大於9毫秒。

下面是方法之一:

connection // declared as instance variable 
preparedStatement //declared as instance variable 
public int insertSubscription(ActiveWatchers activeWatchers) throws SQLException { 

     int index = 0; 

     try { 
      connection = DAOConnectionFactory.getConnection(); 
      preparedStatement = connection.prepareStatement(INSERT_SUBS); 
      preparedStatement.setObject(++index, activeWatchers.getPresentityURI()); 
      preparedStatement.setObject(++index, activeWatchers.getCallId()); 
      preparedStatement.setObject(++index, activeWatchers.getToTag()); 
      preparedStatement.setObject(++index, activeWatchers.getFromTag()); 
      preparedStatement.setObject(++index, activeWatchers.getToUser()); 
      preparedStatement.setObject(++index, activeWatchers.getToDomain()); 
      preparedStatement.setObject(++index, activeWatchers.getWatcherUsername()); 
      preparedStatement.setObject(++index, activeWatchers.getWatcherDomain()); 
      preparedStatement.setObject(++index, activeWatchers.getEvent()); 
      preparedStatement.setObject(++index, activeWatchers.getEventId()); 
      preparedStatement.setObject(++index, activeWatchers.getLocalCseq()); 
      preparedStatement.setObject(++index, activeWatchers.getRemoteCseq()); 
      preparedStatement.setObject(++index, activeWatchers.getExpires()); 
      preparedStatement.setObject(++index, activeWatchers.getStatus()); 
      preparedStatement.setObject(++index, activeWatchers.getReason()); 
      preparedStatement.setObject(++index, activeWatchers.getRecordRoute()); 
      preparedStatement.setObject(++index, activeWatchers.getContact()); 
      preparedStatement.setObject(++index, activeWatchers.getLocalContact()); 
      preparedStatement.setObject(++index, activeWatchers.getVersion()); 
      preparedStatement.setObject(++index, activeWatchers.getSocketInfo()); 

      long start = System.currentTimeMillis(); 
      int status = preparedStatement.executeUpdate(); 
      long end = System.currentTimeMillis(); 
      logger.debug("insertSubscription elasped time {}", (end - start)); 
      logger.debug("Insert returned with status {}.", status); 
      return status; 

     } catch (SQLException ex) { 
      logger.error("Error while adding new subscription by {}@{} for {} into database.", activeWatchers.getWatcherUsername(), activeWatchers.getWatcherDomain(), activeWatchers.getPresentityURI(), ex); 
      throw ex; 
     } catch (Exception ex) { 
      logger.error("Error while adding new subscription by {}@{} for {} into database.", activeWatchers.getWatcherUsername(), activeWatchers.getWatcherDomain(), activeWatchers.getPresentityURI(), ex); 
      throw ex; 
     } finally { 
      DAOConnectionFactory.closeConnection(connection, preparedStatement, null); 
     } 

    } 

其餘部分

subscriptionDAO //declared as instance variable 
@POST 
    @Consumes("application/json") 
    public Response addSubscription(ActiveWatchers activeWatchers) { 
     long start = System.currentTimeMillis(); 
     logger.debug("addSubscription start time {}", start); 
     subscriptionDAO = new SubscriptionDAO(); 
     try { 
      subscriptionDAO.insertSubscription(activeWatchers); 
      long end = System.currentTimeMillis(); 
      logger.debug("addSubscription elasped time {}", (end - start)); 
      return Response.status(201).build(); 
     } catch (Exception ex) { 
      logger.error("Error while creating subscription.", ex); 
      return Response.status(500).entity("Server Error").build(); 
     } 
    } 

我有很多的不同的操作其他相似的功能,每個人都有其影響類似的行爲系統的整體性能。

由於

+3

無關,但:'executeUpdate()'不返回「狀態」。它返回受該語句影響的行數。 –

+0

你爲什麼要做連接和preparedStatement實例變量? –

+0

@a_horse_with_no_name你是對的。我使用計數作爲狀態。 – user3275095

回答

1

到DB的實際調用完成在0-3毫秒的範圍,但整體的時間來完成REST請求花費> 9毫秒。

我認爲,如果你的網絡層原因只有6ms的開銷,那麼它是相當快的。我猜測6ms主要用於反射沉重的JSON反序列化(進入ActiveWatcher實例)。

首先,您應該使用VisualVM(GUI應用程序,JDK的一部分)來分析您的應用程序,因爲基於猜測進行優化只是一件蹩腳的事情。

如果事實證明json反序列化是瓶頸,那麼你可以爲你的ActiveWatchers類開發一個自定義jackson反序列化器,在那裏你可以利用基於慢速反射的行爲的手寫代碼。

但我仍然認爲你的9ms足夠快。

+0

好的,我將剖析代碼。如果你說9毫秒足夠快,我可能會同意,但我有一個C代碼,它調用了這個WS,並且做了很多事,比在幾秒內完成的更多,這就是爲什麼我覺得這太高了。 – user3275095

相關問題