forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemoryTest.h
61 lines (53 loc) · 1.79 KB
/
MemoryTest.h
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
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once
#include "MantidKernel/System.h"
#include "MantidKernel/Timer.h"
#include <cxxtest/TestSuite.h>
#include "MantidKernel/FunctionTask.h"
#include "MantidKernel/Memory.h"
#include "MantidKernel/MultiThreaded.h"
#include "MantidKernel/ThreadPool.h"
using namespace Mantid::Kernel;
void MemoryTest_myTaskFunction() {
MemoryStats mem;
mem.update();
mem.getFreeRatio();
}
class MemoryTest : public CxxTest::TestSuite {
public:
void test_update() {
MemoryStats mem;
TS_ASSERT_THROWS_NOTHING(mem.update());
TS_ASSERT_LESS_THAN_EQUALS(0, mem.availMem());
TS_ASSERT_DIFFERS(mem.availMemStr(), "");
TS_ASSERT_LESS_THAN_EQUALS(0, mem.reservedMem());
TS_ASSERT_LESS_THAN(0, mem.totalMem()); // The machine must have some memory
TS_ASSERT_DIFFERS(mem.totalMemStr(), "");
// Current process stats
TS_ASSERT_LESS_THAN(0, mem.residentMem()); // Current process must use something
TS_ASSERT_DIFFERS(mem.resUsageStr(), "");
TS_ASSERT_LESS_THAN_EQUALS(0, mem.virtualMem());
TS_ASSERT_DIFFERS(mem.vmUsageStr(), "");
}
/// Update in parallel to test thread safety
void test_parallel() {
PARALLEL_FOR_NO_WSP_CHECK()
for (int i = 0; i < 500; i++) {
MemoryStats mem;
mem.update();
mem.getFreeRatio();
}
}
void test_parallel_threadpool() {
ThreadPool pool;
for (int i = 0; i < 500; i++) {
pool.schedule(std::make_shared<FunctionTask>(&MemoryTest_myTaskFunction, 1.0));
}
pool.joinAll();
}
};