forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge PR cosmos#4784: JSON representation of event stats
- Loading branch information
1 parent
0ba74bb
commit 24b9e84
Showing
13 changed files
with
164 additions
and
149 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ tags: | |
- rest | ||
- cli | ||
- modules | ||
- simulation |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#4670 Update simulation statistics to JSON format | ||
- Support exporting the simulation stats to a given JSON file |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,55 @@ | ||
package simulation | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"sort" | ||
"io/ioutil" | ||
) | ||
|
||
type eventStats map[string]uint | ||
// EventStats defines an object that keeps a tally of each event that has occurred | ||
// during a simulation. | ||
type EventStats map[string]map[string]map[string]int | ||
|
||
func newEventStats() eventStats { | ||
events := make(map[string]uint) | ||
return events | ||
// NewEventStats creates a new empty EventStats object | ||
func NewEventStats() EventStats { | ||
return make(EventStats) | ||
} | ||
|
||
func (es eventStats) tally(eventDesc string) { | ||
es[eventDesc]++ | ||
// Tally increases the count of a simulation event. | ||
func (es EventStats) Tally(route, op, evResult string) { | ||
_, ok := es[route] | ||
if !ok { | ||
es[route] = make(map[string]map[string]int) | ||
} | ||
|
||
_, ok = es[route][op] | ||
if !ok { | ||
es[route][op] = make(map[string]int) | ||
} | ||
|
||
es[route][op][evResult]++ | ||
} | ||
|
||
// Pretty-print events as a table | ||
func (es eventStats) Print(w io.Writer) { | ||
var keys []string | ||
for key := range es { | ||
keys = append(keys, key) | ||
// Print the event stats in JSON format. | ||
func (es EventStats) Print(w io.Writer) { | ||
obj, err := json.MarshalIndent(es, "", " ") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
sort.Strings(keys) | ||
fmt.Fprintf(w, "Event statistics: \n") | ||
fmt.Fprintf(w, string(obj)) | ||
} | ||
|
||
// ExportJSON saves the event stats as a JSON file on a given path | ||
func (es EventStats) ExportJSON(path string) { | ||
bz, err := json.MarshalIndent(es, "", " ") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for _, key := range keys { | ||
fmt.Fprintf(w, " % 60s => %d\n", key, es[key]) | ||
err = ioutil.WriteFile(path, bz, 0644) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains 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
Oops, something went wrong.