Skip to content

Commit d55460a

Browse files
committed
prepare some test data, to ensure parsing from a pointer works.
1 parent b42fc12 commit d55460a

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

pyslurm/core/slurmctld/stats.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
# cython: c_string_type=unicode, c_string_encoding=default
3737
# cython: language_level=3
3838

39+
from libc.string cimport memset
3940
from pyslurm cimport slurm
4041
from pyslurm.slurm cimport (
4142
stats_info_response_msg_t,
@@ -44,6 +45,7 @@ from pyslurm.slurm cimport (
4445
slurm_reset_statistics,
4546
slurm_free_stats_response_msg,
4647
xfree,
48+
xmalloc,
4749
)
4850
from pyslurm.utils cimport cstr
4951
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, int64_t

pyslurm/core/slurmctld/stats.pyx

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,3 +427,68 @@ cdef parse_response(stats_info_response_msg_t *ptr):
427427
out.backfill_exit = BackfillExitStatistics.from_ptr(ptr)
428428

429429
return out
430+
431+
432+
# Prepare some test data
433+
def _parse_test_data():
434+
import datetime
435+
436+
cdef stats_info_response_msg_t stats
437+
memset(&stats, 0, sizeof(stats))
438+
439+
stats.req_time = int(datetime.datetime.now().timestamp())
440+
stats.req_time_start = int(datetime.datetime.now().timestamp()) - 200
441+
stats.jobs_submitted = 20
442+
stats.jobs_running = 3
443+
stats.schedule_cycle_counter = 10
444+
stats.schedule_cycle_last = 40
445+
stats.schedule_cycle_sum = 45
446+
447+
stats.bf_cycle_counter = 100
448+
stats.bf_active = 0
449+
stats.bf_backfilled_jobs = 10
450+
stats.bf_cycle_sum = 200
451+
stats.bf_depth_try_sum = 300
452+
stats.bf_queue_len_sum = 600
453+
stats.bf_table_size_sum = 200
454+
455+
stats.rpc_type_size = 3
456+
stats.rpc_type_id = <uint16_t*>xmalloc(sizeof(uint16_t) * stats.rpc_type_size)
457+
stats.rpc_type_cnt = <uint32_t*>xmalloc(sizeof(uint32_t) * stats.rpc_type_size)
458+
stats.rpc_type_time = <uint64_t*>xmalloc(sizeof(uint64_t) * stats.rpc_type_size)
459+
460+
for i in range(stats.rpc_type_size):
461+
stats.rpc_type_id[i] = 2000+i
462+
stats.rpc_type_cnt[i] = i+1
463+
stats.rpc_type_time[i] = i+2
464+
465+
stats.rpc_user_size = 1
466+
stats.rpc_user_id = <uint32_t*>xmalloc(sizeof(uint32_t) * stats.rpc_user_size)
467+
stats.rpc_user_cnt = <uint32_t*>xmalloc(sizeof(uint32_t) * stats.rpc_user_size)
468+
stats.rpc_user_time = <uint64_t*>xmalloc(sizeof(uint64_t) * stats.rpc_user_size)
469+
470+
for i in range(stats.rpc_user_size):
471+
stats.rpc_user_id[i] = i
472+
stats.rpc_user_cnt[i] = i+1
473+
stats.rpc_user_time[i] = i+2
474+
475+
stats.bf_exit_cnt = BF_EXIT_COUNT
476+
stats.bf_exit = <uint32_t*>xmalloc(sizeof(uint32_t) * BF_EXIT_COUNT)
477+
for i in range(stats.bf_exit_cnt):
478+
stats.bf_exit[i] = i+1
479+
480+
stats.schedule_exit_cnt = SCHED_EXIT_COUNT
481+
stats.schedule_exit = <uint32_t*>xmalloc(sizeof(uint32_t) * SCHED_EXIT_COUNT)
482+
483+
for i in range(stats.schedule_exit_cnt):
484+
stats.schedule_exit[i] = i+1
485+
486+
stats.rpc_queue_type_count = 5
487+
stats.rpc_queue_count = <uint32_t*>xmalloc(sizeof(uint32_t) * stats.rpc_queue_type_count)
488+
stats.rpc_queue_type_id = <uint32_t*>xmalloc(sizeof(uint32_t) * stats.rpc_queue_type_count)
489+
490+
for i in range(stats.rpc_queue_type_count):
491+
stats.rpc_queue_count[i] = i+1
492+
stats.rpc_queue_type_id[i] = 2000+i
493+
494+
return parse_response(&stats)

tests/unit/test_slurmctld.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#########################################################################
2+
# test_slurmctld.py - slurmctld unit tests
3+
#########################################################################
4+
# Copyright (C) 2025 Toni Harzendorf <[email protected]>
5+
#
6+
# This file is part of PySlurm
7+
#
8+
# PySlurm is free software; you can redistribute it and/or modify
9+
# it under the terms of the GNU General Public License as published by
10+
# the Free Software Foundation; either version 2 of the License, or
11+
# (at your option) any later version.
12+
13+
# PySlurm is distributed in the hope that it will be useful,
14+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
# GNU General Public License for more details.
17+
#
18+
# You should have received a copy of the GNU General Public License along
19+
# with PySlurm; if not, write to the Free Software Foundation, Inc.,
20+
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21+
"""test_slurmctld.py - Unit test basic slurmctld functionalities."""
22+
23+
import pyslurm
24+
from pyslurm import slurmctld
25+
from pyslurm.core.slurmctld.stats import _parse_test_data
26+
27+
28+
def test_statistics():
29+
stats = _parse_test_data()
30+
assert stats.to_dict()
31+
32+
assert len(stats.rpcs_by_type) == 3
33+
for typ, val in stats.rpcs_by_type.items():
34+
assert val.count > 0
35+
assert val.time > 0
36+
assert val.average_time > 0
37+
assert typ is not None
38+
39+
assert len(stats.rpcs_pending) == 5
40+
for typ, val in stats.rpcs_pending.items():
41+
assert val.count > 0
42+
assert typ is not None
43+
assert isinstance(typ, str)
44+
45+
assert len(stats.rpcs_by_user) == 1
46+
for typ, val in stats.rpcs_by_user.items():
47+
assert val.user_id == 0
48+
assert val.user_name == "root"
49+
assert val.time > 0
50+
assert val.average_time > 0
51+
assert typ is not None
52+
53+
assert stats.schedule_exit
54+
assert stats.backfill_exit
55+
assert stats.jobs_submitted == 20
56+
assert stats.jobs_running == 3
57+
assert stats.schedule_cycle_last == 40
58+
assert stats.schedule_cycle_sum == 45
59+
assert stats.schedule_cycle_mean == 4
60+
assert stats.schedule_cycle_counter == 10
61+
62+
assert stats.backfill_cycle_counter == 100
63+
assert stats.backfill_active is False
64+
assert stats.backfilled_jobs == 10
65+
assert stats.backfill_cycle_sum == 200
66+
assert stats.backfill_depth_try_sum == 300
67+
assert stats.backfill_queue_length_sum == 600
68+
assert stats.backfill_table_size_sum == 200
69+
assert stats.backfill_cycle_mean == 2

0 commit comments

Comments
 (0)