|
|
@@ -1,7 +1,7 @@
|
|
|
#!/usr/bin/env php
|
|
|
<?php
|
|
|
/**
|
|
|
- * Concurrently calls app-ghs order/create-order and stock-out/create-order,
|
|
|
+ * Concurrently calls app-ghs stock-changing APIs,
|
|
|
* then compares xhGhsItem.stock with the expected stock after successful calls.
|
|
|
*
|
|
|
* WARNING: this script sends real requests to api.shop.hzghd.com and creates
|
|
|
@@ -10,7 +10,8 @@
|
|
|
* Usage:
|
|
|
* php scripts/test_ghs_stock_concurrency.php
|
|
|
* php scripts/test_ghs_stock_concurrency.php --rounds=10
|
|
|
- * php scripts/test_ghs_stock_concurrency.php --order=2 --stock-out=2 --rounds=3
|
|
|
+ * php scripts/test_ghs_stock_concurrency.php --order=2 --stock-out=2 --wastage=2 --hd-purchase=2 --rounds=3
|
|
|
+ * php scripts/test_ghs_stock_concurrency.php --stock-in-confirm=RK123 --stock-out-cancel=CK123 --purchase-put-in=123
|
|
|
*/
|
|
|
|
|
|
require(__DIR__ . '/../vendor/autoload.php');
|
|
|
@@ -34,13 +35,16 @@ if (!extension_loaded('curl')) {
|
|
|
exit(1);
|
|
|
}
|
|
|
|
|
|
-$options = getopt('', ['order::', 'stock-out::', 'rounds::']);
|
|
|
-$orderCount = normalizePositiveInt($options, 'order', 1);
|
|
|
-$stockOutCount = normalizePositiveInt($options, 'stock-out', 1);
|
|
|
+$options = getopt('', ['order::', 'stock-out::', 'wastage::', 'hd-purchase::', 'stock-in-confirm::', 'stock-out-cancel::', 'purchase-put-in::', 'rounds::']);
|
|
|
+$orderCount = normalizeNonNegativeInt($options, 'order', 1);
|
|
|
+$stockOutCount = normalizeNonNegativeInt($options, 'stock-out', 1);
|
|
|
+$wastageCount = normalizeNonNegativeInt($options, 'wastage', 1);
|
|
|
+$hdPurchaseCount = normalizeNonNegativeInt($options, 'hd-purchase', 1);
|
|
|
$rounds = normalizePositiveInt($options, 'rounds', 1);
|
|
|
|
|
|
$baseUrl = 'http://api.shop.hzghd.com';
|
|
|
-$productIds = [27286, 27282];
|
|
|
+$hdBaseUrl = 'http://api.shop.huaml.com';
|
|
|
+$defaultProductIds = [27286, 27282];
|
|
|
|
|
|
$commonHeaders = [
|
|
|
'Connection: keep-alive',
|
|
|
@@ -57,32 +61,76 @@ $commonHeaders = [
|
|
|
'Accept-Language: zh-CN,zh;q=0.9',
|
|
|
];
|
|
|
|
|
|
+$hdHeaders = [
|
|
|
+ 'Connection: keep-alive',
|
|
|
+ 'account: 12362',
|
|
|
+ 'content-type: application/x-www-form-urlencoded;charset=UTF-8',
|
|
|
+ 'User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1 wechatdevtools/2.01.2510260 MicroMessenger/8.0.5 Language/zh_CN webview/ hash/1009552471 sid/xxB6sCFvvu',
|
|
|
+ 'token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImp0aSI6IjBfMTM5MSJ9.eyJpc3MiOiJodHRwczpcL1wvYXBpLnNob3AuaHVhbWwuY29tIiwiYXVkIjoiaHR0cHM6XC9cL2FwaS5zaG9wLmh1YW1sLmNvbSIsImp0aSI6IjBfMTM5MSIsImlhdCI6MTc4MDAzNzMxMSwibmJmIjoxNzgwMDM3MzExLCJleHAiOjE4NzQ2NDUzMTEsInVuaXF1ZUlkIjoiMTM5MSIsInNvdXJjZUlkIjowfQ.BgiDsxOd9cktOLsDe8Ud_hcFqzmVwSzKjCVwUdrSXKw',
|
|
|
+ 'appVersion: 2',
|
|
|
+ 'Accept: */*',
|
|
|
+ 'Sec-Fetch-Site: cross-site',
|
|
|
+ 'Sec-Fetch-Mode: cors',
|
|
|
+ 'Sec-Fetch-Dest: empty',
|
|
|
+ 'Referer: https://servicewechat.com/wxe4675bab299a52f7/devtools/page-frame.html',
|
|
|
+ 'Accept-Language: zh-CN,zh;q=0.9',
|
|
|
+];
|
|
|
+
|
|
|
$orderBody = 'shopId=12986&shopName=&sendType=0&transType=4&sendDate=&sendTimeWant=&historyDate=&sendCost=&customSendCost=0&customId=914&packCost=&product=%5B%7B%22productId%22%3A%2227286%22%2C%22bigNum%22%3A2%2C%22smallNum%22%3A0%2C%22classId%22%3A%223322%22%2C%22itemId%22%3A%221009%22%2C%22price%22%3A%2227.00%22%2C%22remark%22%3A%22%22%7D%2C%7B%22productId%22%3A%2227282%22%2C%22bigNum%22%3A2%2C%22smallNum%22%3A0%2C%22classId%22%3A%223322%22%2C%22itemId%22%3A%221005%22%2C%22price%22%3A%220.30%22%2C%22remark%22%3A%22%22%7D%5D&remark=&payWay=0&needPrint=1&hasPay=2&getStaffId=0&getStaffName=&dealPrice=0&wlName=&reductionRule=0&callErrand=0&weight=1&deliveryRemark=&customName=%E5%BE%AE%E5%8D%87%E6%BC%AB%E8%8A%B1&modifyPrice=54.6&newVersion=1&book=0&xj=%22%22&treeData=%22%22';
|
|
|
$stockOutBody = 'confirmIn=1&date=2026-05-25&remark=123&inShopId=37043&itemInfo=%5B%7B%22productId%22%3A%2227286%22%2C%22bigNum%22%3A1%2C%22smallNum%22%3A0%2C%22name%22%3A%22%E8%8B%8F%E9%86%92%22%7D%2C%7B%22productId%22%3A%2227282%22%2C%22bigNum%22%3A1%2C%22smallNum%22%3A0%2C%22name%22%3A%22%E6%B4%9B%E7%A5%9E%22%7D%5D&action=confirm';
|
|
|
+$hdPurchaseBody = 'getType=2&sendTimeWant=&sendType=0&transType=4&remark=&product=%5B%7B%22productId%22%3A%2227286%22%2C%22bigNum%22%3A1%2C%22smallNum%22%3A0%2C%22classId%22%3A%223322%22%2C%22itemId%22%3A%221009%22%2C%22weight%22%3A%221.20%22%2C%22name%22%3A%22%E8%8B%8F%E9%86%92%22%7D%5D&ghsId=932&hbId=0&wlName=&xj=%22%22&itemTotalAmount=27&version=10&direct=0';
|
|
|
+$wastageBody = http_build_query([
|
|
|
+ 'remark' => 'concurrency-test',
|
|
|
+ 'product' => json_encode([
|
|
|
+ ['productId' => '27286', 'bigNum' => 1, 'smallNum' => 0, 'classId' => '3322', 'itemId' => '1009'],
|
|
|
+ ['productId' => '27282', 'bigNum' => 1, 'smallNum' => 0, 'classId' => '3322', 'itemId' => '1005'],
|
|
|
+ ], JSON_UNESCAPED_UNICODE),
|
|
|
+]);
|
|
|
|
|
|
$requestDefs = [
|
|
|
'order' => [
|
|
|
+ 'method' => 'POST',
|
|
|
'url' => $baseUrl . '/order/create-order',
|
|
|
'body' => $orderBody,
|
|
|
'stockDelta' => [27286 => '2', 27282 => '2'],
|
|
|
],
|
|
|
'stock-out' => [
|
|
|
+ 'method' => 'POST',
|
|
|
'url' => $baseUrl . '/stock-out/create-order',
|
|
|
'body' => $stockOutBody,
|
|
|
'stockDelta' => [27286 => '1', 27282 => '1'],
|
|
|
],
|
|
|
+ 'wastage' => [
|
|
|
+ 'method' => 'POST',
|
|
|
+ 'url' => $baseUrl . '/wastage/create-order',
|
|
|
+ 'body' => $wastageBody,
|
|
|
+ 'stockDelta' => [27286 => '1', 27282 => '1'],
|
|
|
+ ],
|
|
|
+ 'hd-purchase' => [
|
|
|
+ 'method' => 'POST',
|
|
|
+ 'url' => $hdBaseUrl . '/purchase/create-order',
|
|
|
+ 'headers' => $hdHeaders,
|
|
|
+ 'body' => $hdPurchaseBody,
|
|
|
+ 'stockDelta' => [27286 => '1'],
|
|
|
+ ],
|
|
|
];
|
|
|
+$requestCounts = ['order' => $orderCount, 'stock-out' => $stockOutCount, 'wastage' => $wastageCount, 'hd-purchase' => $hdPurchaseCount];
|
|
|
+addParameterizedRequests($requestDefs, $requestCounts, $options, $baseUrl);
|
|
|
+$productIds = collectProductIds($requestDefs, $defaultProductIds);
|
|
|
|
|
|
echo "app-ghs stock concurrency test\n";
|
|
|
-echo "Target: {$baseUrl}\n";
|
|
|
-echo "Rounds: {$rounds}, order/create-order: {$orderCount}, stock-out/create-order: {$stockOutCount}\n";
|
|
|
+echo "Targets: {$baseUrl}, {$hdBaseUrl}\n";
|
|
|
+echo "Rounds: {$rounds}, order/create-order: {$orderCount}, stock-out/create-order: {$stockOutCount}, wastage/create-order: {$wastageCount}, app-hd purchase/create-order: {$hdPurchaseCount}\n";
|
|
|
echo "Products: " . implode(', ', $productIds) . "\n";
|
|
|
echo "WARNING: real requests will create real orders and decrease real stock.\n\n";
|
|
|
|
|
|
$overallPass = true;
|
|
|
$overallInvalid = false;
|
|
|
$overallConcurrencyError = false;
|
|
|
-$appLogFile = __DIR__ . '/../app-ghs/runtime/logs/app.log';
|
|
|
+$appLogFiles = [
|
|
|
+ 'app-ghs' => __DIR__ . '/../app-ghs/runtime/logs/app.log',
|
|
|
+ 'app-hd' => __DIR__ . '/../app-hd/runtime/logs/app.log',
|
|
|
+];
|
|
|
|
|
|
for ($round = 1; $round <= $rounds; $round++) {
|
|
|
echo "========== Round {$round}/{$rounds} ==========\n";
|
|
|
@@ -94,11 +142,10 @@ for ($round = 1; $round <= $rounds; $round++) {
|
|
|
$currentRequestDefs['order']['body'] = refreshOrderBodyPrices($orderBody, $priceSummary);
|
|
|
echo "Order request pricing: {$priceSummary}\n";
|
|
|
|
|
|
- $requests = buildRequests($currentRequestDefs, $orderCount, $stockOutCount);
|
|
|
- $logOffset = getFileSize($appLogFile);
|
|
|
+ $requests = buildRequests($currentRequestDefs, $requestCounts);
|
|
|
+ $logOffsets = getLogOffsets($appLogFiles);
|
|
|
$responses = runConcurrentRequests($requests, $commonHeaders);
|
|
|
- $newLog = readFileFromOffset($appLogFile, $logOffset);
|
|
|
- $concurrencyErrors = findConcurrencyErrors($newLog);
|
|
|
+ $concurrencyErrors = findConcurrencyErrorsFromLogs($appLogFiles, $logOffsets);
|
|
|
|
|
|
foreach ($responses as $response) {
|
|
|
$ok = $response['businessSuccess'] ? 'OK' : 'ERR';
|
|
|
@@ -185,6 +232,19 @@ function normalizePositiveInt($options, $key, $default)
|
|
|
return $value;
|
|
|
}
|
|
|
|
|
|
+function normalizeNonNegativeInt($options, $key, $default)
|
|
|
+{
|
|
|
+ if (!isset($options[$key]) || $options[$key] === false || $options[$key] === '') {
|
|
|
+ return $default;
|
|
|
+ }
|
|
|
+ $value = (int)$options[$key];
|
|
|
+ if ($value < 0) {
|
|
|
+ fwrite(STDERR, "ERROR: --{$key} must be a non-negative integer.\n");
|
|
|
+ exit(1);
|
|
|
+ }
|
|
|
+ return $value;
|
|
|
+}
|
|
|
+
|
|
|
function loadStocks($productIds)
|
|
|
{
|
|
|
$rows = Yii::$app->db->createCommand(
|
|
|
@@ -202,26 +262,95 @@ function loadStocks($productIds)
|
|
|
return $stocks;
|
|
|
}
|
|
|
|
|
|
-function buildRequests($requestDefs, $orderCount, $stockOutCount)
|
|
|
+function buildRequests($requestDefs, $requestCounts)
|
|
|
{
|
|
|
$requests = [];
|
|
|
- for ($i = 1; $i <= $orderCount; $i++) {
|
|
|
- $requests[] = [
|
|
|
- 'type' => 'order',
|
|
|
- 'label' => 'order#' . $i,
|
|
|
- 'url' => $requestDefs['order']['url'],
|
|
|
- 'body' => $requestDefs['order']['body'],
|
|
|
+ foreach ($requestCounts as $type => $count) {
|
|
|
+ if (empty($requestDefs[$type]) || $count <= 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ for ($i = 1; $i <= $count; $i++) {
|
|
|
+ $requests[] = [
|
|
|
+ 'type' => $type,
|
|
|
+ 'label' => $type . '#' . $i,
|
|
|
+ 'method' => isset($requestDefs[$type]['method']) ? $requestDefs[$type]['method'] : 'POST',
|
|
|
+ 'url' => $requestDefs[$type]['url'],
|
|
|
+ 'headers' => isset($requestDefs[$type]['headers']) ? $requestDefs[$type]['headers'] : null,
|
|
|
+ 'body' => isset($requestDefs[$type]['body']) ? $requestDefs[$type]['body'] : '',
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $requests;
|
|
|
+}
|
|
|
+
|
|
|
+function addParameterizedRequests(&$requestDefs, &$requestCounts, $options, $baseUrl)
|
|
|
+{
|
|
|
+ if (!empty($options['stock-in-confirm'])) {
|
|
|
+ $orderSn = (string)$options['stock-in-confirm'];
|
|
|
+ $requestDefs['stock-in-confirm'] = [
|
|
|
+ 'method' => 'GET',
|
|
|
+ 'url' => $baseUrl . '/stock-in/confirm-order?orderSn=' . rawurlencode($orderSn),
|
|
|
+ 'body' => '',
|
|
|
+ 'stockDelta' => loadItemNumDelta('xhGhsStockInOrderItem', $orderSn, -1),
|
|
|
];
|
|
|
+ $requestCounts['stock-in-confirm'] = 1;
|
|
|
}
|
|
|
- for ($i = 1; $i <= $stockOutCount; $i++) {
|
|
|
- $requests[] = [
|
|
|
- 'type' => 'stock-out',
|
|
|
- 'label' => 'stock-out#' . $i,
|
|
|
- 'url' => $requestDefs['stock-out']['url'],
|
|
|
- 'body' => $requestDefs['stock-out']['body'],
|
|
|
+
|
|
|
+ if (!empty($options['stock-out-cancel'])) {
|
|
|
+ $orderSn = (string)$options['stock-out-cancel'];
|
|
|
+ $requestDefs['stock-out-cancel'] = [
|
|
|
+ 'method' => 'GET',
|
|
|
+ 'url' => $baseUrl . '/stock-out/cancel-order?orderSn=' . rawurlencode($orderSn),
|
|
|
+ 'body' => '',
|
|
|
+ 'stockDelta' => loadItemNumDelta('xhGhsStockOutOrderItem', $orderSn, -1),
|
|
|
];
|
|
|
+ $requestCounts['stock-out-cancel'] = 1;
|
|
|
}
|
|
|
- return $requests;
|
|
|
+
|
|
|
+ if (!empty($options['purchase-put-in'])) {
|
|
|
+ $purchaseId = intval($options['purchase-put-in']);
|
|
|
+ $orderSn = Yii::$app->db->createCommand('select orderSn from xhGhsCgOrder where id=:id', [':id' => $purchaseId])->queryScalar();
|
|
|
+ if (!empty($orderSn)) {
|
|
|
+ $requestDefs['purchase-put-in'] = [
|
|
|
+ 'method' => 'GET',
|
|
|
+ 'url' => $baseUrl . '/purchase-order/confirm-put-in?id=' . $purchaseId,
|
|
|
+ 'body' => '',
|
|
|
+ 'stockDelta' => loadItemNumDelta('xhGhsCgOrderItem', $orderSn, -1),
|
|
|
+ ];
|
|
|
+ $requestCounts['purchase-put-in'] = 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function loadItemNumDelta($tableName, $orderSn, $direction)
|
|
|
+{
|
|
|
+ $rows = Yii::$app->db->createCommand(
|
|
|
+ "select productId, itemNum from {$tableName} where orderSn=:orderSn",
|
|
|
+ [':orderSn' => $orderSn]
|
|
|
+ )->queryAll();
|
|
|
+ $delta = [];
|
|
|
+ foreach ($rows as $row) {
|
|
|
+ $productId = intval($row['productId']);
|
|
|
+ $itemNum = (string)$row['itemNum'];
|
|
|
+ if ($direction < 0) {
|
|
|
+ $itemNum = bcmul($itemNum, '-1', 2);
|
|
|
+ }
|
|
|
+ $delta[$productId] = isset($delta[$productId]) ? bcadd($delta[$productId], $itemNum, 2) : $itemNum;
|
|
|
+ }
|
|
|
+ return $delta;
|
|
|
+}
|
|
|
+
|
|
|
+function collectProductIds($requestDefs, $defaultProductIds)
|
|
|
+{
|
|
|
+ $ids = $defaultProductIds;
|
|
|
+ foreach ($requestDefs as $requestDef) {
|
|
|
+ foreach (array_keys($requestDef['stockDelta']) as $productId) {
|
|
|
+ $ids[] = intval($productId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $ids = array_values(array_unique(array_filter($ids)));
|
|
|
+ sort($ids);
|
|
|
+ return $ids;
|
|
|
}
|
|
|
|
|
|
function refreshOrderBodyPrices($orderBody, &$summary)
|
|
|
@@ -297,15 +426,20 @@ function runConcurrentRequests($requests, $headers)
|
|
|
|
|
|
foreach ($requests as $index => $request) {
|
|
|
$ch = curl_init($request['url']);
|
|
|
- curl_setopt_array($ch, [
|
|
|
- CURLOPT_POST => true,
|
|
|
- CURLOPT_POSTFIELDS => $request['body'],
|
|
|
- CURLOPT_HTTPHEADER => $headers,
|
|
|
+ $method = isset($request['method']) ? strtoupper($request['method']) : 'POST';
|
|
|
+ $requestHeaders = !empty($request['headers']) ? $request['headers'] : $headers;
|
|
|
+ $curlOptions = [
|
|
|
+ CURLOPT_HTTPHEADER => $requestHeaders,
|
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
|
CURLOPT_ENCODING => '',
|
|
|
CURLOPT_CONNECTTIMEOUT => 10,
|
|
|
CURLOPT_TIMEOUT => 60,
|
|
|
- ]);
|
|
|
+ ];
|
|
|
+ if ($method === 'POST') {
|
|
|
+ $curlOptions[CURLOPT_POST] = true;
|
|
|
+ $curlOptions[CURLOPT_POSTFIELDS] = $request['body'];
|
|
|
+ }
|
|
|
+ curl_setopt_array($ch, $curlOptions);
|
|
|
curl_multi_add_handle($multi, $ch);
|
|
|
$handles[$index] = [
|
|
|
'handle' => $ch,
|
|
|
@@ -395,8 +529,11 @@ function parseBusinessResponse($body, $httpCode, $curlError, $requestType)
|
|
|
|
|
|
function countSuccessfulRequests($responses)
|
|
|
{
|
|
|
- $counts = ['order' => 0, 'stock-out' => 0];
|
|
|
+ $counts = [];
|
|
|
foreach ($responses as $response) {
|
|
|
+ if (!isset($counts[$response['type']])) {
|
|
|
+ $counts[$response['type']] = 0;
|
|
|
+ }
|
|
|
if ($response['businessSuccess']) {
|
|
|
$counts[$response['type']]++;
|
|
|
}
|
|
|
@@ -467,6 +604,27 @@ function getFileSize($file)
|
|
|
return is_file($file) ? filesize($file) : 0;
|
|
|
}
|
|
|
|
|
|
+function getLogOffsets($files)
|
|
|
+{
|
|
|
+ $offsets = [];
|
|
|
+ foreach ($files as $name => $file) {
|
|
|
+ $offsets[$name] = getFileSize($file);
|
|
|
+ }
|
|
|
+ return $offsets;
|
|
|
+}
|
|
|
+
|
|
|
+function findConcurrencyErrorsFromLogs($files, $offsets)
|
|
|
+{
|
|
|
+ $errors = [];
|
|
|
+ foreach ($files as $name => $file) {
|
|
|
+ $offset = isset($offsets[$name]) ? $offsets[$name] : 0;
|
|
|
+ foreach (findConcurrencyErrors(readFileFromOffset($file, $offset)) as $error) {
|
|
|
+ $errors[] = $name . ': ' . $error;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $errors;
|
|
|
+}
|
|
|
+
|
|
|
function readFileFromOffset($file, $offset)
|
|
|
{
|
|
|
if (!is_file($file)) {
|