lab 3
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
package com.example.master;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@RestController
|
||||
public class MasterController {
|
||||
|
||||
private final String[] workerUrls = {
|
||||
"http://192.168.16.116:8081/calculateSum",
|
||||
"http://192.168.16.117:8082/calculateSum",
|
||||
"http://192.168.16.118:8083/calculateSum"
|
||||
};
|
||||
|
||||
@Autowired
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
@GetMapping("/start")
|
||||
public ResponseEntity<Map<String, Object>> start() {
|
||||
long startTime = System.currentTimeMillis();
|
||||
|
||||
// Генерация матрицы 1000x1000
|
||||
int[][] matrix = generateMatrix(1000, 1000);
|
||||
System.out.println("=== Генерация матрицы 1000x1000 завершена ===");
|
||||
|
||||
// Разделение матрицы на 3 части
|
||||
int chunkSize = matrix.length / 3;
|
||||
int[][] part1 = Arrays.copyOfRange(matrix, 0, chunkSize);
|
||||
int[][] part2 = Arrays.copyOfRange(matrix, chunkSize, 2 * chunkSize);
|
||||
int[][] part3 = Arrays.copyOfRange(matrix, 2 * chunkSize, matrix.length);
|
||||
|
||||
System.out.println("\n=== Отправка данных на Worker Services ===");
|
||||
System.out.println("Отправка части 1 на Worker 1 (строки 0-" + (chunkSize - 1) + ")");
|
||||
System.out.println("Отправка части 2 на Worker 2 (строки " + chunkSize + "-" + (2 * chunkSize - 1) + ")");
|
||||
System.out.println("Отправка части 3 на Worker 3 (строки " + (2 * chunkSize) + "-" + (matrix.length - 1) + ")");
|
||||
|
||||
// Отправка частей на Worker Services
|
||||
int[] sums1 = restTemplate.postForObject(workerUrls[0], part1, int[].class);
|
||||
int[] sums2 = restTemplate.postForObject(workerUrls[1], part2, int[].class);
|
||||
int[] sums3 = restTemplate.postForObject(workerUrls[2], part3, int[].class);
|
||||
|
||||
System.out.println("\n=== Получение результатов от Worker Services ===");
|
||||
System.out.println("Результаты от Worker 1: " + sums1.length + " строк");
|
||||
System.out.println("Результаты от Worker 2: " + sums2.length + " строк");
|
||||
System.out.println("Результаты от Worker 3: " + sums3.length + " строк");
|
||||
|
||||
// Объединение результатов
|
||||
int[] allSums = new int[matrix.length];
|
||||
System.arraycopy(sums1, 0, allSums, 0, sums1.length);
|
||||
System.arraycopy(sums2, 0, allSums, sums1.length, sums2.length);
|
||||
System.arraycopy(sums3, 0, allSums, sums1.length + sums2.length, sums3.length);
|
||||
|
||||
System.out.println("\n=== Объединенные суммы строк ===");
|
||||
System.out.println("Всего сумм: " + allSums.length);
|
||||
|
||||
// Сортировка матрицы по убыванию суммы строк
|
||||
sortMatrixBySum(matrix, allSums);
|
||||
|
||||
System.out.println("\n=== Матрица отсортированна ===");
|
||||
|
||||
long endTime = System.currentTimeMillis();
|
||||
long duration = endTime - startTime;
|
||||
|
||||
System.out.println("\n=== Время выполнения ===");
|
||||
System.out.println(duration + " мс");
|
||||
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("durationMs", duration);
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
|
||||
private int[][] generateMatrix(int rows, int cols) {
|
||||
Random random = new Random();
|
||||
int[][] matrix = new int[rows][cols];
|
||||
for (int i = 0; i < rows; i++) {
|
||||
for (int j = 0; j < cols; j++) {
|
||||
matrix[i][j] = random.nextInt(100); // Заполняем случайными числами от 0 до 99
|
||||
}
|
||||
}
|
||||
return matrix;
|
||||
}
|
||||
|
||||
private void sortMatrixBySum(int[][] matrix, int[] sums) {
|
||||
for (int i = 0; i < sums.length - 1; i++) {
|
||||
for (int j = 0; j < sums.length - i - 1; j++) {
|
||||
if (sums[j] < sums[j + 1]) {
|
||||
// Меняем местами суммы
|
||||
int tempSum = sums[j];
|
||||
sums[j] = sums[j + 1];
|
||||
sums[j + 1] = tempSum;
|
||||
|
||||
// Меняем местами строки матрицы
|
||||
int[] tempRow = matrix[j];
|
||||
matrix[j] = matrix[j + 1];
|
||||
matrix[j + 1] = tempRow;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user