summaryrefslogtreecommitdiff
path: root/opendc-experiments/opendc-experiments-base/src/test/kotlin/org/opendc/experiments/base/SchedulerTest.kt
blob: 487b8cbc1246e4892a34643e3e84a5f618cf568d (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 * Copyright (c) 2025 AtLarge Research
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package org.opendc.experiments.base

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertAll
import org.opendc.compute.simulator.scheduler.FilterScheduler
import org.opendc.compute.simulator.scheduler.MemorizingScheduler
import org.opendc.compute.simulator.scheduler.filters.ComputeFilter
import org.opendc.compute.simulator.scheduler.filters.RamFilter
import org.opendc.compute.simulator.scheduler.filters.VCpuFilter
import org.opendc.compute.simulator.scheduler.filters.VGpuFilter
import org.opendc.compute.simulator.scheduler.weights.VCpuWeigher
import org.opendc.compute.simulator.scheduler.weights.VGpuWeigher
import org.opendc.compute.simulator.service.ServiceTask
import org.opendc.simulator.compute.workload.trace.TraceFragment
import java.util.ArrayList

class SchedulerTest {
    @Test
    fun testSimulator4Memorizing() {
        val workload: ArrayList<ServiceTask> =
            arrayListOf(
                createTestTask(
                    id = 0,
                    fragments =
                        arrayListOf(
                            TraceFragment(10 * 60 * 1000, 1000.0),
                        ),
                    cpuCoreCount = 1,
                ),
                createTestTask(
                    id = 1,
                    fragments =
                        arrayListOf(
                            TraceFragment(5 * 60 * 1000, 2000.0),
                        ),
                    cpuCoreCount = 1,
                    submissionTime = "1970-01-01T00:20",
                ),
            )

        val topology = createTopology("single_1_2000.json")

        val computeScheduler =
            MemorizingScheduler(
                filters = listOf(ComputeFilter(), VCpuFilter(1.0), RamFilter(1.0)),
            )
        val monitor = runTest(topology, workload, computeScheduler = computeScheduler)

        assertAll(
            { assertEquals(25 * 60 * 1000, monitor.maxTimestamp) { "Total runtime incorrect" } },
            { assertEquals(((10 * 30000) + (10 * 60000)).toLong(), monitor.hostCpuIdleTimes["H01"]?.sum()) { "Idle time incorrect" } },
            { assertEquals(((10 * 30000) + (5 * 60000)).toLong(), monitor.hostCpuActiveTimes["H01"]?.sum()) { "Active time incorrect" } },
            { assertEquals(9000.0, monitor.hostEnergyUsages["H01"]?.get(0)) { "Incorrect energy usage" } },
            {
                assertEquals(
                    (600 * 150.0) + (600 * 100.0) + (300 * 200.0),
                    monitor.hostEnergyUsages["H01"]?.sum(),
                ) { "Incorrect energy usage" }
            },
        )
    }

    /**
     * This test verifies that the gpu only schedulers are working correctly.
     * The same workload is run 4 times, once with the normal gpu filter scheduler and once with the inverted gpu filter scheduler.
     * Each scheduler is then run with a hardware configuration where the tasks fit onto one host, and one where multiple hosts are needed.
     */
    @Test
    fun testGpuAwareSchedulers() {
        // Define workload with tasks requiring both CPU and GPU resources
        val workload: ArrayList<ServiceTask> =
            arrayListOf(
                createTestTask(
                    id = 0,
                    fragments =
                        arrayListOf(
                            TraceFragment(10 * 60 * 1000, 1000.0, 2000.0),
                        ),
                    cpuCoreCount = 1,
                    gpuCoreCount = 1,
                ),
                createTestTask(
                    id = 1,
                    fragments =
                        arrayListOf(
                            TraceFragment(10 * 60 * 1000, 1000.0, 2000.0),
                        ),
                    cpuCoreCount = 1,
                    gpuCoreCount = 1,
                    submissionTime = "1970-01-01T00:20",
                ),
            )

        // Topology with 1 host having 2 GPUs (both tasks can fit on one host)
        val fittingTopology = createTopology("Gpus/dual_core_gpu_host.json")

        // Topology with 2 hosts each having 1 GPU (tasks must be distributed)
        val nonFittingTopology = createTopology("Gpus/single_gpu_hosts.json")

        val cpuAllocationRatio = 1.0
        val ramAllocationRatio = 1.5
        val gpuAllocationRatio = 1.0

        // Normal scheduler prioritizes hosts with more available resources
        val normalScheduler =
            FilterScheduler(
                filters =
                    listOf(
                        ComputeFilter(),
                        VCpuFilter(cpuAllocationRatio),
                        VGpuFilter(gpuAllocationRatio),
                        RamFilter(ramAllocationRatio),
                    ),
                weighers = listOf(VCpuWeigher(cpuAllocationRatio, multiplier = 1.0), VGpuWeigher(gpuAllocationRatio, multiplier = 1.0)),
            )

        // Inverted scheduler prioritizes hosts with fewer available resources
        val invertedScheduler =
            FilterScheduler(
                filters =
                    listOf(
                        ComputeFilter(),
                        VCpuFilter(cpuAllocationRatio),
                        VGpuFilter(gpuAllocationRatio),
                        RamFilter(ramAllocationRatio),
                    ),
                weighers = listOf(VCpuWeigher(cpuAllocationRatio, multiplier = -1.0), VGpuWeigher(gpuAllocationRatio, multiplier = -1.0)),
            )

        // Run the tests with both schedulers and both topologies
        val normalFittingMonitor = runTest(fittingTopology, workload, computeScheduler = normalScheduler)
        val normalNonFittingMonitor = runTest(nonFittingTopology, workload, computeScheduler = normalScheduler)
        val invertedFittingMonitor = runTest(fittingTopology, workload, computeScheduler = invertedScheduler)
        val invertedNonFittingMonitor = runTest(nonFittingTopology, workload, computeScheduler = invertedScheduler)

        assertAll(
            // Normal scheduler with fitting topology should use just one host
            {
                assertEquals(
                    1,
                    normalFittingMonitor.hostCpuSupplied.size,
                ) { "Normal scheduler should place both tasks on a single host when possible" }
            },
            // Normal scheduler with non-fitting topology must use two hosts
            {
                assertEquals(
                    2,
                    normalNonFittingMonitor.hostCpuSupplied.size,
                ) { "Normal scheduler should distribute tasks across hosts when needed" }
            },
            // Inverted scheduler with fitting topology might still use one host or distribute depending on implementation
            {
                assert(
                    invertedFittingMonitor.hostCpuSupplied.isNotEmpty(),
                ) { "Inverted scheduler should place tasks based on resource availability" }
            },
            // Inverted scheduler with non-fitting topology must use two hosts
            {
                assertEquals(
                    2,
                    invertedNonFittingMonitor.hostCpuSupplied.size,
                ) { "Inverted scheduler should distribute tasks across hosts when needed" }
            },
            // Verify GPU allocations - check that both tasks had their GPUs allocated
            { assertEquals(2, normalFittingMonitor.taskGpuSupplied.size) { "Both tasks should have GPU allocations" } },
            { assertEquals(2, normalNonFittingMonitor.taskGpuSupplied.size) { "Both tasks should have GPU allocations" } },
            { assertEquals(2, invertedFittingMonitor.taskGpuSupplied.size) { "Both tasks should have GPU allocations" } },
            { assertEquals(2, invertedNonFittingMonitor.taskGpuSupplied.size) { "Both tasks should have GPU allocations" } },
        )
    }
}