-
Notifications
You must be signed in to change notification settings - Fork 456
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mkbench: minor improvements / refactoring / cleanup
Ahead of a much larger change that is intended to add a parser for write-throughput benchmark data, split out some smaller tweaks into a separate patch to reduce noise in the follow up patch. Use cobra for flags - this allows for using `mkbench` in a subcommand-style when there are multiple benchmark parsers (soon to be two). In the absence of a subcommand, run the YCSB benchmark parser. As a sanity check, running the following command (with no args / flags) does not result in a diff to the file: ```bash pushd ./internal/mkbench/testdata # Without flags. go run ../ # The same command with flags. go run ../ --dir --in data.js --out data.js popd ``` Split out test helpers and utility methods into separate files. Improve the diff output in the `mkbench` tests, making use of the `difflib` library that is used elsewhere. Prefix various functions with `ycsb` to avoid name collisions with similar functions that will be added in a subsequent patch. Filter YCSB benchmark data in the filesystem walker - this avoids having to read and iterate through non-YCSB data. Add copyright notices to files in which they were previously absent. Append newlines to generated files - a trivial cosmetic change.
- Loading branch information
Showing
6 changed files
with
234 additions
and
143 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
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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright 2021 The LevelDB-Go and Pebble Authors. All rights reserved. Use | ||
// of this source code is governed by a BSD-style license that can be found in | ||
// the LICENSE file. | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/cockroachdb/errors" | ||
"github.com/cockroachdb/errors/oserror" | ||
"github.com/pmezard/go-difflib/difflib" | ||
) | ||
|
||
// filesEqual returns the diff between contents of a and b. | ||
func filesEqual(a, b string) error { | ||
aBytes, err := ioutil.ReadFile(a) | ||
if err != nil { | ||
return err | ||
} | ||
bBytes, err := ioutil.ReadFile(b) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Normalize newlines. | ||
aBytes = bytes.Replace(aBytes, []byte{13, 10} /* \r\n */, []byte{10} /* \n */, -1) | ||
bBytes = bytes.Replace(bBytes, []byte{13, 10}, []byte{10}, -1) | ||
|
||
d, _ := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{ | ||
A: difflib.SplitLines(string(aBytes)), | ||
B: difflib.SplitLines(string(bBytes)), | ||
}) | ||
if d != "" { | ||
return errors.Errorf("a != b\ndiff = %s", d) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// copyDir recursively copies the fromPath to toPath, excluding certain paths. | ||
func copyDir(fromPath, toPath string) error { | ||
return filepath.Walk(fromPath, func(path string, info os.FileInfo, e error) error { | ||
if e != nil { | ||
return e | ||
} | ||
|
||
rel, err := filepath.Rel(fromPath, path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Preserve the directory structure. | ||
if info.IsDir() { | ||
err := os.Mkdir(filepath.Join(toPath, rel), 0700) | ||
if err != nil && !oserror.IsNotExist(err) { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// Copy files. | ||
fIn, err := os.Open(path) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { _ = fIn.Close() }() | ||
|
||
fOut, err := os.OpenFile(filepath.Join(toPath, rel), os.O_CREATE|os.O_WRONLY, 0700) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { _ = fOut.Close() }() | ||
|
||
_, err = io.Copy(fOut, fIn) | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright 2021 The LevelDB-Go and Pebble Authors. All rights reserved. Use | ||
// of this source code is governed by a BSD-style license that can be found in | ||
// the LICENSE file. | ||
|
||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
) | ||
|
||
func prettyJSON(v interface{}) []byte { | ||
data, err := json.MarshalIndent(v, "", "\t") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
return data | ||
} |
Oops, something went wrong.