Advertisement
Guest User

Untitled

a guest
May 26th, 2015
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. // import tmp/97g
  2. package main
  3.  
  4. import (
  5. "database/sql"
  6. "fmt"
  7. "log"
  8. "math/rand"
  9. "strconv"
  10. "time"
  11.  
  12. _ "github.com/go-sql-driver/mysql"
  13. )
  14.  
  15. /*
  16. var migrations = []string{
  17. `create table application (
  18. id int unsigned auto_increment not null primary key,
  19. organization varchar(255) not null,
  20. name varchar(255) not null,
  21. version varchar(255) not null,
  22. platform varchar(255) not null,
  23. unique index name (organization, name)
  24. )`,
  25. `create trigger application_unique before insert on application
  26. for each row
  27. begin
  28. declare found int;
  29. select count(*) into found from application where organization=new.organization and name=new.name and version=new.version and platform=new.platform;
  30. if found > 0 then
  31. signal sqlstate '45000' set MYSQL_ERRNO=1062, MESSAGE_TEXT='Duplicate entry';
  32. end if;
  33. end
  34. `,
  35. }
  36. */
  37.  
  38. var migrations = []string{
  39. `create table application (
  40. id int unsigned auto_increment not null primary key,
  41. organization varchar(255) not null,
  42. name varchar(255) not null,
  43. version varchar(255) not null,
  44. platform varchar(255) not null,
  45. unique index name (organization, name, version, platform)
  46. )`,
  47. }
  48.  
  49. type application struct {
  50. platform string
  51. version string
  52. organization string
  53. name string
  54. }
  55.  
  56. func main() {
  57. db0, err := sql.Open("mysql", "root:root@tcp(localhost:3306)/?columnsWithAlias=true&parseTime=true")
  58. if err != nil {
  59. log.Fatal(err)
  60. }
  61.  
  62. db0.Exec("drop database if exists 97g")
  63. _, err = db0.Exec("create database 97g")
  64. if err != nil {
  65. log.Fatal(err)
  66. }
  67.  
  68. defer db0.Exec("drop database 97g")
  69.  
  70. db, err := sql.Open("mysql", "root:root@tcp(localhost:3306)/97g?columnsWithAlias=true&parseTime=true")
  71. if err != nil {
  72. log.Fatal(err)
  73. }
  74.  
  75. for _, migration := range migrations {
  76. _, err := db.Exec(migration)
  77. if err != nil {
  78. log.Fatal(err)
  79. }
  80. }
  81.  
  82. db.SetMaxOpenConns(30)
  83.  
  84. queue := make(chan *application, 100)
  85. for i := 0; i < 30; i++ {
  86. go worker(db, queue)
  87. }
  88.  
  89. timeCheckpoint := time.Now()
  90. i := 0
  91. for {
  92. app := &application{
  93. platform: strconv.FormatInt(rand.Int63(), 36),
  94. version: strconv.FormatInt(rand.Int63(), 36),
  95. organization: strconv.FormatInt(rand.Int63(), 36),
  96. name: strconv.FormatInt(rand.Int63(), 36),
  97. }
  98.  
  99. queue <- app
  100.  
  101. i++
  102. if i%1000 == 0 {
  103. rows, err := db.Query("select * from application where id=?", rand.Intn(i))
  104. if err != nil {
  105. log.Fatal(err)
  106. }
  107. rows.Close()
  108.  
  109. newTimeCheckpoint := time.Now()
  110. fmt.Printf("%d: %s\n", i, newTimeCheckpoint.Sub(timeCheckpoint))
  111. timeCheckpoint = newTimeCheckpoint
  112. }
  113. }
  114. }
  115.  
  116. func worker(db *sql.DB, queue chan *application) {
  117. for app := range queue {
  118. _, err := db.Exec("insert into application (platform, version, organization, name) values(?, ?, ?, ?)", app.platform, app.version, app.organization, app.name)
  119. if err != nil {
  120. log.Printf("Error inserting %#v:\n\t%s\n", app, err)
  121. }
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement