
test.mysql.MySQL.kt Maven / Gradle / Ivy
package test.mysql
import cn.hutool.core.util.RandomUtil
import cn.hutool.db.Db
import cn.hutool.db.DbUtil
import jchanghong.kotlin.log
import org.springframework.boot.CommandLineRunner
import org.springframework.stereotype.Service
//@Service
class MySQL:CommandLineRunner{
lateinit var myria: Db
lateinit var pg: Db
val sum=5000000
val ranStr=RandomUtil.randomString(1024)
override fun run(vararg args: String?) {
val myriadb = DbUtil.getDs("mariadb")
val postgres = DbUtil.getDs("postgres")
myria = DbUtil.use(myriadb)
pg = DbUtil.use(postgres)
myriadb.connection.close()
postgres.connection.close()
log.info("mysql 测试。。。")
// inittable()
// testpginsert()
testmysqlinsert()
// testpgquery()
testmysqlquery()
}
private fun testmysqlquery() {
val start = System.currentTimeMillis()
(1..100).toList().parallelStream().forEach {
myria.query("""
select * from test426 where name like '%${RandomUtil.randomString(20)}%'
""".trimIndent())
}
(1..100).toList().parallelStream().forEach {
myria.query("""
select * from test426 where name = '%${RandomUtil.randomString(20)}%'
""".trimIndent())
}
println("mysql查询时间:"+(System.currentTimeMillis()-start))
}
private fun testpgquery() {
val start = System.currentTimeMillis()
(1..100).toList().parallelStream().forEach {
pg.query("""
select * from test426 where name like '%${RandomUtil.randomString(20)}%'
""".trimIndent())
}
(1..100).toList().parallelStream().forEach {
pg.query("""
select * from test426 where name = '%${RandomUtil.randomString(20)}%'
""".trimIndent())
}
println("pg查询时间:"+(System.currentTimeMillis()-start))
}
private fun testmysqlinsert() {
val start = System.currentTimeMillis()
(1..sum).toList().parallelStream().forEach {
myria.execute("""
insert into test426( name) values ('$ranStr')
""".trimIndent())
}
println("mysql插入时间:"+(System.currentTimeMillis()-start))
}
private fun testpginsert() {
val start = System.currentTimeMillis()
(1..sum).toList().parallelStream().forEach {
pg.execute("""
insert into test426( name) values ('$ranStr')
""".trimIndent())
}
println("pg插入时间:"+(System.currentTimeMillis()-start))
}
private fun inittable() {
myria.execute("""
drop table test426
""".trimIndent())
myria.execute("""
create table if not exists test426(
id bigint auto_increment not null primary key ,
name VARCHAR(1024) not null ,
time timestamp not null default now()
) engine=Aria
TRANSACTIONAL=0
PAGE_CHECKSUM=0
TABLE_CHECKSUM=0
ROW_FORMAT=FIXED
""".trimIndent())
pg.execute("""
drop table test426
""".trimIndent())
pg.execute("""
create table if not exists test426(
id bigserial not null primary key ,
name text not null ,
time timestamp not null default now()
)
""".trimIndent())
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy