Skip to content

Commit

Permalink
增加清空数据库表和缓存请求,便于 Nginx 部署后进行压力测试
Browse files Browse the repository at this point in the history
  • Loading branch information
gongfukangEE committed Jun 10, 2019
1 parent 1f32f0c commit ee5e007
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 194 deletions.
347 changes: 153 additions & 194 deletions .idea/workspace.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,26 @@ public static void updateStockWithRedis(Stock stock) {
RedisPool.jedisPoolClose(jedis);
}
}

/**
* 重置缓存
*/
public static void initRedisBefore() {
Jedis jedis = null;
try {
jedis = RedisPool.getJedis();
// 开始事务
Transaction transaction = jedis.multi();
// 事务操作
RedisPoolUtil.set(RedisKeysConstant.STOCK_COUNT + 1, "50");
RedisPoolUtil.set(RedisKeysConstant.STOCK_SALE + 1, "0");
RedisPoolUtil.set(RedisKeysConstant.STOCK_VERSION + 1, "0");
// 结束事务
List<Object> list = transaction.exec();
} catch (Exception e) {
log.error("initRedis 获取 Jedis 实例失败:", e);
} finally {
RedisPool.jedisPoolClose(jedis);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.daydreamdev.secondskill.controller;

import com.daydreamdev.secondskill.common.Limit.RedisLimit;
import com.daydreamdev.secondskill.common.StockWithRedis.StockWithRedis;
import com.daydreamdev.secondskill.service.api.OrderService;
import com.daydreamdev.secondskill.service.api.StockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
Expand All @@ -30,6 +32,26 @@ public class IndexController {
@Autowired
private StockService stockService;

/**
* 压测前先请求该方法,初始化数据库和缓存
*/
@RequestMapping(value = "initDBAndRedis", method = RequestMethod.POST)
@ResponseBody
public String initDBAndRedisBefore(HttpServletRequest request) {
int res = 0;
try {
// 初始化库存信息
res = stockService.initDBBefore();
// 清空订单表
res &= (orderService.delOrderDBBefore() == 0 ? 1 : 0);
// 重置缓存
StockWithRedis.initRedisBefore();
} catch (Exception e) {
log.error("Exception: ", e);
}
return res == 1 ? success : error;
}

/**
* 秒杀基本逻辑,存在超卖问题
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
@Mapper
public interface StockMapper {

/**
* 初始化 DB
*/
@Update("UPDATE stock SET count = 50, sale = 0, version = 0")
int initDBBefore();

@Select("SELECT * FROM stock WHERE id = #{id, jdbcType = INTEGER}")
Stock selectByPrimaryKey(@Param("id") int id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.daydreamdev.secondskill.pojo.StockOrder;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Update;

/**
* @auther G.Fukang
Expand All @@ -14,4 +15,11 @@ public interface StockOrderMapper {
@Insert("INSERT INTO stock_order (id, sid, name, create_time) VALUES " +
"(#{id, jdbcType = INTEGER}, #{sid, jdbcType = INTEGER}, #{name, jdbcType = VARCHAR}, #{createTime, jdbcType = TIMESTAMP})")
int insertSelective(StockOrder order);

/**
* 清空订单表
* 成功为 0,失败为 -1
*/
@Update("TRUNCATE TABLE stock_order")
int delOrderDBBefore();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
*/
public interface OrderService {

/**
* 清空订单表
*/
int delOrderDBBefore();

/**
* 创建订单(存在超卖问题)
* @param sid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ public interface StockService {
* 乐观锁更新库存,解决超卖问题
*/
int updateStockByOptimistic(Stock stock);

/**
* 初始化数据库
*/
int initDBBefore();
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public class OrderServiceImpl implements OrderService {
@Autowired
private StockOrderMapper orderMapper;

@Override
public int delOrderDBBefore() {
return orderMapper.delOrderDBBefore();
}

@Override
public int createWrongOrder(int sid) throws Exception {
Stock stock = checkStock(sid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,10 @@ public int updateStockByOptimistic(Stock stock) {

return stockMapper.updateByOptimistic(stock);
}

@Override
public int initDBBefore() {

return stockMapper.initDBBefore();
}
}

0 comments on commit ee5e007

Please sign in to comment.