Java数据存储写入性能:Objectify与JPA [英] Java datastore write performance: Objectify vs. JPA
问题描述
我对数据存储区运行了两个五分钟长的简单基准测试。一个人使用Google提供的Datanucleus顶部的JPA实现,另一个使用Objectify实现持久化。每个请求使用相同的9个字段创建了10个新的索引实体,每个实体使用另一个数据类型。为避免网络连接带来的影响,基准测试返回了10次写入的开始和结束时间。
AVG中位数10%90 %99%
JPA 76.5 76 53 98 114
Objectify 41.1 40 39 43 57
Objectify TX 50.6 50 44 60 69
正如你所看到的,使用Objectify比JPA快得多。对于依赖JPA的用户是否有任何性能提示,或者App Engine项目是否使用Objectify而不是JPA?这种巨大差异背后的原因是什么? 0.99百分位数? Datanucleus / JPA的设计较慢吗?
为了提供更多的细节,这里是我的Objectify代码:
// Objectify
public TimedBenchResult writeIndexedAsyncBenchmark(){
TimedBenchResult result = new TimedBenchResult(); ();
fory()。save()。entity(MyUtils.randomIndexedEntity())。now();
for(int i = 0; i <10; i ++)
}
result.stop();
返回结果;
从Objectify文档:
如果在没有显式事务的情况下对数据存储进行操作,则每个数据存储操作都被视为单独重试的单独小事务。 b
$ b因此,每个
ofy()。save()。entity()
都在它自己的小事务中。 JPA中尽可能接近的实现如下所示:
// JPA
public TimedBenchResult writeIndexedBenchmark( ){
EntityManager em = EntityManagerSingleton.getEntityManager();
TimedBenchResult result = new TimedBenchResult();
尝试{
for(int i = 0; i <10; i ++){
//需要交易
//否则涉及太多的实体组
em.getTransaction()。begin();
em.persist(MyUtils.randomJPAIndexedEntity());
em.getTransaction()。commit();
}
} finally {
em.close();
result.stop();
}
返回结果;
解决方案您应该不再担心这个问题。一个真实世界的应用程序不太可能被POJO到数据存储映射控制。选择你想花时间编程的API。
你测试的一个怪癖是,因为你的Objectify代码使用异步保存,所以你看到很多您在JPA测试中没有看到的并发性。 FWIW,无法从JPA访问异步API。
I ran two five minute long simple benchmarks against the datastore. One used the JPA implementation on top of Datanucleus provided by Google and the other used Objectify for persistence. Each request created 10 new indexed entities with the same 9 fields, each using another datatype. To avoid any effects by the network connection the benchmark returned the timespan between the start and the end of 10 writes.
AVG Median 10% 90% 99% JPA 76.5 76 53 98 114 Objectify 41.1 40 39 43 57 Objectify TX 50.6 50 44 60 69As you can see using Objectify is much faster than JPA. Are there any performance hints for people relying on JPA or should App Engine projects use Objectify instead of JPA? What is the reason behind this huge difference in < 0.99 percentile? Is Datanucleus / JPA that slower by design?
To provide more details, here my Objectify code:
// Objectify public TimedBenchResult writeIndexedAsyncBenchmark() { TimedBenchResult result = new TimedBenchResult(); for (int i = 0; i < 10; i++) { ofy().save().entity(MyUtils.randomIndexedEntity()).now(); } result.stop(); return result; }
From the Objectify documentation:
If you operate on the datastore without an explicit transaction, each datastore operation is treated like a separate little transaction which is retried separately.
So each
ofy().save().entity()
is in it's own little transaction. My as-close-as-possible implementation in JPA looks like this:// JPA public TimedBenchResult writeIndexedBenchmark() { EntityManager em = EntityManagerSingleton.getEntityManager(); TimedBenchResult result = new TimedBenchResult(); try { for (int i = 0; i < 10; i++) { // Transaction needed // otherwise too much entity groups are involved em.getTransaction().begin(); em.persist(MyUtils.randomJPAIndexedEntity()); em.getTransaction().commit(); } } finally { em.close(); result.stop(); } return result; }
解决方案You should stop worrying about this. A real world application is unlikely to be dominated by POJO-to-datastore mapping. Pick which API you'd like to spend your time programming in.
One quirk of your test is that because your Objectify code uses asynchronous saves, you're seeing a lot of concurrency that you aren't seeing in your JPA test. FWIW, there's no access to the async API from JPA.
这篇关于Java数据存储写入性能:Objectify与JPA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!