gpt4 book ai didi

java - 我可以改进这段代码以获得更好的性能吗?

转载 作者:行者123 更新时间:2023-12-01 10:22:26 25 4
gpt4 key购买 nike

我有一个 JAVA 中的 REST WS,使用连接到数据库的 jersey。我不知道理想的执行时间应该是多少,但我觉得花费的时间太多了。

对数据库的实际调用在 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);
}

}

REST部分

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();
}
}

我有很多其他类似的函数用于不同的操作,并且每个函数都有类似的行为,这会影响系统的整体性能。

谢谢

最佳答案

The actual call to DB completes in range of 0-3 milliseconds but the overall time to complete the REST request takes >9 milliseconds.

我认为如果你的 web 层只造成 6ms 的开销,那么它就相当快了。我猜想 6 毫秒主要花在反射密集型 JSON 反序列化(到 ActiveWatcher 实例中)上。

首先,您应该使用 VisualVM(一个 GUI 应用程序,JDK 的一部分)分析您的应用程序,因为基于猜测进行优化只是一件蹩脚的事情。

如果事实证明 json 反序列化是瓶颈,那么您可以为 ActiveWatchers 类开发自定义的 jackson 反序列化器,在其中您可以利用手写代码而不是基于反射的缓慢行为。

但我仍然认为你的 9ms 已经足够快了。

关于java - 我可以改进这段代码以获得更好的性能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35488360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com