aschult5 / actiontime

A Golang library for tracking action time averages
MIT License
0 stars 1 forks source link

Action Time

Build Status codecov

A thread-safe Golang library that accepts a json serialized string of the form below and maintains an average time for each action.

These inputs:

{"action":"jump", "time":100}
{"action":"run", "time":75}
{"action":"jump", "time":200}

Would lead to this output:

[{"action":"jump","avg":150},{"action":"run","avg":75}]

Disclaimer

I have never before written anything in GoLang, but it has elegant support for concurrency and json, which are core aspects of this project. It is also used by the folks that will be inspecting this project. You've been warned!

Usage

This module is intended to be imported from a go program.
Calls to AddAction and GetStats may be made concurrently.

Dependencies

Installation

go get github.com/aschult5/actiontime

Documentation

From go doc

package actiontime // import "github.com/aschult5/actiontime"

Package actiontime takes actions and times as json, tracking average times.
Input is received as a json string, per requirements.

var ErrBadInput = errors.New("actiontime: Malformed input data")
var MaxActionLen = 32
type Stats struct{ ... }

From go doc actiontime.Stats

package actiontime // import "github.com/aschult5/actiontime"

type Stats struct {
        // Has unexported fields.
}
    Stats tracks passed actions' average times.

func (a *Stats) AddAction(input string) error
func (a *Stats) GetStats() string

Example

package main

import (
        "fmt"
        "github.com/aschult5/actiontime"
)

func main() {
        var a actiontime.Stats
        a.AddAction(`{"action":"jump", "time":100}`)
        a.AddAction(`{"action":"fall", "time":100}`)
        a.AddAction(`{"action":"jump", "time":200}`)
        a.AddAction(`{"action":"fall", "time":200}`)
        a.AddAction(`{"action":"sit", "time":500}`)
        a.AddAction(`{"action":"stand", "time":700}`)

        fmt.Println(a.GetStats())
}

Possible output:

[{"action":"stand","avg":700},{"action":"jump","avg":150},{"action":"fall","avg":150},{"action":"sit","avg":500}]

Testing

Running Tests

go test [-v] [-race] [-short]

Note: Testing-race without -short may timeout.

Some test case files will need to be manually generated, as they create large files that probably don't belong in revision control.
You will be prompted with the command if you run go test.

Running Benchmarks

go test -bench . -benchmem -benchtime 10s -run=^$

See travis-ci for latest benchmark results.

Checking code coverage

go test -coverprofile=coverage.txt -covermode=atomic
go tool cover -func coverage.txt

See codecov for code coverage history.

Generating Tests

See python3 ./tools/testgenerator.py --help Generated tests will have to be manually integrated by adding a new Test* case to statsimpl_test.go

Design

Decisions

Assumptions

TODO

See github.com/aschult5/actiontime/issues

Notable improvements