"test/api/test_c_op_construct.c" did not exist on "a129ea197a94a7a97450ad616325c7537360c28e"
naive_test.py 5.84 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Copyright (c) Microsoft Corporation
# All rights reserved.
#
# MIT License
#
# 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.
Zejun Lin's avatar
Zejun Lin committed
20

chicm-ms's avatar
chicm-ms committed
21
22
import sys
import os.path as osp
Zejun Lin's avatar
Zejun Lin committed
23
24
25
26
27
28
import json
import subprocess
import sys
import time
import traceback

29
from utils import is_experiment_done, get_experiment_id, get_nni_log_path, read_last_line, remove_files, setup_experiment, detect_port, snooze
30
from utils import GREEN, RED, CLEAR, EXPERIMENT_URL
Zejun Lin's avatar
Zejun Lin committed
31

32
def naive_test():
33
    '''run naive integration test'''
Zejun Lin's avatar
Zejun Lin committed
34
    to_remove = ['tuner_search_space.json', 'tuner_result.txt', 'assessor_result.txt']
chicm-ms's avatar
chicm-ms committed
35
    to_remove = list(map(lambda file: osp.join('naive_test', file), to_remove))
Zejun Lin's avatar
Zejun Lin committed
36
37
    remove_files(to_remove)

chicm-ms's avatar
chicm-ms committed
38
39
40
41
42
    if sys.platform == 'win32':
        config_file = 'local_win32.yml'
    else:
        config_file = 'local.yml'
    proc = subprocess.run(['nnictl', 'create', '--config', osp.join('naive_test' , config_file)])
Zejun Lin's avatar
Zejun Lin committed
43
44
45
46
    assert proc.returncode == 0, '`nnictl create` failed with code %d' % proc.returncode

    print('Spawning trials...')

47
    nnimanager_log_path = get_nni_log_path(EXPERIMENT_URL)
Zejun Lin's avatar
Zejun Lin committed
48
49
    current_trial = 0

Zejun Lin's avatar
Zejun Lin committed
50
    for _ in range(120):
Zejun Lin's avatar
Zejun Lin committed
51
52
        time.sleep(1)

chicm-ms's avatar
chicm-ms committed
53
54
        tuner_status = read_last_line(osp.join('naive_test', 'tuner_result.txt'))
        assessor_status = read_last_line(osp.join('naive_test', 'assessor_result.txt'))
55
        experiment_status = is_experiment_done(nnimanager_log_path)
Zejun Lin's avatar
Zejun Lin committed
56
57
58
59
60
61
62
63

        assert tuner_status != 'ERROR', 'Tuner exited with error'
        assert assessor_status != 'ERROR', 'Assessor exited with error'

        if experiment_status:
            break

        if tuner_status is not None:
chicm-ms's avatar
chicm-ms committed
64
            for line in open(osp.join('naive_test', 'tuner_result.txt')):
Zejun Lin's avatar
Zejun Lin committed
65
66
67
68
69
70
71
                if line.strip() == 'ERROR':
                    break
                trial = int(line.split(' ')[0])
                if trial > current_trial:
                    current_trial = trial
                    print('Trial #%d done' % trial)

Zejun Lin's avatar
Zejun Lin committed
72
    assert experiment_status, 'Failed to finish in 2 min'
Zejun Lin's avatar
Zejun Lin committed
73

chicm-ms's avatar
chicm-ms committed
74
75
    ss1 = json.load(open(osp.join('naive_test', 'search_space.json')))
    ss2 = json.load(open(osp.join('naive_test', 'tuner_search_space.json')))
Zejun Lin's avatar
Zejun Lin committed
76
77
    assert ss1 == ss2, 'Tuner got wrong search space'

chicm-ms's avatar
chicm-ms committed
78
79
    tuner_result = set(open(osp.join('naive_test', 'tuner_result.txt')))
    expected = set(open(osp.join('naive_test', 'expected_tuner_result.txt')))
Zejun Lin's avatar
Zejun Lin committed
80
81
    # Trials may complete before NNI gets assessor's result,
    # so it is possible to have more final result than expected
chicm-ms's avatar
chicm-ms committed
82
83
    print('Tuner result:', tuner_result)
    print('Expected tuner result:', expected)
Zejun Lin's avatar
Zejun Lin committed
84
85
    assert tuner_result.issuperset(expected), 'Bad tuner result'

chicm-ms's avatar
chicm-ms committed
86
87
    assessor_result = set(open(osp.join('naive_test', 'assessor_result.txt')))
    expected = set(open(osp.join('naive_test', 'expected_assessor_result.txt')))
Zejun Lin's avatar
Zejun Lin committed
88
89
    assert assessor_result == expected, 'Bad assessor result'

90
91
92
93
94
95
    subprocess.run(['nnictl', 'stop'])
    snooze()

def stop_experiment_test():
    '''Test `nnictl stop` command, including `nnictl stop exp_id` and `nnictl stop all`.
    Simple `nnictl stop` is not tested here since it is used in all other test code'''
chicm-ms's avatar
chicm-ms committed
96
97
98
99
    subprocess.run(['nnictl', 'create', '--config', osp.join('tuner_test', 'local.yml'), '--port', '8080'], check=True)
    subprocess.run(['nnictl', 'create', '--config', osp.join('tuner_test', 'local.yml'), '--port', '8888'], check=True)
    subprocess.run(['nnictl', 'create', '--config', osp.join('tuner_test', 'local.yml'), '--port', '8989'], check=True)
    subprocess.run(['nnictl', 'create', '--config', osp.join('tuner_test', 'local.yml'), '--port', '8990'], check=True)
100
101
102
103
104
105
106
107

    # test cmd 'nnictl stop id`
    experiment_id = get_experiment_id(EXPERIMENT_URL)
    proc = subprocess.run(['nnictl', 'stop', experiment_id])
    assert proc.returncode == 0, '`nnictl stop %s` failed with code %d' % (experiment_id, proc.returncode)
    snooze()
    assert not detect_port(8080), '`nnictl stop %s` failed to stop experiments' % experiment_id

108
109
110
111
112
113
    # test cmd `nnictl stop --port`
    proc = subprocess.run(['nnictl', 'stop', '--port', '8990'])
    assert proc.returncode == 0, '`nnictl stop %s` failed with code %d' % (experiment_id, proc.returncode)
    snooze()
    assert not detect_port(8990), '`nnictl stop %s` failed to stop experiments' % experiment_id

114
115
116
    # test cmd `nnictl stop --all`
    proc = subprocess.run(['nnictl', 'stop', '--all'])
    assert proc.returncode == 0, '`nnictl stop --all` failed with code %d' % proc.returncode
117
    snooze()
118
    assert not detect_port(8888) and not detect_port(8989), '`nnictl stop --all` failed to stop experiments'
119
120


Zejun Lin's avatar
Zejun Lin committed
121
122
123
124
if __name__ == '__main__':
    installed = (sys.argv[-1] != '--preinstall')
    setup_experiment(installed)
    try:
125
126
        naive_test()
        stop_experiment_test()
Zejun Lin's avatar
Zejun Lin committed
127
128
129
130
131
132
133
        # TODO: check the output of rest server
        print(GREEN + 'PASS' + CLEAR)
    except Exception as error:
        print(RED + 'FAIL' + CLEAR)
        print('%r' % error)
        traceback.print_exc()
        sys.exit(1)