Skip to content

Commit

Permalink
Add ClickHouse database support
Browse files Browse the repository at this point in the history
ClickHouse can be tested along with all other DBses.
ClickHouse implementation is based on TimescaleDB.
  • Loading branch information
sunsingerus committed Oct 30, 2018
1 parent cd0377f commit 72b6f0a
Show file tree
Hide file tree
Showing 51 changed files with 2,756 additions and 308 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
*.out
*.log
.DS_Store
.idea
20 changes: 16 additions & 4 deletions cmd/tsbs_generate_data/common/distribution.go
Expand Up @@ -21,7 +21,10 @@ type NormalDistribution struct {

// ND creates a new normal distribution with the given mean/stddev
func ND(mean, stddev float64) *NormalDistribution {
return &NormalDistribution{Mean: mean, StdDev: stddev}
return &NormalDistribution{
Mean: mean,
StdDev: stddev,
}
}

// Advance advances this distribution. Since the distribution is
Expand All @@ -45,7 +48,10 @@ type UniformDistribution struct {

// UD creates a new uniform distribution with the given range
func UD(low, high float64) *UniformDistribution {
return &UniformDistribution{Low: low, High: high}
return &UniformDistribution{
Low: low,
High: high,
}
}

// Advance advances this distribution. Since the distribution is
Expand All @@ -72,7 +78,10 @@ type RandomWalkDistribution struct {

// WD creates a new RandomWalkDistribution based on a given distribution and starting state
func WD(step Distribution, state float64) *RandomWalkDistribution {
return &RandomWalkDistribution{Step: step, State: state}
return &RandomWalkDistribution{
Step: step,
State: state,
}
}

// Advance computes the next value of this distribution and stores it.
Expand Down Expand Up @@ -147,7 +156,10 @@ func (d *MonotonicRandomWalkDistribution) Get() float64 {

// MWD creates a new MonotonicRandomWalkDistribution with a given distribution and initial state
func MWD(step Distribution, state float64) *MonotonicRandomWalkDistribution {
return &MonotonicRandomWalkDistribution{Step: step, State: state}
return &MonotonicRandomWalkDistribution{
Step: step,
State: state,
}
}

// ConstantDistribution is a stateful distribution that always returns the same value
Expand Down
2 changes: 1 addition & 1 deletion cmd/tsbs_generate_data/common/simulation.go
Expand Up @@ -8,7 +8,7 @@ import (

// SimulatorConfig is an interface to create a Simulator from a time.Duration
type SimulatorConfig interface {
ToSimulator(time.Duration) Simulator
NewSimulator(time.Duration, uint64) Simulator
}

// Simulator simulates a use case.
Expand Down
8 changes: 6 additions & 2 deletions cmd/tsbs_generate_data/devops/cpu_only_generate_data.go
Expand Up @@ -20,6 +20,7 @@ func (d *CPUOnlySimulator) Fields() map[string][][]byte {

// Next advances a Point to the next state in the generator.
func (d *CPUOnlySimulator) Next(p *serialize.Point) bool {
// switch to the next metric if needed
if d.hostIndex == uint64(len(d.hosts)) {
d.hostIndex = 0

Expand All @@ -36,15 +37,18 @@ func (d *CPUOnlySimulator) Next(p *serialize.Point) bool {
// CPUOnlySimulatorConfig is used to create a CPUOnlySimulator.
type CPUOnlySimulatorConfig commonDevopsSimulatorConfig

// ToSimulator produces a Simulator that conforms to the given SimulatorConfig over the specified interval
func (c *CPUOnlySimulatorConfig) ToSimulator(interval time.Duration) common.Simulator {
// NewSimulator produces a Simulator that conforms to the given SimulatorConfig over the specified interval
func (c *CPUOnlySimulatorConfig) NewSimulator(interval time.Duration, limit uint64) common.Simulator {
hostInfos := make([]Host, c.HostCount)
for i := 0; i < len(hostInfos); i++ {
hostInfos[i] = c.HostConstructor(i, c.Start)
}

epochs := calculateEpochs(commonDevopsSimulatorConfig(*c), interval)
maxPoints := epochs * c.HostCount
if limit > 0 && limit < maxPoints {
maxPoints = limit
}
sim := &CPUOnlySimulator{&commonDevopsSimulator{
madePoints: 0,
maxPoints: maxPoints,
Expand Down
8 changes: 4 additions & 4 deletions cmd/tsbs_generate_data/devops/cpu_only_generate_data_test.go
Expand Up @@ -19,7 +19,7 @@ var (
)

func TestCPUOnlySimulatorFields(t *testing.T) {
s := testCPUOnlyConf.ToSimulator(time.Second).(*CPUOnlySimulator)
s := testCPUOnlyConf.NewSimulator(time.Second, 0).(*CPUOnlySimulator)
fields := s.Fields()
if got := len(fields); got != 1 {
t.Errorf("fields length does not equal 1: got %d", got)
Expand All @@ -45,7 +45,7 @@ func TestCPUOnlySimulatorFields(t *testing.T) {
}

func TestCPUOnlySimulatorNext(t *testing.T) {
s := testCPUOnlyConf.ToSimulator(time.Second).(*CPUOnlySimulator)
s := testCPUOnlyConf.NewSimulator(time.Second, 0).(*CPUOnlySimulator)
// There are two epochs for the test configuration, and a difference of 90
// from init to final, so each epoch should add 45 devices to be written.
writtenIdx := []int{10, 55, 100}
Expand Down Expand Up @@ -77,7 +77,7 @@ func TestCPUOnlySimulatorNext(t *testing.T) {
runFn(3)
}

func TestCPUOnlySimulatorConfigToSimulator(t *testing.T) {
func TestCPUOnlySimulatorConfigNewSimulator(t *testing.T) {
duration := time.Second
start := time.Now()
end := start.Add(10 * time.Second)
Expand All @@ -90,7 +90,7 @@ func TestCPUOnlySimulatorConfigToSimulator(t *testing.T) {
HostCount: numHosts,
HostConstructor: NewHostCPUOnly,
}
sim := conf.ToSimulator(duration).(*CPUOnlySimulator)
sim := conf.NewSimulator(duration, 0).(*CPUOnlySimulator)
if got := sim.madePoints; got != 0 {
t.Errorf("incorrect initial points: got %d want %d", got, 0)
}
Expand Down
7 changes: 5 additions & 2 deletions cmd/tsbs_generate_data/devops/generate_data.go
Expand Up @@ -38,15 +38,18 @@ func (d *DevopsSimulator) Next(p *serialize.Point) bool {
// DevopsSimulatorConfig is used to create a DevopsSimulator.
type DevopsSimulatorConfig commonDevopsSimulatorConfig

// ToSimulator produces a Simulator that conforms to the given SimulatorConfig over the specified interval
func (d *DevopsSimulatorConfig) ToSimulator(interval time.Duration) common.Simulator {
// NewSimulator produces a Simulator that conforms to the given SimulatorConfig over the specified interval
func (d *DevopsSimulatorConfig) NewSimulator(interval time.Duration, limit uint64) common.Simulator {
hostInfos := make([]Host, d.HostCount)
for i := 0; i < len(hostInfos); i++ {
hostInfos[i] = d.HostConstructor(i, d.Start)
}

epochs := calculateEpochs(commonDevopsSimulatorConfig(*d), interval)
maxPoints := epochs * d.HostCount * uint64(len(hostInfos[0].SimulatedMeasurements))
if limit > 0 && limit < maxPoints {
maxPoints = limit
}
dg := &DevopsSimulator{
commonDevopsSimulator: &commonDevopsSimulator{
madePoints: 0,
Expand Down
6 changes: 3 additions & 3 deletions cmd/tsbs_generate_data/devops/generate_data_test.go
Expand Up @@ -18,7 +18,7 @@ var testDevopsConf = &DevopsSimulatorConfig{
}

func TestDevopsSimulatorNext(t *testing.T) {
s := testDevopsConf.ToSimulator(time.Second).(*DevopsSimulator)
s := testDevopsConf.NewSimulator(time.Second, 0).(*DevopsSimulator)
// There are two epochs for the test configuration, and a difference of 90
// from init to final, so each epoch should add 45 devices to be written.
writtenIdx := []int{10, 55, 100}
Expand Down Expand Up @@ -52,7 +52,7 @@ func TestDevopsSimulatorNext(t *testing.T) {
runFn(3)
}

func TestDevopsSimulatorConfigToSimulator(t *testing.T) {
func TestDevopsSimulatorConfigNewSimulator(t *testing.T) {
duration := time.Second
start := time.Now()
end := start.Add(10 * time.Second)
Expand All @@ -65,7 +65,7 @@ func TestDevopsSimulatorConfigToSimulator(t *testing.T) {
HostCount: numHosts,
HostConstructor: NewHost,
}
sim := conf.ToSimulator(duration).(*DevopsSimulator)
sim := conf.NewSimulator(duration, 0).(*DevopsSimulator)
if got := sim.madePoints; got != 0 {
t.Errorf("incorrect initial points: got %d want %d", got, 0)
}
Expand Down

0 comments on commit 72b6f0a

Please sign in to comment.