Forráskód Böngészése

Merge branch 'master' of http://192.168.3.207:10080/group3/SwordFish_Interface_Service

fuwencai 4 éve
szülő
commit
1b0b149940

+ 3 - 3
api/v1/projects.go

@@ -44,7 +44,7 @@ func getProjectsList(c *gin.Context) {
 	if projectName != "" || winner != "" {
 		utils.Check(appID, productID, c, func() ([]map[string]interface{}, int, error) {
 			return service.ProjectListData(productID, appID, projectName, winner, bidTime, false)
-		}, param, requestIP)
+		}, param, requestIP, false)
 	} else {
 		response.FailWithDetailed(response.ParamError, nil, "参数错误", c)
 	}
@@ -65,7 +65,7 @@ func getProjectsListDetail(c *gin.Context) {
 	if winner != "" {
 		utils.Check(appID, productID, c, func() ([]map[string]interface{}, int, error) {
 			return service.ProjectListData(productID, appID, "", winner, "", true)
-		}, param, requestIP)
+		}, param, requestIP, false)
 	}
 }
 
@@ -85,6 +85,6 @@ func getProjectDetail(c *gin.Context) {
 	if id != "" {
 		utils.Check(appID, productID, c, func() ([]map[string]interface{}, int, error) {
 			return service.ProjectDetailData(id)
-		}, param, requestIP)
+		}, param, requestIP, false)
 	}
 }

+ 34 - 0
manage/user/concurrentTest.go

@@ -0,0 +1,34 @@
+package user
+
+import (
+	"log"
+	"sfis/model/response"
+	"sfis/service"
+	"strconv"
+	"sync"
+
+	"github.com/gin-gonic/gin"
+)
+
+//并发测试
+func concurrentTest(c *gin.Context) {
+	count, _ := strconv.Atoi(c.PostForm("count"))
+	appid := c.PostForm("appid")
+	productId, _ := strconv.Atoi(c.PostForm("productId"))
+	var wg = sync.WaitGroup{}
+	pool := make(chan bool, 100000)
+	for i := 0; i < count; i++ {
+		wg.Add(1)
+		pool <- true
+		log.Println(i)
+		go func(i int) {
+			defer func() {
+				wg.Done()
+				<-pool
+			}()
+			service.GetConcurrentData(appid, productId, c)
+		}(i)
+	}
+	wg.Wait()
+	response.Ok(c)
+}

+ 4 - 2
manage/user/user.go

@@ -2,8 +2,6 @@ package user
 
 import (
 	"encoding/json"
-	"github.com/gin-gonic/gin"
-	"go.uber.org/zap"
 	"io/ioutil"
 	"net/http"
 	"sfbase/global"
@@ -14,6 +12,9 @@ import (
 	"sfis/utils"
 	"strconv"
 	"time"
+
+	"github.com/gin-gonic/gin"
+	"go.uber.org/zap"
 )
 
 func DevUserManageRegister(router *gin.Engine) {
@@ -26,6 +27,7 @@ func DevUserManageRegister(router *gin.Engine) {
 		userGroup.POST("/userProductList", userProductList)
 		userGroup.POST("/moneyRecharge", moneyRecharge)
 		userGroup.POST("/productRecharge", productRecharge)
+		userGroup.POST("/concurrentTest", concurrentTest)
 	}
 }
 

+ 3 - 0
router/pageRouter.go

@@ -27,6 +27,7 @@ func login(context *gin.Context) {
 func chooseProductPage(context *gin.Context) {
 	context.HTML(200, "chooseProduct.html", nil)
 }
+
 func createUser(context *gin.Context) {
 	context.HTML(200, "create_user.html", nil)
 }
@@ -42,9 +43,11 @@ func projectDetailsPage(context *gin.Context) {
 func projectListPage(context *gin.Context) {
 	context.HTML(200, "projectList.html", nil)
 }
+
 func projectListDetailPage(context *gin.Context) {
 	context.HTML(200, "projectListDetail.html", nil)
 }
+
 func interfaceListPage(context *gin.Context) {
 	context.HTML(200, "interfaceList.html", nil)
 }

+ 23 - 0
service/concurrentTest.go

@@ -0,0 +1,23 @@
+package service
+
+import (
+	"sfis/utils"
+
+	"github.com/gin-gonic/gin"
+)
+
+func GetConcurrentData(appid string, productId int, c *gin.Context) {
+	if productId == 1000 {
+		utils.Check(appid, productId, c, func() ([]map[string]interface{}, int, error) {
+			return ProjectListData(productId, appid, "河南大学", "", "", false)
+		}, "", "", true)
+	} else if productId == 1001 {
+		utils.Check(appid, productId, c, func() ([]map[string]interface{}, int, error) {
+			return ProjectDetailData("5f6bc00c499cb0822d7e3842")
+		}, "", "", true)
+	} else if productId == 1002 {
+		utils.Check(appid, productId, c, func() ([]map[string]interface{}, int, error) {
+			return ProjectListData(productId, appid, "", "中铁十五局集团有限公司", "", true)
+		}, "", "", true)
+	}
+}

+ 56 - 7
static/templates/login.html

@@ -36,6 +36,14 @@
         .userProTbody tr td {
             text-align: center;
         }
+
+        .test {
+
+            width: 400px;
+            height: 400px;
+        }
+
+
     </style>
     <div>
         <h1 class="title">接口服务平台测试系统</h1>
@@ -70,14 +78,14 @@
                 <thead>
                 <tr class="tableTitle">
                     <th style="width: 5%">序号</th>
-                    <th style="width: 10%">产品ID</th>
+                    <th style="width: 5%">产品ID</th>
                     <th style="width: 25%">用户APPID</th>
                     <th style="width: 5%">剩余量</th>
                     <th style="width: 10%">扣费模式</th>
                     <th style="width: 5%">状态</th>
                     <th style="width: 10%">每日调用次数</th>
                     <th style="width: 10%">单次返回数量</th>
-                    <th style="width: 20%">操作</th>
+                    <th style="width: 25%">操作</th>
                 </tr>
                 </thead>
                 <tbody class="userProTbody">
@@ -88,6 +96,14 @@
 
     </div>
 
+    {{/*<div class="test">
+        <h3 id="demo">并发测试</h3>
+        <input type="text" placeholder="请输入并发量">
+        <button>确定</button>
+    </div>*/}}
+    <div class="result"></div>
+
+
     <script>
         userList()
 
@@ -111,7 +127,7 @@
                                 str += "<td style='width: 100px'>" + item.secret_key + "</td>";
                                 str += "<td style='width: 100px'>" + item.phone + "</td>";
                                 str += "<td style='width: 100px'>" + item.money + "</td>";
-                                str += "<td class='handle' style='width: 100px'><a href=\"/page/moneyRechargePage?appId=" + item.app_id + "\">余额充值</a><a href=\"/page/chooseProductPage?appId="+item.app_id+"\">购买产品</a></td>";
+                                str += "<td class='handle' style='width: 100px'><a href=\"/page/moneyRechargePage?appId=" + item.app_id + "\">余额充值</a><a href=\"/page/chooseProductPage?appId=" + item.app_id + "\">购买产品</a></td>";
 
                                 str += "</tr>";
                             }
@@ -167,20 +183,21 @@
                                 str += "<td>" + item.call_times_limit_day + "</td>";
                                 str += "<td>" + item.data_num_limit_one_times + "</td>";
                                 if (item.product_id === 1000 || item.product_id === 1001) {
-                                    str += "<td class='handle' style='width: 100px ;text-align: left'><a href=\"/page/projectListPage?appId=" + appId + "&key=" + key + "\" >测试接口</a>";
+                                    str += "<td class='handle' style='width: 100px ;'><a href=\"/page/projectListPage?appId=" + appId + "&key=" + key + "\" >测试接口</a>";
                                 } else {
-                                    str += "<td class='handle' style='width: 100px;text-align: left'><a href=\"/page/projectListDetail?appId=" + appId + "&key=" + key + "\"  >测试接口</a>";
+                                    str += "<td class='handle' style='width: 100px;'><a href=\"/page/projectListDetail?appId=" + appId + "&key=" + key + "\"  >测试接口</a>";
                                 }
-                                if (item.cost_model == 0){
+                                if (item.cost_model == 0) {
                                     str += "<a href=\"/page/userRecharge?appId=" + appId + "\"  >剩余量充值</a>"
                                 }
+                                str += "<a onclick=\"myFunction('" + appId + "','" + key + "','" + item.product_id + "')\"  href='javascript:void(0);' style='pointer:hand;'>并发测试</a>"
                                 // else {
                                 //
                                 // }
                                 str += "</td>";
                                 str += "</tr>";
                             }
-                            console.log(str);
+                            // console.log(str);
                             let trBody = $(str);
                             $(".userProTbody").append(trBody);
                         } else {
@@ -194,6 +211,38 @@
             })
         }
 
+        //并发测试点击事件,参数 appid 产品id,并发量
+
+        function myFunction(appId, key, productId) {
+            console.log(appId, key, productId)
+            var num = prompt("请输入并发量", "");
+            if (num != null && num != "") {
+                console.log(num)
+                var param = {
+                    "appid": appId,
+                    "productId": productId,
+                    "count": num,
+                }
+                $.ajax({
+                    url: "/manage/user/userProductList",
+                    type: "POST",
+                    dataType: "json",
+                    async: false,
+                    data: param,
+                    success: function (r) {
+                        // if (r.code === 0) {
+                            alert(JSON.stringify(r))
+                            // $(".result").html(r)
+                        // } else {
+                        //
+                        // }
+                    }
+                })
+
+            }
+        }
+
+
     </script>
     </body>
     </html>

+ 1 - 0
static/templates/moneyRecharge.html

@@ -12,6 +12,7 @@
         <input type="number" id="moneys" placeholder="请输入充值金额">
         <button type="button" id="moneyBtn">提交</button>
     </div>
+    <div id="resultContent"></div>
 
     </body>
     </html>

+ 4 - 23
utils/api_util.go

@@ -18,7 +18,7 @@ import (
 	"go.uber.org/zap"
 )
 
-func Check(appID string, productID int, c *gin.Context, getData func() ([]map[string]interface{}, int, error), param, ip string) {
+func Check(appID string, productID int, c *gin.Context, getData func() ([]map[string]interface{}, int, error), param, ip string, concurrentTest bool) {
 	lock.MainLock.Lock()
 	userLock := lock.UserLockMap[appID]
 	lock.MainLock.Unlock()
@@ -65,27 +65,6 @@ func Check(appID string, productID int, c *gin.Context, getData func() ([]map[st
 	product := GetProductByID(productID)
 	userLock.Lock()
 	log.Println(param + "锁住......")
-	//
-	// ctx, _ := context.WithTimeout(c, 20*time.Second)
-	// go func() {
-	// 	var i = 0
-	// 	for {
-	// 		select {
-	// 		case <-ctx.Done():
-	// 			userLock.Unlock()
-	// 			return
-	// 		case <-time.After(time.Second * time.Duration(2)):
-	// 			i++
-	// 			if i == 10 {
-	// 				userLock.Unlock()
-	// 				return
-	// 			}
-	// 			continue
-	// 		}
-	// 	}
-	// }()
-	// time.Sleep(time.Second * time.Duration(25))
-	//
 	db.GetSFISDB().First(userProduct, &model.UserProduct{AppID: appID, ProductID: productID})
 	// costModel = 0
 	switch costModel {
@@ -112,7 +91,9 @@ func Check(appID string, productID int, c *gin.Context, getData func() ([]map[st
 		})
 		limittodaykey := fmt.Sprintf("limittoday_%d_%d_%s", time.Now().Day(), userProduct.ProductID, userProduct.AppID)
 		redis.Incr("limit", limittodaykey)
-		response.FailWithDetailed(response.SUCCESS, datas, "OK", c)
+		if !concurrentTest {
+			response.FailWithDetailed(response.SUCCESS, datas, "OK", c)
+		}
 	} else {
 		if strings.Contains(errStr, "不足") {
 			response.FailWithDetailed(response.LeftNumEmpty, nil, errStr, c)

+ 1 - 0
utils/cost_by_account_balance.go

@@ -44,6 +44,7 @@ func costByAccountBalance(getData func() ([]map[string]interface{}, int, error),
 			datas = data
 			if len(datas) == 0 {
 				global.Logger.Info("无数据", getUserProductError(appID, productID, err)...)
+				orderCode, err = afterCheck(len(data), payMoney, userProduct, statusCode, param, ip)
 			} else {
 				orderCode, err = afterCheck(len(data), payMoney, userProduct, statusCode, param, ip)
 			}