Created
August 4, 2014 08:16
-
-
Save PirosB3/a85825ec49b577824c54 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"math" | |
"flag" | |
"fmt" | |
"sort" | |
"labix.org/v2/mgo" | |
"labix.org/v2/mgo/bson" | |
) | |
const ( | |
MAP = ` | |
function () { | |
var n = this.diff.length; | |
for (var i=0; i < n; i++) { | |
emit(this.name, { | |
control: this.control[i], | |
experiment: this.experiment[i], | |
diff: this.diff[i] | |
}); | |
} | |
}` | |
REDUCE = ` | |
function (key, values) { | |
var control = Array.avg(values.map(function(e) { | |
return e.control; | |
})); | |
var experiment = Array.avg(values.map(function(e) { | |
return e.experiment; | |
})); | |
var diff = Array.avg(values.map(function(e) { | |
return e.diff; | |
})); | |
return { control: control, experiment: experiment, diff: diff }; | |
}` | |
) | |
func Tenary(condition bool, yes interface{}, no interface{}) interface{} { | |
if condition { | |
return yes | |
} else { | |
return no | |
} | |
} | |
type Result []map[string]interface{} | |
// +gen | |
type ResultItem struct { | |
Name string | |
diff float64 | |
} | |
func (r Result) Len() int { return len(r) } | |
func (r Result) Swap(i, j int) { r[i], r[j] = r[j], r[i] } | |
func (r Result) Less(i, j int) bool { | |
value_i := r[i]["value"].(map[string]interface{}) | |
value_j := r[j]["value"].(map[string]interface{}) | |
return value_i["diff"].(float64) > value_j["diff"].(float64) | |
} | |
func MakePercentage(control float64, diff float64) (float64, string) { | |
percentage := (math.Abs(diff) / control) * 100 | |
word := Tenary(diff > 0, "faster", "slower").(string) | |
return percentage, word | |
} | |
func main() { | |
dbPtr := flag.String("path", "localhost", "a string") | |
portPtr := flag.Int("port", 27017, "a number") | |
onlyQueryPtr := flag.Bool("onlyQuery", false, "a boolean") | |
flag.Parse() | |
db, port, onlyQuery := *dbPtr, *portPtr, *onlyQueryPtr | |
session, err := mgo.Dial(fmt.Sprintf("%s:%d", db, port)) | |
if err != nil { | |
panic(err) | |
} | |
collection := session.DB("django").C("benchmarks") | |
var query *bson.M | |
if onlyQuery { | |
query = &bson.M{"name": bson.RegEx{"^query_", ""}} | |
} else { | |
query = nil | |
} | |
var result Result | |
map_reduce := &mgo.MapReduce{ Map: MAP, Reduce: REDUCE } | |
_, err2 := collection.Find(query).MapReduce(map_reduce, &result) | |
if err2 != nil { | |
panic(err2) | |
} | |
sort.Sort(result) | |
fmt.Println("| Benchmark Name | Control | Experiment | Diff |") | |
fmt.Println("| -------------- | ------- | ---------- | ---- |") | |
positiveDelta := 0.0 | |
negativeDelta := 0.0 | |
positivePercentage := 0.0 | |
negativePercentage := 0.0 | |
for _, item := range result { | |
value := item["value"].(map[string]interface{}) | |
percentage, word := MakePercentage( | |
value["control"].(float64), | |
value["diff"].(float64), | |
) | |
diff := value["diff"].(float64) | |
if diff > 0 { | |
positiveDelta += math.Abs(diff) | |
positivePercentage += math.Abs(percentage) | |
} else { | |
negativeDelta += math.Abs(diff) | |
negativePercentage += math.Abs(percentage) | |
} | |
fmt.Printf("| %s | %0.10f | %0.10f | %0.10f (%0.2f%% %s) |\n", | |
item["_id"].(string), | |
value["control"].(float64), | |
value["experiment"].(float64), | |
value["diff"].(float64), | |
percentage, word) | |
} | |
fmt.Printf("Sum of all benchmarks (bigger is better): %0.10f\n", positiveDelta-negativeDelta) | |
percentageDelta := positivePercentage-negativePercentage | |
word := Tenary(percentageDelta > 0, "faster", "slower").(string) | |
fmt.Printf("Experiment is %0.2f%% %s\n", math.Abs(percentageDelta), word) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment