-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
executable file
·110 lines (96 loc) · 3.73 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const SockJS = require('sockjs-client');
const { Client } = require('@stomp/stompjs');
const { v4: uuidv4 } = require('uuid');
const axios = require('axios');
const roomId = "1";
const serverUrl = 'http://localhost:8097/ws';
const apiClientRuningService = axios.create({
baseURL: 'http://localhost:8097', // 根据你的API服务URL调整
});
const axiosRetry = require('axios-retry');
const connections = [];
const moveInterval = 600000; // 移动消息发送间隔
const numberOfUsers = 1000; // concurrent users
function createClient(userId, index) {
const socket = new SockJS(serverUrl);
const client = new Client({
webSocketFactory: () => socket,
debug: (str) => console.log("__bgin_connect:"+str),
onConnect: async () => {
console.log(`User ${index + 1} (${userId}) connected`);
const player = {
id: userId,
segments: [{ x: Math.random() * 500, y: Math.random() * 500 }],
color: '#FF0000',
score: 0,
username: `user${index + 1}`,
nickname: `User_${index + 1}`,
type: 'snake'
};
try {
const response = await apiClientRuningService.post(`/api/rooms/${roomId}/players`, player);
console.log(`Player ${userId} added to room ${roomId} via API.`);
if (client.connected) {
client.publish({
destination: `/app/game/${roomId}/add`,
body: JSON.stringify(player)
});
} else {
console.error(`STOMP client not connected for user ${userId}`);
}
} catch (error) {
console.error(`Failed to add player ${player} to room ${roomId}:`, error);
throw error;
}
setInterval(() => {
if (client.connected) {
client.publish({
destination: `/app/game/${roomId}/move`,
body: JSON.stringify({
id: userId,
head: {
x: Math.random() * 500,
y: Math.random() * 500
},
type: 'snake',
timestamp: Date.now()
})
});
} else {
console.error(`STOMP client not connected for user ${userId}`);
}
}, moveInterval);
},
onStompError: (frame) => {
console.error('STOMP error:', frame);
},
onDisconnect: () => {
console.log(`User ${index + 1} (${userId}) disconnected`);
}
});
client.activate();
return client;
}
const loopStartTime = Date.now();
for (let i = 0; i < numberOfUsers; i++) {
const userId = uuidv4();
const player = {
id: userId,
segments: [{ x: Math.random() * 500, y: Math.random() * 500 }],
color: '#FF0000',
score: 0,
username: `user${i+ 1}`,
nickname: `User_${i + 1}`,
type: 'snake'
};
const response = apiClientRuningService.post(`/api/rooms/${roomId}/players`, player)
.then(response => {
const loopEndTime = Date.now();
const loopDurationSeconds = (loopEndTime - loopStartTime) / 1000;
console.log(`Total execution time for the loop: ${loopDurationSeconds.toFixed(3)} seconds ,${JSON.stringify(response)}`);
})
.catch(error => {
// console.error('callback Failed to add player:', error);
});
console.log(`Player ${userId} added to room ${roomId} via API.${i}` );
}