diff --git a/tests/stress/README.md b/tests/stress/README.md index e23927bd1af95a572857c4f665811ee78146209a..c00e954c2445286cdf07780e7821da9c9917fe46 100644 --- a/tests/stress/README.md +++ b/tests/stress/README.md @@ -5,7 +5,7 @@ Stress test tool for TDengine. It run a set of test cases randomly and show stat ## COMMAND LINE ``` bash -$ ./stress [-h=] [-P=<0>] [-d=] [-u=] [-p=] [-c=<4>] [-f=] [test case file] +$ ./stress [-h=] [-P=<0>] [-d=] [-u=] [-p=] [-c=<4>] [-f=] [path_or_sql] ``` * **-h**: host name or IP address of TDengine server (default: localhost). @@ -14,7 +14,7 @@ $ ./stress [-h=] [-P=<0>] [-d=] [-u=] [-p=] [-c * **-p**: password (default: taosdata). * **-c**: concurrency, number of concurrent goroutines for query (default: 4). * **-f**: fetch data or not (default: true). -* **test case file**: the path of a JSON file which contains the test cases (default: cases.json). +* **path_or_sql**: a SQL statement or path of a JSON file which contains the test cases (default: cases.json). ## TEST CASE FILE diff --git a/tests/stress/main.go b/tests/stress/main.go index 2e0775d498bccf33223c7851cfddac1757377c53..c32f1660a122e225af5d7c6da450944185889ba4 100644 --- a/tests/stress/main.go +++ b/tests/stress/main.go @@ -28,7 +28,7 @@ type testCase struct { isQuery bool `json:"-"` numArgs int `json:"-"` Weight int `json:"weight"` - Sql string `json:"sql"` + SQL string `json:"sql"` Args []argument `json:"args"` } @@ -103,7 +103,7 @@ func (tc *testCase) buildSql() string { for i := 0; i < len(tc.Args); i++ { args = tc.Args[i].generate(args) } - return fmt.Sprintf(tc.Sql, args...) + return fmt.Sprintf(tc.SQL, args...) } type statitics struct { @@ -129,22 +129,19 @@ var ( cases []testCase ) -func loadTestCase(path string) error { - f, e := os.Open(path) - if e != nil { +func loadTestCaseFromFile(file *os.File) error { + if e := json.NewDecoder(file).Decode(&cases); e != nil { return e } - defer f.Close() - e = json.NewDecoder(f).Decode(&cases) - if e != nil { - return e + if len(cases) == 0 { + return fmt.Errorf("no test case loaded.") } for i := 0; i < len(cases); i++ { c := &cases[i] - c.Sql = strings.TrimSpace(c.Sql) - c.isQuery = strings.ToLower(c.Sql[:6]) == "select" + c.SQL = strings.TrimSpace(c.SQL) + c.isQuery = strings.ToLower(c.SQL[:6]) == "select" if c.Weight < 0 { return fmt.Errorf("test %d: negative weight", i) } @@ -171,6 +168,28 @@ func loadTestCase(path string) error { return nil } +func loadTestCase(pathOrSQL string) error { + if f, e := os.Open(pathOrSQL); e == nil { + defer f.Close() + return loadTestCaseFromFile(f) + } + + pathOrSQL = strings.TrimSpace(pathOrSQL) + if strings.ToLower(pathOrSQL[:6]) != "select" { + return fmt.Errorf("'%s' is not a valid file or SQL statement", pathOrSQL) + } + + cases = append(cases, testCase{ + isQuery: true, + Weight: 1, + numArgs: 0, + SQL: pathOrSQL, + }) + totalWeight = 1 + + return nil +} + func selectTestCase() *testCase { sum, target := 0, rand.Intn(totalWeight) var c *testCase @@ -297,16 +316,13 @@ func main() { flag.UintVar(&concurrency, "c", 4, "concurrency, number of goroutines for query") flag.Parse() - caseFile := flag.Arg(0) - if caseFile == "" { - caseFile = "cases.json" + pathOrSQL := flag.Arg(0) + if len(pathOrSQL) == 0 { + pathOrSQL = "cases.json" } - if e := loadTestCase(caseFile); e != nil { + if e := loadTestCase(pathOrSQL); e != nil { fmt.Println("failed to load test cases:", e.Error()) return - } else if len(cases) == 0 { - fmt.Println("there's no test case") - return } rand.Seed(time.Now().UnixNano())