Skip to content

Commit

Permalink
first version
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicola Strappazzon committed Oct 20, 2019
1 parent dc5925a commit c6804e5
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.test
.DS_Store
chainsaw
Gopkg.lock
pkg/
vendor/
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
SHELL := /bin/bash

.PHONY: help

help: ## Show this help.
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-40s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)

deps: ## Install dependencies
go get -u github.com/golang/dep/cmd/dep
go get -u github.com/go-sql-driver/mysql

tests: ## Run tests
go test -v ./...

build: ## Build binary for local operating system
go build -o chainsaw main.go
68 changes: 68 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package main

import (
"fmt"
"time"
)

type Chunk struct {
Count uint64 // Rows in table.
Delta uint64 // Delta value.
Total uint64 // Count + delta.
Index uint64 // Number of chunks.
Steps uint64 // Numbers of chunks.
Length uint64 // Length of chunk.
Percentage uint64 // Percentage overall process.
Sleep time.Duration // Wait between chunk and chunk.
Start uint64 // Start chunk number.
End uint64 // End chunk number.
StartTime time.Time // Start each chunk.
EndTime time.Time // End each chunk.
Duration time.Duration // Duration time between start and end duration.
ETA time.Duration // Estimated time of arrival to complete overall process.
}

func main() {
c := Chunk{Count: 1000, Delta: 100, Length: 100, Sleep: 2 * time.Second}
c.Loop(func(){

})

// fmt.Printf("%#v\n", c)
}

func (c *Chunk) Calculate() {
c.Total = c.Count + c.Delta
c.Steps = c.Total / c.Length
c.Percentage = ((100 * c.Index) / c.Total)
c.Duration = c.EndTime.Sub(c.StartTime)
c.ETA = time.Duration(int64(c.Total - c.Index)) * c.Duration

//start = ((c.Index * c.Length) - c.Length) + 1;
//end = (c.Index * c.Length);
}

func (c *Chunk) Loop(f interface{}) {
c.Calculate()
for i := c.Start; i <= c.Total; i++ {
if i % c.Length == 0 {
c.StartTime = time.Now()

f.(func())()

time.Sleep(c.Sleep)

c.EndTime = time.Now()
c.Index = i
c.Calculate()
c.Log()
}
}
}

func (c *Chunk) Log() {
fmt.Printf("Chunk: %0.5d/%0.5d %0.2d%% %v.\n", c.Index, c.Total, c.Percentage, c.ETA.Minutes())
}

// $chunk_duration = ceil(($chunk_end_time + $chunk_duration) / 2);
// $chunk_eta = (($chunk_total - $chunk_position) - 1) * $chunk_duration;
80 changes: 80 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package main_test

import (
"fmt"
"testing"
"os"
"database/sql"

_ "github.com/go-sql-driver/mysql"

"github.com/swapbyt3s/chainsaw"
)

var db *sql.DB
var err error

func TestMain(t *testing.M) {
db, err = sql.Open("mysql", "root:admin@tcp(127.0.0.1:3306)/")
if err != nil {
panic(err.Error())
}
defer db.Close()

setupDatabase()

code := t.Run()

os.Exit(code)
}

func setupDatabase() {
migrations := []string{
"DROP DATABASE IF EXISTS demo_test;",
"CREATE DATABASE IF NOT EXISTS demo_test;",
"USE demo_test;",
`CREATE TABLE IF NOT EXISTS demo_test.foo (
id int NOT NULL auto_increment,
value char(32) NOT NULL,
token char(32),
PRIMARY KEY (id)
);`,
}

for i := 1; i <= 100; i++ {
migrations = append(migrations, fmt.Sprintf("INSERT INTO demo_test.foo (value) VALUES (MD5('%d'));", i))
}

for _, migration := range migrations {
_, err := db.Query(migration)
if err != nil {
panic(err.Error())
}
}

}

//func TestSomeFeature(t *testing.T) {
// rows, err := db.Query("SELECT id, value FROM demo_test.foo")
// if err != nil {
// panic(err.Error())
// }
//
// for rows.Next() {
// var id int
// var val string
//
// if err := rows.Scan(&id, &val); err != nil {
// panic(err)
// }
//
// fmt.Printf("%d\t%s\n", id, val)
// }
//}

func TestChunkCalculate(t *testing.T) {
c := main.Chunk{Count: 1000, Delta: 100, Length: 100, Index: 550}
c.Calculate()
fmt.Printf("%d\n", c.Percentage)
fmt.Printf("%d\n", c.Steps)
}

0 comments on commit c6804e5

Please sign in to comment.