Commit 69fe1411 by 朱瑞泽

test

parent fc443230
......@@ -117,7 +117,7 @@
<classesDirectory>target/classes/</classesDirectory>
<archive>
<manifest>
<mainClass>com.gic.commons.DubboMain</mainClass>
<mainClass>com.gic.demo.single.TestRun</mainClass>
<!-- 打包时 MANIFEST.MF文件不记录的时间戳版本 -->
<useUniqueVersions>false</useUniqueVersions>
<addClasspath>true</addClasspath>
......
package com.gic.demo.single;
import com.gic.demo.single.service.DemoStoreService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
/**
* @author zhurz
*/
@Component
public class TestRun {
@Value("${jdbc.maxsize}")
private Integer jdbcMaxsize;
public void test(Consumer<String> consumer) throws InterruptedException {
int maxThreadSize = 100;
int size = 100000;
ExecutorService exec = Executors.newFixedThreadPool(maxThreadSize);
CountDownLatch countDownLatch = new CountDownLatch(size);
long begin = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
int finalI = i;
exec.execute(() -> {
try {
consumer.accept("sss" + finalI);
} finally {
countDownLatch.countDown();
}
});
}
countDownLatch.await();
long now = System.currentTimeMillis();
double cost = ((now - begin) / 1000d);
System.out.printf(
"并发线程: %d, jdbc最大连接: %d, 查询次数: %d, 耗时:%.2f s, QPS: %.2f\n",
maxThreadSize,
jdbcMaxsize,
size,
cost,
size / cost
);
}
public static void main(String[] args) throws InterruptedException {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-conf.xml");
TestRun bean = applicationContext.getBean(TestRun.class);
DemoStoreService demoStoreService = applicationContext.getBean(DemoStoreService.class);
System.out.println("无事务");
bean.test(demoStoreService::test1);
System.out.println("开启事务");
bean.test(demoStoreService::test2);
System.out.println("只读事务");
bean.test(demoStoreService::test3);
}
}
......@@ -17,4 +17,7 @@ public interface DemoStoreService {
*/
TabGicDemoStore findDemoStoreById(String storeId);
void test1(String storeId);
void test2(String storeId);
void test3(String storeId);
}
......@@ -6,6 +6,7 @@ import com.gic.demo.single.service.DemoStoreService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
......@@ -26,10 +27,29 @@ public class DemoStoreServiceImpl implements DemoStoreService {
* @return
*/
@Override
// @Transactional()
// @Transactional(readOnly = true)
public TabGicDemoStore findDemoStoreById(String storeId) {
if (StringUtils.isBlank(storeId)) {
return null;
}
return mapper.selectByPrimaryKey(storeId);
}
@Override
public void test1(String storeId) {
mapper.selectByPrimaryKey(storeId);
}
@Override
@Transactional
public void test2(String storeId) {
mapper.selectByPrimaryKey(storeId);
}
@Override
@Transactional(readOnly = true)
public void test3(String storeId) {
mapper.selectByPrimaryKey(storeId);
}
}
......@@ -10,6 +10,7 @@ import com.gic.demo.single.service.DemoStoreService;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* @author zhurz
......@@ -27,6 +28,7 @@ public class DemoStoreApiServiceImpl implements DemoStoreApiService {
* @return
*/
@Override
@Transactional
public ServiceResponse<DemoStoreDTO> findDemoStoreById(String storeId) {
if (RandomUtils.nextInt(0, 10) > 5) {
return ServiceResponse.failure(DemoSinglApiConstant.ERROR, "服务异常");
......
......@@ -4,24 +4,24 @@
xmlns:context="http://www.springframework.org/schema/context"
xmlns:apollo="http://www.ctrip.com/schema/apollo"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.ctrip.com/schema/apollo
http://www.ctrip.com/schema/apollo.xsd">
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.ctrip.com/schema/apollo http://www.ctrip.com/schema/apollo.xsd
http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<context:annotation-config/>
<apollo:config namespaces="COMMON.sharding,COMMON.jdbc"/>
<!-- <apollo:config namespaces="COMMON.sharding,COMMON.jdbc"/>-->
<bean id="dataSource" class="com.gic.sharding.sdk.ShardingDatasource" init-method="init">
<property name="shardingId" value="${sharding.shardingId}"/>
<property name="maxSize" value="${jdbc.maxsize}"/>
<property name="urlType" value="${sharding.urlType:inner}"/>
</bean>
<!-- <bean id="dataSource" class="com.gic.sharding.sdk.ShardingDatasource" init-method="init">-->
<!-- <property name="shardingId" value="${sharding.shardingId}"/>-->
<!-- <property name="maxSize" value="${jdbc.maxsize}"/>-->
<!-- <property name="urlType" value="${sharding.urlType:inner}"/>-->
<!-- </bean>-->
<import resource="classpath:applicationContext-db-only.xml"/>
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<property name="dataSource" ref="dataSource"/>
......@@ -56,4 +56,30 @@
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
\ No newline at end of file
<!-- <tx:advice id="txAdvice" transaction-manager="txManager">-->
<!-- <tx:attributes>-->
<!-- &lt;!&ndash; 设置所有匹配的方法,然后设置传播级别和事务隔离 &ndash;&gt;-->
<!-- &lt;!&ndash; 增 &ndash;&gt;-->
<!-- <tx:method name="insert*" rollback-for="java.lang.Exception"/>-->
<!-- <tx:method name="save*" rollback-for="java.lang.Exception"/>-->
<!-- <tx:method name="add*" rollback-for="java.lang.Exception"/>-->
<!-- &lt;!&ndash; 删 &ndash;&gt;-->
<!-- <tx:method name="delete*" rollback-for="java.lang.Exception"/>-->
<!-- <tx:method name="remove*" rollback-for="java.lang.Exception"/>-->
<!-- &lt;!&ndash; 改 &ndash;&gt;-->
<!-- <tx:method name="update*" rollback-for="java.lang.Exception"/>-->
<!-- &lt;!&ndash; 查 &ndash;&gt;-->
<!-- <tx:method name="get*" read-only="true"/>-->
<!-- <tx:method name="find*" read-only="true"/>-->
<!-- <tx:method name="count*" read-only="true"/>-->
<!-- <tx:method name="list*" read-only="true"/>-->
<!-- <tx:method name="query*" read-only="true"/>-->
<!-- </tx:attributes>-->
<!-- </tx:advice>-->
<!-- <aop:config>-->
<!-- <aop:pointcut id="txPointcut" expression="execution(public * com.gic.demo.single.service..*.*(..))" />-->
<!-- <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />-->
<!-- </aop:config>-->
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="error" packages="com.gic.commons.log">
<appenders>
<!--这个输出控制台的配置-->
<Console name="Console" target="SYSTEM_OUT">
<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
<ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY" />
<!--这个都知道是输出日志的格式-->
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c:%L] [%traceId] %m%n" />
</Console>
</appenders>
<loggers>
<!--建立一个默认的root的logger-->
<!-- <Logger name="com.gic" level="DEBUG"/>-->
<Logger name="com.gic" level="WARN"/>
<!-- <Logger name="org.springframework.jdbc.datasource.DataSourceTransactionManager" level="DEBUG"/>-->
<Logger name="org.springframework.jdbc.datasource.DataSourceTransactionManager" level="WARN"/>
<logger name="Sharding-JDBC-SQL" level="INFO"/>
<Root level="WARN">
<!-- <Root level="DEBUG">-->
<AppenderRef ref="Console" />
</Root>
</loggers>
</configuration>
......@@ -6,13 +6,19 @@ import com.gic.demo.single.dao.mapper.DemoEnterpriseMapper;
import com.gic.demo.single.dao.mapper.DemoStoreMapper;
import com.gic.demo.single.entity.TabGicDemoEnterprise;
import com.gic.demo.single.entity.TabGicDemoStore;
import com.gic.demo.single.service.DemoStoreService;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Unit test for simple App.
*/
......@@ -26,6 +32,9 @@ public class AppTest {
@Autowired
private DemoStoreMapper storeMapper;
@Autowired
private DemoStoreService demoStoreService;
@Test
public void test1() {
TabGicDemoEnterprise tabGicDemoEnterprise = enterpriseMapper.selectByPrimaryKey("22323423");
......@@ -52,4 +61,44 @@ public class AppTest {
}
}
@Test
public void test4() {
TabGicDemoStore store = demoStoreService.findDemoStoreById("001d854981019000");
System.out.println(JSON.toJSONString(store, true));
}
@Value("${jdbc.maxsize}")
private Integer jdbcMaxsize;
@Test
public void test5() throws InterruptedException {
int maxThreadSize = 100;
int size = 50000;
ExecutorService exec = Executors.newFixedThreadPool(maxThreadSize);
CountDownLatch countDownLatch = new CountDownLatch(size);
long begin = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
int finalI = i;
exec.execute(() -> {
try {
demoStoreService.findDemoStoreById("sss" + finalI);
} finally {
countDownLatch.countDown();
}
});
}
countDownLatch.await();
long now = System.currentTimeMillis();
double cost = ((now - begin) / 1000d);
System.out.printf(
"并发线程: %d, jdbc最大连接: %d, 查询次数: %d, 耗时:%.2f s, QPS: %.2f",
maxThreadSize,
jdbcMaxsize,
size,
cost,
size / cost
);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment