135 lines
3.7 KiB
Go
135 lines
3.7 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"path/filepath"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// Config 结构体包含测试所需的所有配置项
|
|
type Config struct {
|
|
Server ServerConfig `yaml:"server"`
|
|
Client ClientConfig `yaml:"client"`
|
|
Test TestConfig `yaml:"test"`
|
|
}
|
|
|
|
// ServerConfig 包含服务器配置
|
|
type ServerConfig struct {
|
|
ListenAddr string `yaml:"listen_addr"`
|
|
Port int `yaml:"port"`
|
|
LogFile string `yaml:"log_file"`
|
|
DevicesNVMe string `yaml:"devices_nvme"` // NVMe设备路径
|
|
DevicesHDD string `yaml:"devices_hdd"` // HDD设备路径
|
|
CacheInstanceID string `yaml:"cache_instance_id"` // Open-CAS缓存实例ID
|
|
MountPoint string `yaml:"mount_point"` // 文件系统挂载点
|
|
}
|
|
|
|
// ClientConfig 包含客户端配置
|
|
type ClientConfig struct {
|
|
ServerAddr string `yaml:"server_addr"`
|
|
LogFile string `yaml:"log_file"`
|
|
Concurrency int `yaml:"concurrency"`
|
|
TimeoutSec int `yaml:"timeout_sec"`
|
|
ReportFormat string `yaml:"report_format"` // json, csv, html
|
|
}
|
|
|
|
// TestConfig 包含测试相关配置
|
|
type TestConfig struct {
|
|
DataSizeMB int `yaml:"data_size_mb"` // 每次测试的数据量
|
|
VerificationFrequency int `yaml:"verification_frequency"` // 验证频率
|
|
PowerCutMethod string `yaml:"power_cut_method"` // 模拟断电方法
|
|
TestDuration int `yaml:"test_duration"` // 测试持续时间(分钟)
|
|
BlockSize int `yaml:"block_size"` // 块大小(KB)
|
|
EnabledTests []string `yaml:"enabled_tests"` // 启用的测试用例
|
|
}
|
|
|
|
// Load 从指定的文件路径加载配置
|
|
func Load(path string) (*Config, error) {
|
|
filename, err := filepath.Abs(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
yamlFile, err := ioutil.ReadFile(filename)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error reading config file: %s", err)
|
|
}
|
|
|
|
config := &Config{}
|
|
err = yaml.Unmarshal(yamlFile, config)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error parsing config file: %s", err)
|
|
}
|
|
|
|
// 设置默认值
|
|
if config.Server.Port == 0 {
|
|
config.Server.Port = 8080
|
|
}
|
|
if config.Client.Concurrency == 0 {
|
|
config.Client.Concurrency = 5
|
|
}
|
|
if config.Client.TimeoutSec == 0 {
|
|
config.Client.TimeoutSec = 30
|
|
}
|
|
if config.Test.DataSizeMB == 0 {
|
|
config.Test.DataSizeMB = 100
|
|
}
|
|
if config.Test.BlockSize == 0 {
|
|
config.Test.BlockSize = 4 // 4KB
|
|
}
|
|
if config.Test.TestDuration == 0 {
|
|
config.Test.TestDuration = 60 // 60分钟
|
|
}
|
|
|
|
return config, nil
|
|
}
|
|
|
|
// Save 将配置保存到指定路径
|
|
func (c *Config) Save(path string) error {
|
|
data, err := yaml.Marshal(c)
|
|
if err != nil {
|
|
return fmt.Errorf("error marshaling config: %s", err)
|
|
}
|
|
|
|
err = ioutil.WriteFile(path, data, 0644)
|
|
if err != nil {
|
|
return fmt.Errorf("error writing config file: %s", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// CreateDefaultConfig 创建默认配置文件
|
|
func CreateDefaultConfig(path string) error {
|
|
config := &Config{
|
|
Server: ServerConfig{
|
|
ListenAddr: "0.0.0.0",
|
|
Port: 8080,
|
|
LogFile: "server.log",
|
|
DevicesNVMe: "/dev/nvme0n1",
|
|
DevicesHDD: "/dev/sda",
|
|
CacheInstanceID: "cache1",
|
|
MountPoint: "/mnt/opencas",
|
|
},
|
|
Client: ClientConfig{
|
|
ServerAddr: "localhost:8080",
|
|
LogFile: "client.log",
|
|
Concurrency: 5,
|
|
TimeoutSec: 30,
|
|
ReportFormat: "json",
|
|
},
|
|
Test: TestConfig{
|
|
DataSizeMB: 100,
|
|
VerificationFrequency: 10,
|
|
PowerCutMethod: "signal", // signal或physical
|
|
TestDuration: 60,
|
|
BlockSize: 4,
|
|
EnabledTests: []string{"sequential", "random", "mixed", "concurrent", "power_loss", "stability"},
|
|
},
|
|
}
|
|
|
|
return config.Save(path)
|
|
}
|