Skip to content

Commit

Permalink
Development: Improve metrics performance by reducing lookahead period…
Browse files Browse the repository at this point in the history
…s. (#7459)
  • Loading branch information
sleiss authored Oct 29, 2023
1 parent 1119515 commit babc1bc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 46 deletions.
6 changes: 2 additions & 4 deletions src/main/java/de/tum/in/www1/artemis/config/MetricsBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ public class MetricsBean {
/**
* Some metrics (e.g. the number of upcoming exercises) are calculated for multiple lookahead periods.
* Each period/range is exposed as metrics with a according tag "range"
* NOTE: we reduced the intervals temporarily until more intervals are necessary
*/
private static final int[] MINUTE_RANGES_LOOKAHEAD = { 15, 30, 45, 60, 120 };
private static final int[] MINUTE_RANGES_LOOKAHEAD = { 15 };

private final MeterRegistry meterRegistry;

Expand Down Expand Up @@ -210,9 +211,6 @@ private void registerWebsocketMetrics() {
// Publish the number of currently (via WebSockets) connected sessions
Gauge.builder("artemis.instance.websocket.sessions", webSocketHandler, MetricsBean::extractWebsocketSessionCount).strongReference(true)
.description("Number of sessions connected to this Artemis instance").register(meterRegistry);
// TODO: DEPRECATED metric with same value - Should be removed after October 2023
Gauge.builder("artemis.instance.websocket.users", webSocketHandler, MetricsBean::extractWebsocketSessionCount).strongReference(true)
.description("Number of sessions connected to this Artemis instance").register(meterRegistry);

// Publish the number of currently (via WebSockets) connected users
Gauge.builder("artemis.global.websocket.users", userRegistry, MetricsBean::extractWebsocketUserCount).strongReference(true)
Expand Down
65 changes: 23 additions & 42 deletions src/test/java/de/tum/in/www1/artemis/config/MetricsBeanTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,11 @@ void testPrometheusMetricsExercises() {
course1.setStudentGroupName(TEST_PREFIX + "course1students");

course1.addExercises(exerciseRepository
.save(QuizExerciseFactory.generateQuizExercise(ZonedDateTime.now().plusMinutes(25), ZonedDateTime.now().plusMinutes(55), QuizMode.SYNCHRONIZED, course1)));
course1.addExercises(
exerciseRepository.save(QuizExerciseFactory.generateQuizExercise(ZonedDateTime.now(), ZonedDateTime.now().plusMinutes(3), QuizMode.SYNCHRONIZED, course1)));
.save(QuizExerciseFactory.generateQuizExercise(ZonedDateTime.now().plusMinutes(5), ZonedDateTime.now().plusMinutes(55), QuizMode.SYNCHRONIZED, course1)));
course1.addExercises(exerciseRepository
.save(textExerciseUtilService.createIndividualTextExercise(course1, ZonedDateTime.now().plusMinutes(1), ZonedDateTime.now().plusMinutes(25), null)));
.save(QuizExerciseFactory.generateQuizExercise(ZonedDateTime.now().plusMinutes(1), ZonedDateTime.now().plusMinutes(3), QuizMode.SYNCHRONIZED, course1)));
course1.addExercises(exerciseRepository
.save(textExerciseUtilService.createIndividualTextExercise(course1, ZonedDateTime.now().plusMinutes(1), ZonedDateTime.now().plusMinutes(12), null)));
courseRepository.save(course1);

metricsBean.recalculateMetrics();
Expand All @@ -356,12 +356,9 @@ void testPrometheusMetricsExercises() {
// Should now have one active user
assertMetricEquals(1, "artemis.scheduled.exercises.due.student_multiplier.active.14", "exerciseType", ExerciseType.QUIZ.toString(), "range", "15");

// Only one quiz is released within the next 30 minutes
assertMetricEquals(1, "artemis.scheduled.exercises.release.count", "exerciseType", ExerciseType.QUIZ.toString(), "range", "30");
assertMetricEquals(3 * 1, "artemis.scheduled.exercises.release.student_multiplier", "exerciseType", ExerciseType.QUIZ.toString(), "range", "30");

// Only one active user
assertMetricEquals(1, "artemis.scheduled.exercises.release.student_multiplier.active.14", "exerciseType", ExerciseType.QUIZ.toString(), "range", "30");
// Two quizzes are released within the next 15 minutes, but have the same users (-> Users are only counted once)
assertMetricEquals(2, "artemis.scheduled.exercises.release.count", "exerciseType", ExerciseType.QUIZ.toString(), "range", "15");
assertMetricEquals(3 * 1, "artemis.scheduled.exercises.release.student_multiplier", "exerciseType", ExerciseType.QUIZ.toString(), "range", "15");

// Add activity to another user
quizExerciseUtilService.saveQuizSubmission(exerciseUtilService.getFirstExerciseWithType(course1, QuizExercise.class), ParticipationFactory.generateQuizSubmission(true),
Expand All @@ -370,11 +367,7 @@ void testPrometheusMetricsExercises() {
metricsBean.recalculateMetrics();

// Should now have two active users
assertMetricEquals(2, "artemis.scheduled.exercises.release.student_multiplier.active.14", "exerciseType", ExerciseType.QUIZ.toString(), "range", "30");

// Both quizzes end within the next 120 minutes, but have the same users (-> Users are only counted once)
assertMetricEquals(2, "artemis.scheduled.exercises.due.count", "exerciseType", ExerciseType.QUIZ.toString(), "range", "120");
assertMetricEquals(3 * 1, "artemis.scheduled.exercises.due.student_multiplier", "exerciseType", ExerciseType.QUIZ.toString(), "range", "120");
assertMetricEquals(2, "artemis.scheduled.exercises.release.student_multiplier.active.14", "exerciseType", ExerciseType.QUIZ.toString(), "range", "15");

var course2 = courseUtilService.createCourse();
course2.setStudentGroupName(TEST_PREFIX + "course2students");
Expand All @@ -385,19 +378,19 @@ void testPrometheusMetricsExercises() {

metricsBean.recalculateMetrics();

// 3 quizzes end within the next 120 minutes, and are in two different courses -> 6 different users in total
assertMetricEquals(3, "artemis.scheduled.exercises.due.count", "exerciseType", ExerciseType.QUIZ.toString(), "range", "120");
assertMetricEquals(3 * 2, "artemis.scheduled.exercises.due.student_multiplier", "exerciseType", ExerciseType.QUIZ.toString(), "range", "120");
// 2 quizzes end within the next 15 minutes, and are in two different courses -> 6 different users in total
assertMetricEquals(2, "artemis.scheduled.exercises.due.count", "exerciseType", ExerciseType.QUIZ.toString(), "range", "15");
assertMetricEquals(3 * 2, "artemis.scheduled.exercises.due.student_multiplier", "exerciseType", ExerciseType.QUIZ.toString(), "range", "15");

// One text exercise is released within the next 30 minutes
assertMetricEquals(1, "artemis.scheduled.exercises.release.count", "exerciseType", ExerciseType.TEXT.toString(), "range", "30");
assertMetricEquals(1, "artemis.scheduled.exercises.release.count", "exerciseType", ExerciseType.TEXT.toString(), "range", "15");
}

@Test
void testPrometheusMetricsExams() {
var users = userUtilService.addUsers(TEST_PREFIX, 3, 0, 0, 0);
var course = courseUtilService.createCourse();
var exam1 = examUtilService.addExam(course, ZonedDateTime.now(), ZonedDateTime.now().plusMinutes(20), ZonedDateTime.now().plusMinutes(40));
var exam1 = examUtilService.addExam(course, ZonedDateTime.now(), ZonedDateTime.now().plusMinutes(2), ZonedDateTime.now().plusMinutes(10));
var registeredExamUser1 = new ExamUser();
registeredExamUser1.setUser(users.get(0));
registeredExamUser1.setExam(exam1);
Expand All @@ -415,7 +408,7 @@ void testPrometheusMetricsExams() {
examUtilService.addExerciseGroupsAndExercisesToExam(exam1, false);
courseRepository.save(course);

var exam2 = examUtilService.addExam(course, ZonedDateTime.now(), ZonedDateTime.now().plusMinutes(65), ZonedDateTime.now().plusMinutes(85));
var exam2 = examUtilService.addExam(course, ZonedDateTime.now(), ZonedDateTime.now().plusMinutes(5), ZonedDateTime.now().plusMinutes(12));
var registeredExamUser3 = new ExamUser();
registeredExamUser3.setUser(users.get(0));
registeredExamUser3.setExam(exam2);
Expand All @@ -429,25 +422,13 @@ void testPrometheusMetricsExams() {

metricsBean.recalculateMetrics();

// One exam ends within the next 60 minutes
assertMetricEquals(1, "artemis.scheduled.exams.due.count", "range", "60");
assertMetricEquals(1 * 2, "artemis.scheduled.exams.due.student_multiplier", "range", "60"); // 2 students are registered for the exam
// Two exams start within the next 15 minutes, but have the same users (-> Users are only counted once)
assertMetricEquals(2, "artemis.scheduled.exams.release.count", "range", "15");
assertMetricEquals(1 * 2, "artemis.scheduled.exams.release.student_multiplier", "range", "15");

// Two exams ends within the next 120 minutes
assertMetricEquals(2, "artemis.scheduled.exams.due.count", "range", "120");
assertMetricEquals(1 * 2, "artemis.scheduled.exams.due.student_multiplier", "range", "120"); // 2 + 1 students are registered for the exam, but they are duplicate users

// No exam starts within the next 15 minutes
assertMetricEquals(0, "artemis.scheduled.exams.release.count", "range", "15");
assertMetricEquals(0, "artemis.scheduled.exams.release.student_multiplier", "range", "15");

// One exam starts within the next 30 minutes
assertMetricEquals(1, "artemis.scheduled.exams.release.count", "range", "30");
assertMetricEquals(1 * 2, "artemis.scheduled.exams.release.student_multiplier", "range", "30"); // 2 registered students

// Two exams start within the next 120 minutes, but have the same users (-> Users are only counted once)
assertMetricEquals(2, "artemis.scheduled.exams.release.count", "range", "120");
assertMetricEquals(1 * 2, "artemis.scheduled.exams.release.student_multiplier", "range", "120");
assertMetricEquals(2, "artemis.scheduled.exams.due.count", "range", "15");
assertMetricEquals(1 * 2, "artemis.scheduled.exams.due.student_multiplier", "range", "15"); // 2 + 1 students are registered for the exam, but they are duplicate users

var registeredExamUser4 = new ExamUser();
registeredExamUser4.setUser(users.get(2));
Expand All @@ -459,12 +440,12 @@ void testPrometheusMetricsExams() {

metricsBean.recalculateMetrics();

// Two exams start within the next 120 minutes, but have the same users (-> Users are only counted once)
assertMetricEquals(2, "artemis.scheduled.exams.release.count", "range", "120");
assertMetricEquals(1 * 2 + 1 * 1, "artemis.scheduled.exams.release.student_multiplier", "range", "120");
// Two exams start within the next 15 minutes, but have the same users (-> Users are only counted once)
assertMetricEquals(2, "artemis.scheduled.exams.release.count", "range", "15");
assertMetricEquals(1 * 2 + 1 * 1, "artemis.scheduled.exams.release.student_multiplier", "range", "15");

// Exam exercises are not returned in the exercises metrics
assertMetricEquals(0, "artemis.scheduled.exercises.due.count", "exerciseType", ExerciseType.QUIZ.toString(), "range", "60");
assertMetricEquals(0, "artemis.scheduled.exercises.due.count", "exerciseType", ExerciseType.QUIZ.toString(), "range", "15");
}

@Test
Expand Down

0 comments on commit babc1bc

Please sign in to comment.