public class Bean extends WithApplication {
static final String URL_BASIC = "/testBean";
static final String URL_BATCH = "/testBeanBatch";
static final String URL_TRANS = "/testBeanTrans";
private void callApi(String url) throws UnsupportedEncodingException {
long start = Calendar.getInstance().getTimeInMillis();
TestUtils.get(url);
long end = Calendar.getInstance().getTimeInMillis();
Logger.debug("end - start = " + String.valueOf(end - start));
}
@Test
public void testTran() throws UnsupportedEncodingException {
Logger.debug("testTran()");
Ebean.createSqlUpdate("delete from receipt").execute();
callApi(URL_TRANS);
callApi(URL_TRANS);
callApi(URL_TRANS);
}
@Test
public void testBatch() throws UnsupportedEncodingException {
Logger.debug("testBatch()");
Ebean.createSqlUpdate("delete from receipt").execute();
callApi(URL_BATCH);
callApi(URL_BATCH);
callApi(URL_BATCH);
}
@Test
public void testBasic() throws UnsupportedEncodingException {
Logger.debug("testBasic()");
Ebean.createSqlUpdate("delete from receipt").execute();
callApi(URL_BASIC);
callApi(URL_BASIC);
callApi(URL_BASIC);
}
}
### API 定義
1. bean() 不使用 `@Transactional`
2. beanTrans() 使用 `@Transactional`
3. beanBatch() 使用 `@Transactional`, 並使用 BatchMode
4. 每個 API 都是 Insert 1000 筆資料
```java
public Result bean() {
for (int i = 0; i < 1000; i++) {
new Receipt(new Auth(0)).save();
}
return ok();
}
`@Transactional`
public Result beanTrans() {
for (int i = 0; i < 1000; i++) {
new Receipt(new Auth(0)).save();
}
return ok();
}
public Result beanBatch() {
Transaction txn = Ebean.beginTransaction();
try {
txn.setBatchMode(true);
txn.setBatchSize(100);
// ** turn off getGeneratedKeys if you don't need to use the
// mymodel bean instances after you have inserted them **
txn.setBatchGetGeneratedKeys(false);
for (int i = 0; i < 1000; i++) {
new Receipt(new Auth(0)).save();
}
txn.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
txn.end();
}
return ok();
}
實驗結果
Test 1 測試三種 API 的結果
使用 @Transactional 可以減少約 66% 時間
是否開啟 BatchMode 似乎沒有差別
是否 setBatchMode(), setBatchSize() 沒有效果? (請看 Test 2)
[debug] application - testBasic()
[debug] application - end - start = 4669
[debug] application - end - start = 3621
[debug] application - end - start = 3387
[debug] application - end - start = 3124
[debug] application - end - start = 3482
[debug] application - testBatch()
[debug] application - end - start = 1211
[debug] application - end - start = 1079
[debug] application - end - start = 1237
[debug] application - end - start = 1131
[debug] application - end - start = 1163
[debug] application - testTran()
[debug] application - end - start = 1564
[debug] application - end - start = 1207
[debug] application - end - start = 1017
[debug] application - end - start = 1083
[debug] application - end - start = 1140
結論
@Transactional
annotation 可以加速大筆 insert 動作@Transactional
是差不多的速度)實驗方法
打開 Ebean logger
在 logback.xml 加入
Test Case 定義
import com.avaje.ebean.Ebean; import controllers.TestUtils; import org.junit.Test; import play.Logger; import play.test.WithApplication;
import java.io.UnsupportedEncodingException; import java.util.Calendar;
public class Bean extends WithApplication { static final String URL_BASIC = "/testBean"; static final String URL_BATCH = "/testBeanBatch"; static final String URL_TRANS = "/testBeanTrans";
}
實驗結果
Test 1 測試三種 API 的結果
@Transactional
可以減少約 66% 時間[debug] application - testBatch() [debug] application - end - start = 1211 [debug] application - end - start = 1079 [debug] application - end - start = 1237 [debug] application - end - start = 1131 [debug] application - end - start = 1163
[debug] application - testTran() [debug] application - end - start = 1564 [debug] application - end - start = 1207 [debug] application - end - start = 1017 [debug] application - end - start = 1083 [debug] application - end - start = 1140
~Test 3 測試 Batch Mode 的影響~
10000
筆為基準, 呼叫十一次 API, 忽略第一次結果Test 4 測試 Batch Size 的影響
10000
筆為基準, 呼叫十一次 API, 忽略第一次結果Test 5 觀察 Transaction Log
@Transactional
的 API 會有多個 txn[txnId]@Transactional
或是實作 Ebean.beginTransaction() 則會使用同一組 txn[txnId] basic transaction logtrans transaction log batch transaction log