Skip to content

Commit

Permalink
benchmarks added to as and xmath, xtime package added
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonWaldherr committed Mar 13, 2016
1 parent 271d3a5 commit a15dcd5
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .hound.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go:
enabled: true
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ import "simonwaldherr.de/go/golibs/foreach"

**foreach** calls a given function for each element of a [ JSON-string ]

### gcurses - [![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat-square)](https://godoc.org/github.com/SimonWaldherr/golibs/gcurses) [![Coverage Status](https://img.shields.io/badge/coverage-90%25-green.svg?style=flat-square)](https://coveralls.io/r/SimonWaldherr/golibs) [![Build Status](https://img.shields.io/badge/build-passing-brightgreen.svg?style=flat-square)](https://travis-ci.org/SimonWaldherr/golibs)
### gcurses - [![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat-square)](https://godoc.org/github.com/SimonWaldherr/golibs/gcurses) [![Coverage Status](https://img.shields.io/badge/coverage-97%25-brightgreen.svg?style=flat-square)](https://coveralls.io/r/SimonWaldherr/golibs) [![Build Status](https://img.shields.io/badge/build-passing-brightgreen.svg?style=flat-square)](https://travis-ci.org/SimonWaldherr/golibs)

```go
import "simonwaldherr.de/go/golibs/gcurses"
Expand Down Expand Up @@ -411,3 +411,17 @@ fmt.Printf("Arithmetic: %v\n", xmath.Arithmetic(f))
fmt.Printf("Harmonic: %v\n", xmath.Harmonic(f))
fmt.Printf("Geometric: %v\n", xmath.Geometric(f))
```
### xtime - [![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat-square)](https://godoc.org/github.com/SimonWaldherr/golibs/xtime) [![Coverage Status](https://img.shields.io/badge/coverage-75%25-yellowgreen.svg?style=flat-square)](https://coveralls.io/r/SimonWaldherr/golibs) [![Build Status](https://img.shields.io/badge/build-passing-brightgreen.svg?style=flat-square)](https://travis-ci.org/SimonWaldherr/golibs)
```go
import "simonwaldherr.de/go/golibs/xtime"
```
**xmath** implements a subset of [strftime](http://man7.org/linux/man-pages/man3/strftime.3.html)
```go
t, _ := time.Now()
fmt.Println(xtime.Fmt("%Y-%m-%d %H:%M:%S", t))
}
```
72 changes: 72 additions & 0 deletions as/as_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ func Test_Bool(t *testing.T) {
}
}

func Benchmark_Bool(b *testing.B) {
for i := 0; i < b.N; i++ {
Bool(i)
}
}

func Test_Bytes(t *testing.T) {
if String(Bytes([]byte{'l', 'o', 'r', 'e', 'm'})) != String(Bytes("lorem")) {
t.Fatalf("Bytes Test failed")
Expand All @@ -41,6 +47,12 @@ func Test_Bytes(t *testing.T) {
}
}

func Benchmark_Bytes(b *testing.B) {
for i := 0; i < b.N; i++ {
Bytes(i)
}
}

func Test_Duration(t *testing.T) {
if String(Duration(42)) != "42ns" {
t.Fatalf("Duration Test failed")
Expand All @@ -56,6 +68,12 @@ func Test_Duration(t *testing.T) {
}
}

func Benchmark_Duration(b *testing.B) {
for i := 0; i < b.N; i++ {
Duration(i)
}
}

func Test_FixedLengthAfter(t *testing.T) {
if FixedLengthAfter("13.37", " ", 7) != "13.37 " {
t.Fatalf("FixedLengthAfter Test 1 failed")
Expand All @@ -68,6 +86,12 @@ func Test_FixedLengthAfter(t *testing.T) {
}
}

func Benchmark_FixedLengthAfter(b *testing.B) {
for i := 0; i < b.N; i++ {
FixedLengthAfter(string(i), " ", 8)
}
}

func Test_FixedLengthBefore(t *testing.T) {
if FixedLengthBefore("13.37", " ", 7) != " 13.37" {
t.Fatalf("FixedLengthBefore Test 1 failed")
Expand All @@ -80,6 +104,12 @@ func Test_FixedLengthBefore(t *testing.T) {
}
}

func Benchmark_FixedLengthBefore(b *testing.B) {
for i := 0; i < b.N; i++ {
FixedLengthBefore(string(i), " ", 8)
}
}

func Test_FixedLengthCenter(t *testing.T) {
if FixedLengthCenter("13.37", " ", 7) != " 13.37 " {
t.Fatalf("FixedLengthCenter Test 1 failed")
Expand All @@ -95,6 +125,12 @@ func Test_FixedLengthCenter(t *testing.T) {
}
}

func Benchmark_FixedLengthCenter(b *testing.B) {
for i := 0; i < b.N; i++ {
FixedLengthCenter(string(i), " ", 15)
}
}

func Test_Float(t *testing.T) {
if Float("13.37") != float64(13.37) {
t.Fatalf("Float Test failed")
Expand Down Expand Up @@ -146,6 +182,12 @@ func Test_Float(t *testing.T) {
}
}

func Benchmark_Float(b *testing.B) {
for i := 0; i < b.N; i++ {
Float(i)
}
}

func Test_FloatFromXString(t *testing.T) {
if FloatFromXString("13.000.000,00") != float64(13000000) {
t.Fatalf("FloatFromXString Test failed")
Expand Down Expand Up @@ -173,6 +215,12 @@ func Test_FloatFromXString(t *testing.T) {
}
}

func Benchmark_FloatFromXString(b *testing.B) {
for i := 0; i < b.N; i++ {
FloatFromXString(string(i))
}
}

func Test_Int(t *testing.T) {
if Int(int8(1)) != int64(1) {
t.Fatalf("Int Test failed")
Expand Down Expand Up @@ -221,6 +269,12 @@ func Test_Int(t *testing.T) {
}
}

func Benchmark_Int(b *testing.B) {
for i := 0; i < b.N; i++ {
Int(i)
}
}

func Test_String(t *testing.T) {
if String(nil) != "" {
t.Fatalf("String Test failed")
Expand All @@ -245,6 +299,12 @@ func Test_String(t *testing.T) {
}
}

func Benchmark_String(b *testing.B) {
for i := 0; i < b.N; i++ {
String(i)
}
}

func Test_Time(t *testing.T) {
if String(Time("11.01.2015")) != "2015-01-11T00:00:00Z" {
t.Fatalf("Time Test failed")
Expand All @@ -254,6 +314,12 @@ func Test_Time(t *testing.T) {
}
}

func Benchmark_Time(b *testing.B) {
for i := 0; i < b.N; i++ {
Time(i)
}
}

func Test_Uint(t *testing.T) {
if Uint(int(0)) != uint64(0) {
t.Fatalf("Uint Test failed")
Expand Down Expand Up @@ -305,6 +371,12 @@ func Test_Uint(t *testing.T) {
}
}

func Benchmark_Uint(b *testing.B) {
for i := 0; i < b.N; i++ {
Uint(i)
}
}

func printlnType(t *testing.T, str, type1, type2 string) {
t1, err := Type(str)

Expand Down
3 changes: 2 additions & 1 deletion coverage.log
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ cachedfile coverage: 96.0% of statements
channel coverage: 100.0% of statements
file coverage: 92.7% of statements
foreach coverage: 85.7% of statements
gcurses coverage: 90.9% of statements
gcurses coverage: 97.0% of statements
gopath coverage: 93.1% of statements
graphics coverage: 100.0% of statements
log coverage: 100.0% of statements
Expand All @@ -17,3 +17,4 @@ rss coverage: 92.9% of statements
ssl coverage: 87.2% of statements
stack coverage: 99.0% of statements
xmath coverage: 100.0% of statements
xtime coverage: 75.0% of statements
2 changes: 1 addition & 1 deletion pre-commit
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh

export GO15VENDOREXPERIMENT=1
declare -a libs=(ansi arg as cache cachedfile channel file foreach gcurses gopath graphics log re regex rss ssl stack xmath)
declare -a libs=(ansi arg as cache cachedfile channel file foreach gcurses gopath graphics log re regex rss ssl stack xmath xtime)

echo "COVERAGE TEST" > coverage.log

Expand Down
38 changes: 38 additions & 0 deletions xmath/math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ func Test_Sqrt(t *testing.T) {
}
}

func Benchmark_Sqrt(b *testing.B) {
for i := 0; i < b.N; i++ {
Sqrt(int64(i))
}
}

func Test_Prime(t *testing.T) {
if Prime(99) != 523 {
t.Fatalf("Prime Test failed")
Expand Down Expand Up @@ -153,6 +159,38 @@ func Test_Mean(t *testing.T) {
}
}

func Benchmark_Median(b *testing.B) {
var prev int64 = 0
for i := 0; i < b.N; i++ {
Median([]int64{int64(i), prev})
prev = int64(i)
}
}

func Benchmark_Arithmetic(b *testing.B) {
var prev int64 = 0
for i := 0; i < b.N; i++ {
Arithmetic([]int64{int64(i), prev})
prev = int64(i)
}
}

func Benchmark_Harmonic(b *testing.B) {
var prev int64 = 0
for i := 0; i < b.N; i++ {
Harmonic([]int64{int64(i), prev})
prev = int64(i)
}
}

func Benchmark_Geometric(b *testing.B) {
var prev int64 = 0
for i := 0; i < b.N; i++ {
Geometric([]int64{int64(i), prev})
prev = int64(i)
}
}

func Test_Odd_Even(t *testing.T) {
if Odd(3) != true {
t.Fatalf("Odd Test 1 failed")
Expand Down
24 changes: 24 additions & 0 deletions xtime/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package xtime_test

import (
xtime "../xtime/"
"fmt"
"time"
//"simonwaldherr.de/go/golibs/xtime"
)

func ExampleFmt() {
t := time.Unix(1234567890, 0)
cet, _ := time.LoadLocation("CET")
t = t.In(cet)
fmt.Println(xtime.Fmt("%Y-%m-%d %H:%M:%S", t))
// Output:
// 2009-02-14 00:31:30
}

func ExampleFmt2() {
t, _ := time.Parse(time.ANSIC, "Mon Jan 02 15:04:05 2006")
fmt.Println(xtime.Fmt("%Y-%m-%d %H:%M:%S", t))
// Output:
// 2006-01-02 15:04:05
}
60 changes: 60 additions & 0 deletions xtime/time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package xtime

import (
"time"
)

var conv = map[rune]string{
'a': "Mon",
'A': "Monday",
'b': "Jan",
'B': "January",
'd': "02",
'D': "01-02-2006",
'F': "2006-01-02",
'H': "15",
'I': "03",
'L': ".000",
'm': "01",
'M': "04",
'p': "PM",
'S': "05",
'x': "2006-01-02",
'X': "15:04:05",
'y': "06",
'Y': "2006",
'z': "-0700",
'Z': "MST",
'0': "",
'%': "",
}

// StrfTime implements a subset of strftime
// http://man7.org/linux/man-pages/man3/strftime.3.html
func StrfTime(format string, t time.Time) string {
ret := make([]byte, 0, len(format))
for i := 0; i < len(format); i++ {
if format[i] == '%' {
if layout, ok := conv[rune(format[i+1])]; ok {
ret = append(ret, []byte(t.Format(layout))...)
i++
} else {
ret = append(ret, format[i])
}
} else {
ret = append(ret, format[i])
}
}
return string(ret)
}

// Fmt (xtime.Fmt) is an alias for StrfTime
func Fmt(format string, t time.Time) string {
return StrfTime(format, t)
}

// FmtNow is like StrfTime, but automatically with the current local time
func FmtNow(format string) string {
t := time.Now()
return StrfTime(format, t)
}
27 changes: 27 additions & 0 deletions xtime/time_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package xtime

import (
"testing"
"time"
)

func Test_Fmt(t *testing.T) {
tt := time.Unix(1234571490, 0)
cet, _ := time.LoadLocation("CET")
tt = tt.In(cet)
if Fmt("%Y-%m-%d %H:%M:%S", tt) != "2009-02-14 01:31:30" {
t.Fatalf("%s != %s", Fmt("%Y-%m-%d %H:%M:%S", tt), "2009-02-14 01:31:30")
}
}

func Benchmark_Fmt(b *testing.B) {
t := time.Unix(1333333333, 0)
cet, _ := time.LoadLocation("CET")
t = t.In(cet)
for i := 0; i < b.N; i++ {
if Fmt("DATE: %Z %X %x", t) != "DATE: CEST 04:22:13 2012-04-02" {
b.Logf("%s != %s", Fmt("DATE: %Z %X %x", t), "DATE: CEST 04:22:13 2012-04-02")
b.Fail()
}
}
}

0 comments on commit a15dcd5

Please sign in to comment.