009--【秒殺】進行秒殺壓測

1、寫作背景

在進行秒殺的時候設(shè)置了單用戶不能進行重復秒殺的規(guī)則,所以在進行秒殺的時候要模擬多用戶場景

2、學習目的

  • 模擬多用戶參數(shù)
  • 模擬秒殺場景
  • 避免超賣現(xiàn)象

3、數(shù)據(jù)準備

  • 多用戶模擬,要在數(shù)據(jù)庫創(chuàng)建用戶(手機號)

  • 返回對應的token,和手機號進行一對一記錄,生成JMeter的參數(shù)配置文件

  • 創(chuàng)建一個UserUtils工具類,進行用戶創(chuàng)建和模擬登陸

public class UserUtil {
    
    private static void createUser(int count) throws Exception{
        List<LoginUser> users = new ArrayList<>(count);
        //生成用戶
        for(int i=0;i<count;i++) {
            LoginUser user = new LoginUser();
            user.setId(13000000000L+i);
            user.setLoginCount(1);
            user.setNickname("user"+i);
            user.setRegisterDate(new Date());
            user.setSalt("1a2b3c");
            user.setPassword(MD5Utils.inputPassToDbPass("123456", user.getSalt()));
            users.add(user);
        }
        System.out.println("create user");
        //插入數(shù)據(jù)庫
//      Connection conn = DBUtil.getConn();
//      String sql = "insert into login_user(loginCount, nickname, registerDate, salt, password, id)values(?,?,?,?,?,?)";
//      PreparedStatement pstmt = conn.prepareStatement(sql);
//      for(int i=0;i<users.size();i++) {
//          LoginUser user = users.get(i);
//          pstmt.setInt(1, user.getLoginCount());
//          pstmt.setString(2, user.getNickname());
//          pstmt.setTimestamp(3, new Timestamp(user.getRegisterDate().getTime()));
//          pstmt.setString(4, user.getSalt());
//          pstmt.setString(5, user.getPassword());
//          pstmt.setLong(6, user.getId());
//          pstmt.addBatch();
//      }
//      pstmt.executeBatch();
//      pstmt.close();
//      conn.close();
//      System.out.println("insert to db");
        //登錄,生成token
        String urlString = "http://localhost:9091/login/do_login";
        File file = new File("C:\\TT_Enzoism+\\tokens.txt");
        if(file.exists()) {
            file.delete();
        }
        RandomAccessFile raf = new RandomAccessFile(file, "rw");
        file.createNewFile();
        raf.seek(0);
        for(int i=0;i<users.size();i++) {
            LoginUser user = users.get(i);
            URL url = new URL(urlString);
            HttpURLConnection co = (HttpURLConnection)url.openConnection();
            co.setRequestMethod("POST");
            co.setDoOutput(true);
            OutputStream out = co.getOutputStream();
            String params = "mobile="+user.getId()+"&password="+MD5Utils.inputPassToFormPass("123456");
            out.write(params.getBytes());
            out.flush();
            InputStream inputStream = co.getInputStream();
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            byte buff[] = new byte[1024];
            int len = 0;
            while((len = inputStream.read(buff)) >= 0) {
                bout.write(buff, 0 ,len);
            }
            inputStream.close();
            bout.close();
            String response = new String(bout.toByteArray());
            JSONObject jo = JSON.parseObject(response);
            String token = jo.getString("data");
            System.out.println("create token : " + user.getId());
            
            String row = user.getId()+","+token;
            raf.seek(raf.length());
            raf.write(row.getBytes());
            raf.write("\r\n".getBytes());
            System.out.println("write to file : " + user.getId());
        }
        raf.close();
        
        System.out.println("over");
    }
    
    public static void main(String[] args)throws Exception {
        createUser(5000);
    }
}

  • 在JMeter中導入配置用戶信息,然后進行
  • 執(zhí)行結(jié)果有點搞笑了,超賣,這個就是我們要解決的事情
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容