-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding a new feature: corrupting MySQL by forcing it to use a lot of …
…CPU.
- Loading branch information
Showing
3 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package backend | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/bo-er/corrupt-mysql/pkg" | ||
) | ||
|
||
const prepareHighCPUUsageSQL = ` | ||
DROP DATABASE IF EXISTS corrupt_mysql_highcpuusage_test; | ||
CREATE DATABASE corrupt_mysql_highcpuusage_test; | ||
USE corrupt_mysql_highcpuusage_test; | ||
DROP TABLE IF EXISTS t; | ||
CREATE TABLE t(x int primary key auto_increment); | ||
INSERT INTO t() values(),(),(),(); | ||
` | ||
|
||
func MakeCPUUsageHigh(c pkg.Connect) error { | ||
db, err := pkg.GetDB(c) | ||
if err != nil { | ||
return err | ||
} | ||
err = pkg.BatchExec(db, prepareBigTransactionSQL) | ||
if err != nil { | ||
return err | ||
} | ||
// maybe there is a better way to do this. | ||
_, err = db.Exec(`USE corrupt_mysql_highcpuusage_test;`) | ||
if err != nil { | ||
return fmt.Errorf("use corrupt_mysql_highcpuusage_test: %s", err.Error()) | ||
} | ||
// preparing some data | ||
for i := 1; i <= 20; i++ { | ||
_, err = db.Exec(quadraticGrowthSQL) | ||
if err != nil { | ||
return fmt.Errorf("calling(%s) for the %dth time: %s", quadraticGrowthSQL, i, err.Error()) | ||
} | ||
} | ||
// calling rand() on each entry. | ||
_, err = db.Exec(`SELECT * FROM t order by rand() limit 1;`) | ||
if err != nil { | ||
return fmt.Errorf("calling rand() on each entry: %s", err.Error()) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/bo-er/corrupt-mysql/backend" | ||
"github.com/bo-er/corrupt-mysql/pkg" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// highCPUCmd increases mysql's cpu usage dramatically. | ||
var highCPUCmd = &cobra.Command{ | ||
Use: "hcpu", | ||
Short: "increasing mysql's cpu usage dramatically", | ||
Long: `./corrupt-mysql hcpu -H10.186.62.63 -P25690 -uuniverse_udb -p123`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
connect := pkg.Connect{ | ||
User: user, | ||
Host: host, | ||
Password: password, | ||
DBName: "mysql", | ||
Port: port, | ||
} | ||
err := backend.MakeCPUUsageHigh(connect) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
}, | ||
} |