|
| 1 | +package writestress |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "encoding/binary" |
| 7 | + "fmt" |
| 8 | + "math/rand" |
| 9 | + "sync" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/juju/errors" |
| 13 | + "github.com/ngaut/log" |
| 14 | + |
| 15 | + "github.com/pingcap/tipocket/pkg/cluster/types" |
| 16 | + "github.com/pingcap/tipocket/pkg/core" |
| 17 | + "github.com/pingcap/tipocket/util" |
| 18 | +) |
| 19 | + |
| 20 | +// Table schema comes from the bank of pufa `tmp_jieb_instmnt_daily` |
| 21 | +// CREATE TABLE `tmp_jieb_instmnt_daily` ( |
| 22 | +// `ID` bigint(20) DEFAULT NULL COMMENT '主键ID', |
| 23 | +// `TABLE_ID` int(11) NOT NULL COMMENT '分库ID', |
| 24 | +// `FILE_DATE` char(8) NOT NULL COMMENT '文件日期', |
| 25 | +// `CONTRACT_NO` varchar(128) NOT NULL COMMENT '借据号', |
| 26 | +// `SETTLE_DATE` char(8) NOT NULL COMMENT '减免会计日期', |
| 27 | +// `TERM_NO` int(11) NOT NULL COMMENT '期次号', |
| 28 | +// |
| 29 | +// `INPT_DATE` char(8) DEFAULT NULL COMMENT '录入日期', |
| 30 | +// `INPT_TIME` varchar(20) DEFAULT NULL COMMENT '录入时间', |
| 31 | +// `RCRD_ST_CODE` varchar(1) DEFAULT NULL COMMENT '记录状态代码', |
| 32 | +// UNIQUE KEY `TMP_JIEB_INSTMNT_DAILY_IDX1` (`CONTRACT_NO`,`TERM_NO`), |
| 33 | +// KEY `TMP_JIEB_INSTMNT_DAILY_IDX2` (`TABLE_ID`,`CONTRACT_NO`) |
| 34 | +// ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin/*!90000 SHARD_ROW_ID_BITS=5 PRE_SPLIT_REGIONS=5 */ COMMENT='借呗日终(分期)信息临时表'; |
| 35 | +const ( |
| 36 | + stmtDrop = `DROP TABLE IF EXISTS write_stress` |
| 37 | + stmtCreate = ` |
| 38 | + CREATE TABLE write_stress ( |
| 39 | + TABLE_ID int(11) NOT NULL COMMENT '分库ID', |
| 40 | + CONTRACT_NO varchar(128) NOT NULL COMMENT '借据号', |
| 41 | + TERM_NO int(11) NOT NULL COMMENT '期次号', |
| 42 | + NOUSE char(60) NOT NULL COMMENT '填充位', |
| 43 | + |
| 44 | + UNIQUE KEY TMP_JIEB_INSTMNT_DAILY_IDX1 (CONTRACT_NO, TERM_NO), |
| 45 | + KEY TMP_JIEB_INSTMNT_DAILY_IDX2 (TABLE_ID, CONTRACT_NO) |
| 46 | + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; |
| 47 | +` |
| 48 | +) |
| 49 | + |
| 50 | +// Config is for writestressClient |
| 51 | +type Config struct { |
| 52 | + DataNum int `toml:"dataNum"` |
| 53 | + Concurrency int `toml:"concurrency"` |
| 54 | + Batch int `toml:"batch"` |
| 55 | +} |
| 56 | + |
| 57 | +// ClientCreator creates writestressClient |
| 58 | +type ClientCreator struct { |
| 59 | + Cfg *Config |
| 60 | +} |
| 61 | + |
| 62 | +// Create ... |
| 63 | +func (l ClientCreator) Create(node types.ClientNode) core.Client { |
| 64 | + return &writestressClient{ |
| 65 | + Config: l.Cfg, |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// ledgerClient simulates a complete record of financial transactions over the |
| 70 | +// life of a bank (or other company). |
| 71 | +type writestressClient struct { |
| 72 | + *Config |
| 73 | + db *sql.DB |
| 74 | + contract_ids [][]byte |
| 75 | +} |
| 76 | + |
| 77 | +func (c *writestressClient) SetUp(ctx context.Context, nodes []types.ClientNode, idx int) error { |
| 78 | + if idx != 0 { |
| 79 | + return nil |
| 80 | + } |
| 81 | + |
| 82 | + var err error |
| 83 | + node := nodes[idx] |
| 84 | + dsn := fmt.Sprintf("root@tcp(%s:%d)/test", node.IP, node.Port) |
| 85 | + |
| 86 | + log.Infof("start to init...") |
| 87 | + c.db, err = util.OpenDB(dsn, c.Concurrency) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + defer func() { |
| 92 | + log.Infof("init end...") |
| 93 | + }() |
| 94 | + |
| 95 | + if _, err := c.db.Exec(stmtDrop); err != nil { |
| 96 | + log.Fatalf("execute statement %s error %v", stmtDrop, err) |
| 97 | + } |
| 98 | + |
| 99 | + if _, err := c.db.Exec(stmtCreate); err != nil { |
| 100 | + log.Fatalf("execute statement %s error %v", stmtCreate, err) |
| 101 | + } |
| 102 | + |
| 103 | + return nil |
| 104 | +} |
| 105 | + |
| 106 | +func (c *writestressClient) TearDown(ctx context.Context, nodes []types.ClientNode, idx int) error { |
| 107 | + return nil |
| 108 | +} |
| 109 | + |
| 110 | +func (c *writestressClient) Invoke(ctx context.Context, node types.ClientNode, r interface{}) core.UnknownResponse { |
| 111 | + panic("implement me") |
| 112 | +} |
| 113 | + |
| 114 | +func (c *writestressClient) NextRequest() interface{} { |
| 115 | + panic("implement me") |
| 116 | +} |
| 117 | + |
| 118 | +func (c *writestressClient) DumpState(ctx context.Context) (interface{}, error) { |
| 119 | + panic("implement me") |
| 120 | +} |
| 121 | + |
| 122 | +func (c *writestressClient) Start(ctx context.Context, cfg interface{}, clientNodes []types.ClientNode) error { |
| 123 | + log.Infof("start to test...") |
| 124 | + defer func() { |
| 125 | + log.Infof("test end...") |
| 126 | + }() |
| 127 | + c.contract_ids = make([][]byte, c.DataNum) |
| 128 | + timeUnix := time.Now().Unix() |
| 129 | + count := uint8(0) |
| 130 | + b := make([]byte, 8) |
| 131 | + for i := 0; i < c.DataNum; i++ { |
| 132 | + // "abcd" + timestamp(8 bit) + count(8 bit) |
| 133 | + c.contract_ids[i] = append(c.contract_ids[i], []byte("abcd")...) |
| 134 | + binary.LittleEndian.PutUint64(b, uint64(timeUnix)) |
| 135 | + c.contract_ids[i] = append(c.contract_ids[i], b...) |
| 136 | + binary.LittleEndian.PutUint64(b, uint64(count)) |
| 137 | + c.contract_ids[i] = append(c.contract_ids[i], b...) |
| 138 | + |
| 139 | + count++ |
| 140 | + if count == 0 { |
| 141 | + timeUnix++ |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + var wg sync.WaitGroup |
| 146 | + for i := 0; i < c.Concurrency; i++ { |
| 147 | + wg.Add(1) |
| 148 | + go func(i int) { |
| 149 | + defer wg.Done() |
| 150 | + for { |
| 151 | + select { |
| 152 | + case <-ctx.Done(): |
| 153 | + return |
| 154 | + default: |
| 155 | + } |
| 156 | + if err := c.ExecuteInsert(c.db, i); err != nil { |
| 157 | + log.Fatalf("exec failed %v", err) |
| 158 | + } |
| 159 | + } |
| 160 | + }(i) |
| 161 | + } |
| 162 | + |
| 163 | + wg.Wait() |
| 164 | + return nil |
| 165 | +} |
| 166 | + |
| 167 | +// ExecuteInsert is run case |
| 168 | +func (c *writestressClient) ExecuteInsert(db *sql.DB, pos int) error { |
| 169 | + num := c.Config.DataNum * 10000 / c.Config.Concurrency |
| 170 | + |
| 171 | + tx, err := db.Begin() |
| 172 | + if err != nil { |
| 173 | + return errors.Trace(err) |
| 174 | + } |
| 175 | + defer tx.Rollback() |
| 176 | + str := make([]byte, 50) |
| 177 | + rnd := rand.New(rand.NewSource(time.Now().Unix())) |
| 178 | + for i := 0; i < num/c.Config.Batch; i++ { |
| 179 | + n := num*pos + i*c.Config.Batch |
| 180 | + if n >= c.DataNum { |
| 181 | + break |
| 182 | + } |
| 183 | + query := fmt.Sprintf(`INSERT INTO write_stress (TABLE_ID, CONTRACT_NO, TERM_NO, NOUSE) VALUES `) |
| 184 | + for j := 0; j < c.Config.Batch; j++ { |
| 185 | + n := num*pos + i*c.Config.Batch + j |
| 186 | + if n >= c.DataNum { |
| 187 | + break |
| 188 | + } |
| 189 | + contract_id := c.contract_ids[n] |
| 190 | + util.RandString(str, rnd) |
| 191 | + if j != 0 { |
| 192 | + query += "," |
| 193 | + } |
| 194 | + query += fmt.Sprintf(`(%v, %v, %v, %v)`, rnd.Uint32()%960+1, string(contract_id[:]), rnd.Uint32()%36+1, string(str[:])) |
| 195 | + } |
| 196 | + fmt.Println(query) |
| 197 | + if _, err := tx.Exec(query); err != nil { |
| 198 | + return errors.Trace(err) |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + if err := tx.Commit(); err != nil { |
| 203 | + return errors.Trace(err) |
| 204 | + } |
| 205 | + |
| 206 | + return nil |
| 207 | +} |
0 commit comments