optimizer.go 2.7 KB
Newer Older
Z
Zhipeng Xie 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (c) 2019 Huawei Technologies Co., Ltd.
 * A-Tune is licensed under the Mulan PSL v1.
 * You can use this software according to the terms and conditions of the Mulan PSL v1.
 * You may obtain a copy of Mulan PSL v1 at:
 *     http://license.coscl.org.cn/MulanPSL
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
 * See the Mulan PSL v1 for more details.
 * Create: 2019-10-29
 */

package models

import (
	"atune/common/config"
	"atune/common/http"
	"encoding/json"
20
	"fmt"
Z
Zhipeng Xie 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
	"io/ioutil"
)

// OptimizerPostBody send to the service to create a optimizer task
type OptimizerPostBody struct {
	MaxEval int    `json:"max_eval"`
	Knobs   []Knob `json:"knobs"`
}

// Knob body store the tuning properties
type Knob struct {
	Dtype   string   `json:"dtype"`
	Name    string   `json:"name"`
	Options []string `json:"options"`
	Type    string   `json:"type"`
	Range   []int64  `json:"range"`
	Items   []int64  `json:"items"`
	Step    int64    `json:"step"`
	Ref     string   `json:"ref"`
}

// RespPostBody :the body returned of create optimizer task
type RespPostBody struct {
44 45 46
	TaskID  string `json:"task_id"`
	Status  string `json:"status"`
	Message string `json:"message"`
Z
Zhipeng Xie 已提交
47 48 49 50 51 52 53 54 55 56
}

// OptimizerPutBody send to the optimizer service when iterations
type OptimizerPutBody struct {
	Iterations int    `json:"iterations"`
	Value      string `json:"value"`
}

// RespPutBody :the body returned of each optimizer iteration
type RespPutBody struct {
57 58
	Param   string `json:"param"`
	Message string `json:"message"`
Z
Zhipeng Xie 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
}

// Post method create a optimizer task
func (o *OptimizerPostBody) Post() (*RespPostBody, error) {
	url := config.GetURL(config.OptimizerURI)
	res, err := http.Post(url, o)
	if err != nil {
		return nil, err
	}

	defer res.Body.Close()

	respBody, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return nil, err
	}

	respPostIns := new(RespPostBody)

	err = json.Unmarshal(respBody, respPostIns)
	if err != nil {
		return nil, err
	}

83 84 85 86
	if res.StatusCode != 200 {
		return nil, fmt.Errorf(respPostIns.Message)
	}

Z
Zhipeng Xie 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
	return respPostIns, nil
}

// Put method send benchmark result to optimizer service
func (o *OptimizerPutBody) Put(url string) (*RespPutBody, error) {
	res, err := http.Put(url, o)
	if err != nil {
		return nil, err
	}

	defer res.Body.Close()

	respBody, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return nil, err
	}

	respPutIns := new(RespPutBody)

	err = json.Unmarshal(respBody, respPutIns)
	if err != nil {
		return nil, err
	}

111 112 113 114
	if res.StatusCode != 200 {
		return nil, fmt.Errorf(respPutIns.Message)
	}

Z
Zhipeng Xie 已提交
115 116
	return respPutIns, nil
}