- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在开始一个新项目,我有大约 190 个存储库测试。我注意到的一件事 - 我不完全确定为什么会这样 - 是针对 HSQLDB (2.2.8) 的集成测试运行速度比我认为的要慢得多。
我想我已经在每次测试之前跟踪了数据插入的瓶颈。对于大多数测试,设置数据库所需的时间从 0.15 秒到 0.38 秒不等。这是无法接受的。我本以为内存数据库会快得多:(
这是我的所有存储库测试都从中扩展的数据库测试类:
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
@TransactionConfiguration(defaultRollback=true)
@Transactional
public abstract class DatabaseTest {
public static final String TEST_RESOURCES = "src/test/resources/";
@Autowired
protected SessionFactory sessionFactory;
@Autowired
protected UserRepository userRepository;
@Autowired
protected DataSource dataSource;
protected IDatabaseTester databaseTester;
protected Map<String, Object> jdbcMap;
protected JdbcTemplate jdbcTemplate;
@PostConstruct
public void initialize() throws SQLException, IOException, DataSetException {
jdbcTemplate = new JdbcTemplate(dataSource);
setupHsqlDb();
databaseTester = new DataSourceDatabaseTester(dataSource);
databaseTester.setSetUpOperation(DatabaseOperation.CLEAN_INSERT);
databaseTester.setTearDownOperation(DatabaseOperation.NONE);
databaseTester.setDataSet(getDataSet());
}
@Before
public void insertDbUnitData() throws Exception {
long time = System.currentTimeMillis();
databaseTester.onSetup();
long elapsed = System.currentTimeMillis() - time;
System.out.println(getClass() + " Insert DB Unit Data took: " + elapsed);
}
@After
public void cleanDbUnitData() throws Exception {
databaseTester.onTearDown();
}
public IDataSet getDataSet() throws IOException, DataSetException {
Set<String> filenames = getDataSets().getFilenames();
IDataSet[] dataSets = new IDataSet[filenames.size()];
Iterator<String> iterator = filenames.iterator();
for(int i = 0; iterator.hasNext(); i++) {
dataSets[i] = new FlatXmlDataSet(
new FlatXmlProducer(
new InputSource(TEST_RESOURCES + iterator.next()), false, true
)
);
}
return new CompositeDataSet(dataSets);
}
public void setupHsqlDb() throws SQLException {
Connection sqlConnection = DataSourceUtils.getConnection(dataSource);
String databaseName = sqlConnection.getMetaData().getDatabaseProductName();
sqlConnection.close();
if("HSQL Database Engine".equals(databaseName)) {
jdbcTemplate.update("SET DATABASE REFERENTIAL INTEGRITY FALSE;");
// MD5
jdbcTemplate.update("DROP FUNCTION MD5 IF EXISTS;");
jdbcTemplate.update(
"CREATE FUNCTION MD5(VARCHAR(226)) " +
"RETURNS VARCHAR(226) " +
"LANGUAGE JAVA " +
"DETERMINISTIC " +
"NO SQL " +
"EXTERNAL NAME 'CLASSPATH:org.apache.commons.codec.digest.DigestUtils.md5Hex';"
);
} else {
jdbcTemplate.update("SET foreign_key_checks = 0;");
}
}
protected abstract DataSet getDataSets();
protected void flush() {
sessionFactory.getCurrentSession().flush();
}
protected void clear() {
sessionFactory.getCurrentSession().clear();
}
protected void setCurrentUser(User user) {
if(user != null) {
Authentication authentication = new UsernamePasswordAuthenticationToken(user,
user, user.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(authentication);
}
}
protected void setNoCurrentUser() {
SecurityContextHolder.getContext().setAuthentication(null);
}
protected User setCurrentUser(long userId) {
User user = userRepository.find(userId);
if(user.getId() != userId) {
throw new IllegalArgumentException("There is no user with id: " + userId);
}
setCurrentUser(user);
return user;
}
protected User getCurrentUser() {
return (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
}
}
这是我的应用程序上下文中的相关 bean:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:applicationContext.properties"/>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${database.driver}"/>
<property name="jdbcUrl" value="${database.url}"/>
<property name="user" value="${database.username}"/>
<property name="password" value="${database.password}"/>
<property name="initialPoolSize" value="10"/>
<property name="minPoolSize" value="10"/>
<property name="maxPoolSize" value="50"/>
<property name="idleConnectionTestPeriod" value="100"/>
<property name="acquireIncrement" value="2"/>
<property name="maxStatements" value="0"/>
<property name="maxIdleTime" value="1800"/>
<property name="numHelperThreads" value="3"/>
<property name="acquireRetryAttempts" value="2"/>
<property name="acquireRetryDelay" value="1000"/>
<property name="checkoutTimeout" value="5000"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>...</value>
</list>
</property>
<property name="namingStrategy">
<bean class="org.hibernate.cfg.ImprovedNamingStrategy"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="javax.persistence.validation.mode">none</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}
</prop>
<prop key="hibernate.generate_statistics">false</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.cache.use_second_level_cache">false</prop>
<prop key="hibernate.cache.provider_class">
</prop>
</props>
</property>
</bean>
<bean class="org.springframework.orm.hibernate4.HibernateExceptionTranslator"/>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
为了尝试插入更少的数据,我允许每个测试类选择一个只加载它需要的数据的数据集枚举。它是这样指定的:
public enum DataSet {
NONE(create()),
CORE(create("core.xml")),
USERS(combine(create("users.xml"), CORE)),
TAGS(combine(create("tags.xml"), USERS)),
这会不会导致它运行得更慢而不是更快?这个想法是,如果我只想要核心 xml(语言、省份等),我只需要加载那些记录。我认为这会使测试套件更快,但它仍然太慢。
我可以通过创建专门为每个测试类设计的单独 xml 数据集来节省一些时间。这会删除一些插入语句。但是,即使我在单个 xml 数据集中有 20 个插入语句(因此,除了将数据集直接内联到 java 代码之外,I/O 损失最小),每个测试在初始化过程中仍然需要 0.1 到 0.15 秒数据库数据!我不敢相信将 20 条记录插入内存需要 0.15 秒。
在我使用 Spring 3.0 和 Hibernate 3.x 的另一个项目中,在每次测试之前插入所有内容需要 30 毫秒,但实际上每次测试插入 100 行或更多行。对于只有 20 个插入的测试,它们正在飞行,就好像根本没有延迟一样。这是我所期望的。我开始认为问题在于 Spring 的注释 - 或者我在 DatabaseTest
类中设置它们的方式。这基本上是现在唯一不同的地方。
此外,我的存储库使用 sessionFactory.getCurrentSession() 而不是 HibernateTemplate。这是我第一次开始使用 Spring 中基于注释的单元测试,因为 Spring 测试类已被弃用。这可能是他们进展缓慢的原因吗?
如果您需要了解任何其他信息以帮助解决问题,请告诉我。我有点难过。
编辑:我输入了答案。问题是 hsqldb 2.2.x。恢复到 2.0.0 可以解决问题。
最佳答案
恕我直言,这看起来相当快。我见过更慢的集成测试。也就是说,有多种方法可以使您的测试更快:
我想用 DbUnit 应该可以做到。如果您准备使用其他框架,可以使用我自己的 DbSetup ,开箱即用。
关于performance - 使用 Spring 3.1、Hibernate 4.1、Dbunit 等提高数据库测试的性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12876278/
我正在比较工作簿中的工作表。该工作簿有两张名为 PRE 和 POST 的工作表,每张工作表都有相同的 19 列。行数每天都不同,但特定一天的两张表的行数相同。该宏将 PRE 工作表中的每一行与 POS
我有一个对象数组,我一次循环遍历该数组一个对象,然后进行几次检查以查看该数组中的每个对象是否满足特定条件,如果该对象满足此条件,则复制一个属性将此对象放入数组中(该属性还包含另一个对象)。 for(v
我正在编写一个必须非常快的应用程序。我使用 Qt 5.5 和 Qt Creator,Qt 的 64 位 MSVC2013 编译版本。 我使用非常困倦的 CS 来分析我的应用程序,我看到占用最多独占时间
我有以下 CountDownTimer 在我的 Android 应用程序中不断运行。 CountDownTimer timer_status; timer_status = new CountDown
有一个优化问题,我必须调用随机森林回归器的预测函数数千次。 from sklearn.ensemble import RandomForestRegressor rfr = RandomForestR
我正在努力提高现有 Asp.Net Web 应用程序的数据访问层的性能。场景是。 它是一个基于 Web 的 Asp.Net 应用程序。 数据访问层使用 NHibernate 1.2 构建并作为 WCF
我在我的 Intel Edison 上运行 Debian,并尝试使用 ffmpeg 通过 USB 网络摄像头捕获视频。我正在使用的命令是: ffmpeg -f video4linux2 -i /dev
我有一个 For循环遍历整数 1 到 9 并简单地找到与该整数对应的最底部的条目(即 1,1,1,2,3,4,5 将找到第三个“1”条目)并插入一个空白行。我将数字与仅对应于此代码的应用程序的字符串“
我有一个带有非规范化架构(1 个表)的 postgresql 数据库,其中包含大约 400 万个条目。现在我有这个查询: SELECT count(*) AS Total, (SELECT c
在 Ltac 中实现复杂的策略时,有一些 Ltac 命令或策略调用我预计会失败以及预期失败(例如终止 repeat 或导致回溯)。这些故障通常在故障级别 0 时引发。 更高级别引发的故障“逃避”周
我正在尝试提高 ansible playbook 的性能。我有一个测试剧本如下: --- - name: Test hosts: localhost connection: local g
我正在使用 axios从 Azure 存储 Blob 下载文件 (~100MB)。 axios({ method: 'get', url: uri, onDownloadProgress:
我有一个 ClojureScript 程序,主要对集合执行数学计算。它是在惯用的、独立于主机的 Clojure 中开发的,因此很容易对其进行基准测试。令我惊讶的是(与答案对 Which is fast
我有一个程序必须在硬件允许的情况下尽快发出数千个 http 请求。在现实世界中,这些连接中的每一个都将连接到一个离散的服务器,但我已经编写了一个测试程序来帮助我模拟负载(希望如此)。 我的程序使用 A
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我正在计算 Clojure 中 3d 点云的边界框。点云表示为 Java 原始浮点数组,点云中的每个点都使用 4 个浮点存储,其中最后一个浮点未使用。像这样: [x0 y0 z0 u0 x1 y1
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我正在尝试使用rayshader 包制作图像。我很高兴能够使用如下代码创建一个 png 文件: library(ggplot2) library(rayshader) example_plot <-
更新 显然,jQuery 模板可以被编译,并且它有助于显示带有 if 语句 的模板的性能 here . 但是如图here ,预编译的 jQuery 模板对我的情况没有多大作用,因为我的模板不包含逻辑
我是编程新手。我有一个启用分页的 ScrollView ,其中包含许多页面(最多十个),并且在每个页面上都有一个自定义按钮。每个自定义按钮都有一个自定义图像。我在 Interface Builder
我是一名优秀的程序员,十分优秀!