Skip to content

Commit

Permalink
adding a new feature: corrupting MySQL by forcing it to use a lot of …
Browse files Browse the repository at this point in the history
…CPU.
  • Loading branch information
bo-er committed Jan 11, 2024
1 parent 83e5994 commit 9e38b0c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ Corrupting MySQL and testing how the monitoring system reacts.
./corrupt-mysql bt -H10.186.62.63 -P25690 -uuniverse_udb -p123 100MB
```

![example](./pics/bt.jpg)
![example](./pics/bt.jpg)

## Make MySQL's CPU usage high

```bash
./corrupt-mysql hcpu -H10.186.62.63 -P25690 -uuniverse_udb -p123
```
45 changes: 45 additions & 0 deletions backend/high_cpu_usage.go
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
}
27 changes: 27 additions & 0 deletions cmd/high_cpu_usage.go
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())
}
},
}

0 comments on commit 9e38b0c

Please sign in to comment.