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
|
--TEST--
proc_nice() basic behaviour
--SKIPIF--
<?php
/* No function_exists() check, proc_nice() is always available on Windows */
if (!defined('PHP_WINDOWS_VERSION_MAJOR')) {
die('skip: Only for Windows');
}
if (getenv('SKIP_SLOW_TESTS')) {
doe('skip: Slow test');
}
?>
--FILE--
<?php
function get_priority_from_wmic() {
static $bin, $pid;
if (!$bin) {
$t = explode('\\', PHP_BINARY);
$bin = end($t);
$pid = getmypid();
}
$t = '';
$p = popen('wmic process where name="' . $bin . '"', 'r');
if (!$p) {
return false;
}
while(!feof($p)) {
$t .= fread($p, 1024);
}
pclose($p);
$t = explode(PHP_EOL, $t);
$f = false;
$m = [
strpos($t[0], ' ProcessId' ),
strpos($t[0], ' Priority ')
];
foreach ($t as $n => $l) {
if (!$n || empty($l)) {
continue;
}
$d = [];
foreach ($m as $c) {
$d[] = (int) substr($l, $c + 1, strpos($l, ' ', $c + 2) - ($c + 1));
}
if ($d[0] === $pid) {
return $d[1];
}
}
return false;
}
$p = [
/* '<verbose name>' => ['<wmic value>', '<proc_nice value>'] */
'Idle' => [4, 10],
'Below normal' => [6, 5],
'Normal' => [8, 0],
'Above normal' => [10, -5],
'High priority' => [13, -10]
];
foreach ($p as $test => $data) {
printf('Testing \'%s\' (%d): ', $test, $data[1]);
proc_nice($data[1]);
print (($wp = get_priority_from_wmic()) === $data[0] ? 'Passed' : 'Failed (' . $wp . ')') . PHP_EOL;
}
?>
--EXPECT--
Testing 'Idle' (10): Passed
Testing 'Below normal' (5): Passed
Testing 'Normal' (0): Passed
Testing 'Above normal' (-5): Passed
Testing 'High priority' (-10): Passed
|