本文整理匯總了Golang中github.com/dustin/go-humanize.Bytes函數的典型用法代碼示例。如果您正苦於以下問題:Golang Bytes函數的具體用法?Golang Bytes怎麽用?Golang Bytes使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Bytes函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: PrintStats
func (f *FetcherEnumerator) PrintStats() {
var (
start = time.Now()
then = time.Now()
lastStats stats
)
for _ = range time.Tick(10 * time.Second) {
f.mu.Lock()
now := time.Now()
stats := f.stats
f.mu.Unlock()
elapsed := now.Sub(start)
delta := uint64(now.Sub(then).Seconds())
bytes := uint64(stats.bytes - lastStats.bytes)
blobs := float64(stats.blobs - lastStats.blobs)
hit := float64(stats.hit - lastStats.hit)
miss := float64(stats.miss - lastStats.miss)
fmt.Printf("%s: %s blobs (%s/sec); %s bytes (%s/sec); cache %[email protected]%.0f%% (cum %.0f%%)\n",
elapsed,
humanize.SI(float64(stats.blobs), ""), humanize.SI(blobs/float64(delta), ""),
humanize.Bytes(stats.bytes), humanize.Bytes(bytes/uint64(delta)),
humanize.SI(hit+miss, ""), 100*hit/(hit+miss),
float64(100*stats.hit)/float64(stats.hit+stats.miss),
)
lastStats = stats
then = now
}
}
示例2: main
func main() {
useSHA := flag.String("use-sha", "", "<default>=no hashing, 1=sha1, 256=sha256, 512=sha512, blake=blake2b")
useBH := flag.Bool("use-bh", false, "whether we buzhash the bytes")
flag.Parse()
flag.Usage = func() {
fmt.Printf("%s <big-file>\n", os.Args[0])
flag.PrintDefaults()
return
}
if len(flag.Args()) < 1 {
flag.Usage()
return
}
p := flag.Args()[0]
bh := buzhash.NewBuzHash(64 * 8)
f, _ := os.Open(p)
defer f.Close()
t0 := time.Now()
buf := make([]byte, 4*1024)
l := uint64(0)
var h hash.Hash
if *useSHA == "1" {
h = sha1.New()
} else if *useSHA == "256" {
h = sha256.New()
} else if *useSHA == "512" {
h = sha512.New()
} else if *useSHA == "blake" {
h = blake2.NewBlake2B()
}
for {
n, err := f.Read(buf)
l += uint64(n)
if err == io.EOF {
break
}
s := buf[:n]
if h != nil {
h.Write(s)
}
if *useBH {
bh.Write(s)
}
}
t1 := time.Now()
d := t1.Sub(t0)
fmt.Printf("Read %s in %s (%s/s)\n", humanize.Bytes(l), d, humanize.Bytes(uint64(float64(l)/d.Seconds())))
digest := []byte{}
if h != nil {
fmt.Printf("%x\n", h.Sum(digest))
}
}
示例3: GetPinsStatusByFolder
func (m *Model) GetPinsStatusByFolder() map[string]string {
result := make(map[string]string)
m.fmut.RLock()
defer m.fmut.RUnlock()
for fldr, files := range m.pinnedFiles {
pendingBytes := uint64(0)
pendingFileCount := 0
pinnedBytes := uint64(0)
pinnedFileCount := 0
fbc := m.blockCaches[fldr]
tc := m.treeCaches[fldr]
for _, file := range files {
pending := false
fileEntry, _ := tc.GetEntry(file)
for _, block := range fileEntry.Blocks {
if false == fbc.HasPinnedBlock(block.Hash) {
pending = true
pendingBytes += uint64(block.Size)
} else {
pinnedBytes += uint64(block.Size)
}
}
if pending {
pendingFileCount += 1
} else {
pinnedFileCount += 1
}
}
if pendingFileCount > 0 {
pendingByteComment := human.Bytes(pendingBytes)
fileLabel := "files"
if pendingFileCount == 1 {
fileLabel = "file"
}
result[fldr] = fmt.Sprintf("%d %s (%s) pending", pendingFileCount, fileLabel, pendingByteComment)
} else {
if pinnedFileCount > 0 {
pinnedByteComment := human.Bytes(pinnedBytes)
fileLabel := "files"
if pinnedFileCount == 1 {
fileLabel = "file"
}
result[fldr] = fmt.Sprintf("%d %s (%s) pinned", pinnedFileCount, fileLabel, pinnedByteComment)
}
}
}
return result
}
示例4: getStatusPrinter
func getStatusPrinter(expected uint64) progressreader.Callback {
startTime := time.Now()
return func(seen uint64) {
percent := float64(seen) / float64(expected) * 100
elapsed := time.Now().Sub(startTime)
rate := float64(seen) / elapsed.Seconds()
status.Printf("%.2f%% of %s (%s/s)...",
percent,
humanize.Bytes(expected),
humanize.Bytes(uint64(rate)))
}
}
示例5: main
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: %s <dataset> <file>\n", os.Args[0])
flag.PrintDefaults()
}
spec.RegisterDatabaseFlags(flag.CommandLine)
flag.Parse(true)
if len(flag.Args()) != 2 {
d.CheckError(errors.New("expected dataset and file flags"))
}
var blob types.Blob
path := flag.Arg(0)
if db, val, err := spec.GetPath(path); err != nil {
d.CheckErrorNoUsage(err)
} else if val == nil {
d.CheckErrorNoUsage(fmt.Errorf("No value at %s", path))
} else if b, ok := val.(types.Blob); !ok {
d.CheckErrorNoUsage(fmt.Errorf("Value at %s is not a blob", path))
} else {
defer db.Close()
blob = b
}
filePath := flag.Arg(1)
if filePath == "" {
d.CheckErrorNoUsage(errors.New("Empty file path"))
}
// Note: overwrites any existing file.
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0644)
d.CheckErrorNoUsage(err)
defer file.Close()
expected := humanize.Bytes(blob.Len())
start := time.Now()
progReader := progressreader.New(blob.Reader(), func(seen uint64) {
elapsed := time.Since(start).Seconds()
rate := uint64(float64(seen) / elapsed)
status.Printf("%s of %s written in %ds (%s/s)...", humanize.Bytes(seen), expected, int(elapsed), humanize.Bytes(rate))
})
io.Copy(file, progReader)
status.Done()
}
示例6: printStats
func printStats() {
statTimer := time.NewTicker(30 * time.Second)
diskUsageWarning := false
for {
<-statTimer.C
var memstats runtime.MemStats
runtime.ReadMemStats(&memstats)
log.Printf("stats [started: %s]\n", humanize.RelTime(time.Time{}, stratuxClock.Time, "ago", "from now"))
log.Printf(" - Disk bytes used = %s (%.1f %%), Disk bytes free = %s (%.1f %%)\n", humanize.Bytes(usage.Used()), 100*usage.Usage(), humanize.Bytes(usage.Free()), 100*(1-usage.Usage()))
log.Printf(" - CPUTemp=%.02f deg C, MemStats.Alloc=%s, MemStats.Sys=%s, totalNetworkMessagesSent=%s\n", globalStatus.CPUTemp, humanize.Bytes(uint64(memstats.Alloc)), humanize.Bytes(uint64(memstats.Sys)), humanize.Comma(int64(totalNetworkMessagesSent)))
log.Printf(" - UAT/min %s/%s [maxSS=%.02f%%], ES/min %s/%s, Total traffic targets tracked=%s", humanize.Comma(int64(globalStatus.UAT_messages_last_minute)), humanize.Comma(int64(globalStatus.UAT_messages_max)), float64(maxSignalStrength)/10.0, humanize.Comma(int64(globalStatus.ES_messages_last_minute)), humanize.Comma(int64(globalStatus.ES_messages_max)), humanize.Comma(int64(len(seenTraffic))))
log.Printf(" - Network data messages sent: %d total, %d nonqueueable. Network data bytes sent: %d total, %d nonqueueable.\n", globalStatus.NetworkDataMessagesSent, globalStatus.NetworkDataMessagesSentNonqueueable, globalStatus.NetworkDataBytesSent, globalStatus.NetworkDataBytesSentNonqueueable)
if globalSettings.GPS_Enabled {
log.Printf(" - Last GPS fix: %s, GPS solution type: %d using %d satellites (%d/%d seen/tracked), NACp: %d, est accuracy %.02f m\n", stratuxClock.HumanizeTime(mySituation.LastFixLocalTime), mySituation.Quality, mySituation.Satellites, mySituation.SatellitesSeen, mySituation.SatellitesTracked, mySituation.NACp, mySituation.Accuracy)
log.Printf(" - GPS vertical velocity: %.02f ft/sec; GPS vertical accuracy: %v m\n", mySituation.GPSVertVel, mySituation.AccuracyVert)
}
// Check if we're using more than 95% of the free space. If so, throw a warning (only once).
if !diskUsageWarning && usage.Usage() > 95.0 {
err_p := fmt.Errorf("Disk bytes used = %s (%.1f %%), Disk bytes free = %s (%.1f %%)", humanize.Bytes(usage.Used()), 100*usage.Usage(), humanize.Bytes(usage.Free()), 100*(1-usage.Usage()))
addSystemError(err_p)
diskUsageWarning = true
}
logStatus()
}
}
示例7: runSave
func runSave(cmd *cobra.Command, args []string) (reterr error) {
if len(args) == 0 {
return errors.New("image reference missing")
}
output, err := cmd.Flags().GetString("output")
if err != nil {
return err
}
if output == "-" && terminal.IsTerminal(int(os.Stdout.Fd())) {
return errors.New("refusing to output to terminal, specify output file")
}
client, err := engineapi.NewEnvClient()
if err != nil {
return err
}
ctx, cancel := context.WithCancel(context.Background())
callOnSignal(ctx, cancel, syscall.SIGINT)
defer cancel()
graphdir, err := cmd.Flags().GetString("graph")
if err != nil {
return err
}
c, err := buildcache.New(client).Get(ctx, graphdir, args[0])
if err != nil {
return err
}
if output == "-" {
_, err := io.Copy(os.Stdout, c)
return err
}
f, err := ioutil.TempFile(filepath.Dir(output), ".buildcache-")
if err != nil {
return err
}
defer func() {
if reterr != nil {
os.RemoveAll(f.Name())
}
}()
if n, err := io.Copy(f, c); err != nil {
return err
} else {
logrus.Debugf("saving: %v", humanize.Bytes(uint64(n)))
}
if err := f.Sync(); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
return os.Rename(f.Name(), output)
}
示例8: Write
func (w *hexWriter) Write(p []byte) (n int, err error) {
for _, v := range p {
if w.count == 16 {
if !w.sizeWritten {
w.hrs.write(" // ")
w.hrs.write(humanize.Bytes(w.size))
w.sizeWritten = true
}
w.hrs.newLine()
w.count = 0
} else if w.count != 0 {
w.hrs.write(" ")
}
if v < 0x10 {
w.hrs.write("0")
}
w.hrs.write(strconv.FormatUint(uint64(v), 16))
if w.hrs.err != nil {
err = w.hrs.err
return
}
n++
w.count++
}
return
}
示例9: fmtTransfer
// pretty-print #files/#bytes transfer statistics
func fmtTransfer(numFiles, numBytes uint64, duration time.Duration) string {
var s string
if numFiles == 0 && numBytes == 0 {
return "none"
}
s = fmt.Sprintf("%d file", numFiles)
if numFiles != 1 {
s += "s"
}
s = fmt.Sprintf("%s, %s", s, humanize.Bytes(numBytes))
if duration > 0 {
s = fmt.Sprintf("%s (%s/s)", s, humanize.Bytes(numBytes/uint64(duration.Seconds())))
}
return s
}
示例10: writeEncodedValue
func writeEncodedValue(w io.Writer, v types.Value) error {
if v.Type().Kind() != types.BlobKind {
return types.WriteEncodedValue(w, v)
}
write(w, []byte("Blob ("))
write(w, []byte(humanize.Bytes(v.(types.Blob).Len())))
return write(w, []byte(")"))
}
示例11: String
func (mi *MetaInfo) String() string {
s := ""
if mi.Info != nil {
s += fmt.Sprintf("info: %x(sha1)\n", mi.infoHash)
s += fmt.Sprintf("\tpiece length: %d\n", mi.Info.PieceLength)
s += fmt.Sprintf("\tpieces: suppressed\n")
s += fmt.Sprintf("\tprivate: %d\n", mi.Info.Private)
if mi.Info.Name != "" {
s += fmt.Sprintf("\tname: %q", mi.Info.Name)
}
if mi.Info.Length > 0 {
s += fmt.Sprintf(" (%s)\n", humanize.Bytes(mi.Info.Length))
} else {
s += "\n"
}
if len(mi.Info.Files) > 0 {
s += fmt.Sprintf("\tfiles:\n")
}
for _, f := range mi.Info.Files {
s += fmt.Sprintf("\t\tpath: %q (%s)\n", filepath.Join(f.Path...), humanize.Bytes(f.Length))
}
}
if len(mi.Announce) > 0 {
s += fmt.Sprintf("announce: %q\n", mi.Announce)
}
if len(mi.AnnounceList) > 0 {
s += fmt.Sprintf("announce-list:\n")
}
for _, a := range mi.AnnounceList {
s += fmt.Sprintf("\t%q\n", strings.Join(a, ","))
}
if mi.CreationDate > 0 {
s += fmt.Sprintf("ceation date: %s\n", time.Unix(mi.CreationDate, 0))
}
if mi.Comment != "" {
s += fmt.Sprintf("comment: %q\n", mi.Comment)
}
if mi.CreatedBy != "" {
s += fmt.Sprintf("created by: %q\n", mi.CreatedBy)
}
if mi.Encoding != "" {
s += fmt.Sprintf("encoding: %s\n", mi.Encoding)
}
return s
}
示例12: main
func main() {
var policies []clcv2.SBSServerPolicy
var day = time.Now()
var date = flag.String("start", day.Format("2006-01-02"), "Day to query storage usage for")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: %s [options] <Server-Policy-ID | Server-Name>\n", path.Base(os.Args[0]))
flag.PrintDefaults()
}
flag.Parse()
if flag.NArg() != 1 {
flag.Usage()
os.Exit(0)
}
day, err := time.Parse("2006-01-02", *date)
if err != nil {
exit.Error("invalid backup query date %s (expected format: YYYY-MM-DD)", *date)
}
client, err := clcv2.NewCLIClient()
if err != nil {
exit.Fatal(err.Error())
}
// First look up the corresponding Server Policy or Policies, since the query needs the Account Policy ID.
if utils.LooksLikeServerName(flag.Arg(0)) {
policies, err = client.SBSgetServerPolicyDetails(flag.Arg(0))
if err != nil {
exit.Fatalf("failed to list SBS policies for server %s: %s", flag.Arg(0), err)
}
} else {
p, err := client.SBSgetServerPolicy(flag.Arg(0))
if err != nil {
exit.Fatalf("failed to list SBS Server Policy %s: %s", flag.Arg(0), err)
}
policies = []clcv2.SBSServerPolicy{*p}
}
table := tablewriter.NewWriter(os.Stdout)
table.SetAutoFormatHeaders(false)
table.SetAlignment(tablewriter.ALIGN_CENTRE)
table.SetAutoWrapText(false)
table.SetHeader([]string{"Server",
fmt.Sprintf("Usage on %s", day.Format("Mon, Jan 2 2006")),
"Server Policy ID", "Account Policy ID",
})
for _, p := range policies {
usage, err := client.SBSgetServerStorageUsage(p.AccountPolicyID, p.ID, day)
if err != nil {
exit.Fatalf("failed to obtain server %s storage use on %s: %s", p.ServerID, day.Format("2006-01-02"), err)
}
table.Append([]string{p.ServerID, humanize.Bytes(usage), p.ID, p.AccountPolicyID})
}
table.Render()
}
示例13: status
func (c *clusterClient) status(context *cli.Context) {
c.clusterOptions(context)
jsonOut := context.GlobalBool("json")
outFd := os.Stdout
fn := "status"
cluster, err := c.manager.Enumerate()
if err != nil {
cmdError(context, fn, err)
return
}
if jsonOut {
fmtOutput(context, &Format{Cluster: &cluster})
} else {
fmt.Fprintf(outFd, "Cluster Information:\nCluster ID %s: Status: %v\n\n",
cluster.Id, cluster.Status)
fmt.Fprintf(outFd, "Load Information:\n")
w := new(tabwriter.Writer)
w.Init(outFd, 12, 12, 1, ' ', 0)
fmt.Fprintln(w, "ID\t MGMT IP\t STATUS\t CPU\t MEM TOTAL\t MEM FREE")
for _, n := range cluster.Nodes {
status := ""
if n.Status == api.Status_STATUS_INIT {
status = "Initializing"
} else if n.Status == api.Status_STATUS_OK {
status = "OK"
} else if n.Status == api.Status_STATUS_OFFLINE {
status = "Off Line"
} else {
status = "Error"
}
fmt.Fprintln(w, n.Id, "\t", n.MgmtIp, "\t", status, "\t",
n.Cpu, "\t", humanize.Bytes(n.MemTotal), "\t",
humanize.Bytes(n.MemFree))
}
fmt.Fprintln(w)
w.Flush()
}
}
示例14: getStatusPrinter
func getStatusPrinter(expectedLen int64) progressreader.Callback {
return func(seenLen uint64) {
var expected string
if expectedLen < 0 {
expected = "(unknown)"
} else {
expected = human.Bytes(uint64(expectedLen))
}
elapsed := time.Now().Sub(start)
rate := uint64(float64(seenLen) / elapsed.Seconds())
status.Printf("%s of %s written in %ds (%s/s)...",
human.Bytes(seenLen),
expected,
uint64(elapsed.Seconds()),
human.Bytes(rate))
}
}
示例15: GetSizes
func GetSizes() string {
kernel32, err := syscall.LoadDLL("kernel32.dll")
if nil != err {
abort("loadLibrary", err)
}
defer kernel32.Release()
get, err := kernel32.FindProc("GetSystemFileCacheSize")
if nil != err {
abort("GetProcAddress", err)
}
var minFileCache uint64
var maxFileCache uint64
var lpFlags uint32
res, _, err := get.Call(uintptr(unsafe.Pointer(&minFileCache)), uintptr(unsafe.Pointer(&maxFileCache)), uintptr(unsafe.Pointer(&lpFlags)))
if res == 0 {
abort("getSystemFileCacheSize", err)
}
return fmt.Sprintf("Min: %v Max: %v Flags: %v", human.Bytes(minFileCache), human.Bytes(maxFileCache), lpFlags)
}