本文整理汇总了Golang中github.com/cockroachdb/cockroach/util/timeutil.Now函数的典型用法代码示例。如果您正苦于以下问题:Golang Now函数的具体用法?Golang Now怎么用?Golang Now使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Now函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: runMVCCConditionalPut
func runMVCCConditionalPut(valueSize int, createFirst bool, b *testing.B) {
rng, _ := randutil.NewPseudoRand()
value := roachpb.MakeValueFromBytes(randutil.RandBytes(rng, valueSize))
keyBuf := append(make([]byte, 0, 64), []byte("key-")...)
stopper := stop.NewStopper()
defer stopper.Stop()
rocksdb := NewInMem(roachpb.Attributes{}, testCacheSize, stopper)
b.SetBytes(int64(valueSize))
var expected *roachpb.Value
if createFirst {
for i := 0; i < b.N; i++ {
key := roachpb.Key(encoding.EncodeUvarintAscending(keyBuf[:4], uint64(i)))
ts := makeTS(timeutil.Now().UnixNano(), 0)
if err := MVCCPut(rocksdb, nil, key, ts, value, nil); err != nil {
b.Fatalf("failed put: %s", err)
}
}
expected = &value
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
key := roachpb.Key(encoding.EncodeUvarintAscending(keyBuf[:4], uint64(i)))
ts := makeTS(timeutil.Now().UnixNano(), 0)
if err := MVCCConditionalPut(rocksdb, nil, key, ts, value, expected, nil); err != nil {
b.Fatalf("failed put: %s", err)
}
}
b.StopTimer()
}
示例2: monitor
func (z *zeroSum) monitor(d time.Duration) {
start := timeutil.Now()
lastTime := start
var lastOps uint64
for ticks := 0; true; ticks++ {
time.Sleep(d)
if ticks%20 == 0 {
fmt.Printf("_elapsed__accounts_________ops__ops/sec___errors___splits____xfers___ranges_____________replicas\n")
}
now := timeutil.Now()
elapsed := now.Sub(lastTime).Seconds()
ops := atomic.LoadUint64(&z.stats.ops)
z.ranges.Lock()
ranges, replicas := z.ranges.count, z.ranges.replicas
z.ranges.Unlock()
fmt.Printf("%8s %9d %11d %8.1f %8d %8d %8d %8d %20s\n",
time.Duration(now.Sub(start).Seconds()+0.5)*time.Second,
z.accountsLen(), ops, float64(ops-lastOps)/elapsed,
atomic.LoadUint64(&z.stats.errors),
atomic.LoadUint64(&z.stats.splits),
atomic.LoadUint64(&z.stats.transfers),
ranges, z.formatReplicas(replicas))
lastTime = now
lastOps = ops
}
}
示例3: testGossipPeeringsInner
func testGossipPeeringsInner(t *testing.T, c cluster.Cluster, cfg cluster.TestConfig) {
num := c.NumNodes()
deadline := timeutil.Now().Add(cfg.Duration)
waitTime := longWaitTime
if cfg.Duration < waitTime {
waitTime = shortWaitTime
}
for timeutil.Now().Before(deadline) {
checkGossip(t, c, waitTime, hasPeers(num))
// Restart the first node.
log.Infof(context.Background(), "restarting node 0")
if err := c.Restart(0); err != nil {
t.Fatal(err)
}
checkGossip(t, c, waitTime, hasPeers(num))
// Restart another node (if there is one).
var pickedNode int
if num > 1 {
pickedNode = rand.Intn(num-1) + 1
}
log.Infof(context.Background(), "restarting node %d", pickedNode)
if err := c.Restart(pickedNode); err != nil {
t.Fatal(err)
}
checkGossip(t, c, waitTime, hasPeers(num))
}
}
示例4: runMVCCConditionalPut
func runMVCCConditionalPut(emk engineMaker, valueSize int, createFirst bool, b *testing.B) {
rng, _ := randutil.NewPseudoRand()
value := roachpb.MakeValueFromBytes(randutil.RandBytes(rng, valueSize))
keyBuf := append(make([]byte, 0, 64), []byte("key-")...)
eng, stopper := emk(b, fmt.Sprintf("cput_%d", valueSize))
defer stopper.Stop()
b.SetBytes(int64(valueSize))
var expected *roachpb.Value
if createFirst {
for i := 0; i < b.N; i++ {
key := roachpb.Key(encoding.EncodeUvarintAscending(keyBuf[:4], uint64(i)))
ts := makeTS(timeutil.Now().UnixNano(), 0)
if err := MVCCPut(context.Background(), eng, nil, key, ts, value, nil); err != nil {
b.Fatalf("failed put: %s", err)
}
}
expected = &value
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
key := roachpb.Key(encoding.EncodeUvarintAscending(keyBuf[:4], uint64(i)))
ts := makeTS(timeutil.Now().UnixNano(), 0)
if err := MVCCConditionalPut(context.Background(), eng, nil, key, ts, value, expected, nil); err != nil {
b.Fatalf("failed put: %s", err)
}
}
b.StopTimer()
}
示例5: AfterTest
// AfterTest snapshots the currently-running goroutines and returns a
// function to be run at the end of tests to see whether any
// goroutines leaked.
func AfterTest(t testing.TB) func() {
orig := map[string]bool{}
for _, g := range interestingGoroutines() {
orig[g] = true
}
return func() {
// Loop, waiting for goroutines to shut down.
// Wait up to 5 seconds, but finish as quickly as possible.
deadline := timeutil.Now().Add(5 * time.Second)
for {
var leaked []string
for _, g := range interestingGoroutines() {
if !orig[g] {
leaked = append(leaked, g)
}
}
if len(leaked) == 0 {
return
}
if timeutil.Now().Before(deadline) {
time.Sleep(50 * time.Millisecond)
continue
}
for _, g := range leaked {
t.Errorf("Leaked goroutine: %v", g)
}
return
}
}
}
示例6: TestPeekType
func TestPeekType(t *testing.T) {
encodedDurationAscending, _ := EncodeDurationAscending(nil, duration.Duration{})
encodedDurationDescending, _ := EncodeDurationDescending(nil, duration.Duration{})
testCases := []struct {
enc []byte
typ Type
}{
{EncodeNullAscending(nil), Null},
{EncodeNotNullAscending(nil), NotNull},
{EncodeNullDescending(nil), Null},
{EncodeNotNullDescending(nil), NotNull},
{EncodeVarintAscending(nil, 0), Int},
{EncodeVarintDescending(nil, 0), Int},
{EncodeUvarintAscending(nil, 0), Int},
{EncodeUvarintDescending(nil, 0), Int},
{EncodeFloatAscending(nil, 0), Float},
{EncodeFloatDescending(nil, 0), Float},
{EncodeDecimalAscending(nil, inf.NewDec(0, 0)), Decimal},
{EncodeDecimalDescending(nil, inf.NewDec(0, 0)), Decimal},
{EncodeBytesAscending(nil, []byte("")), Bytes},
{EncodeBytesDescending(nil, []byte("")), BytesDesc},
{EncodeTimeAscending(nil, timeutil.Now()), Time},
{EncodeTimeDescending(nil, timeutil.Now()), TimeDesc},
{encodedDurationAscending, Duration},
{encodedDurationDescending, Duration},
}
for i, c := range testCases {
typ := PeekType(c.enc)
if c.typ != typ {
t.Fatalf("%d: expected %d, but found %d", i, c.typ, typ)
}
}
}
示例7: TestAdminAPIUIData
func TestAdminAPIUIData(t *testing.T) {
defer leaktest.AfterTest(t)()
s := StartTestServer(t)
defer s.Stop()
start := timeutil.Now()
mustSetUIData := func(key string, val []byte) {
var resp struct{}
b64Val := base64.StdEncoding.EncodeToString(val)
reqBody := fmt.Sprintf(`{"key": "%s", "value": "%s"}`, key, b64Val)
if err := apiPost(s, "uidata", reqBody, &resp); err != nil {
t.Fatal(err)
}
}
expectValueEquals := func(key string, expVal []byte) {
var resp GetUIDataResponse
url := fmt.Sprintf("uidata?key=%s", key)
if err := apiGet(s, url, &resp); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(resp.Value, expVal) {
t.Fatalf("value for key %s = %v != expected %v", key, resp.Value, expVal)
}
// Sanity check LastUpdated.
now := timeutil.Now()
lastUpdated := time.Unix(resp.LastUpdated.Sec, int64(resp.LastUpdated.Nsec))
if lastUpdated.Before(start) {
t.Fatalf("lastUpdated %s < start %s", lastUpdated, start)
}
if lastUpdated.After(now) {
t.Fatalf("lastUpdated %s > now %s", lastUpdated, now)
}
}
expectKeyNotFound := func(key string) {
var resp GetUIDataResponse
url := fmt.Sprintf("uidata?key=%s", key)
expErr := "key " + key + " not found"
if err := apiGet(s, url, &resp); !testutils.IsError(err, expErr) {
t.Fatalf("unexpected error: %s", err)
}
}
// Basic tests.
mustSetUIData("k1", []byte("v1"))
expectValueEquals("k1", []byte("v1"))
expectKeyNotFound("NON_EXISTENT_KEY")
// Write a binary blob with all possible byte values, then verify it.
var buf bytes.Buffer
for i := 0; i < 997; i++ {
buf.WriteByte(byte(i % 256))
}
mustSetUIData("bin", buf.Bytes())
expectValueEquals("bin", buf.Bytes())
}
示例8: Execute
// Execute the statement(s) in the given request and returns a response.
// On error, the returned integer is an HTTP error code.
func (e *Executor) Execute(args Request) (Response, int, error) {
defer func(start time.Time) {
e.latency.RecordValue(timeutil.Now().Sub(start).Nanoseconds())
}(timeutil.Now())
results := e.ExecuteStatements(
args.User, args.Session, args.SQL, args.Params)
return Response{Results: results, Session: args.Session}, 0, nil
}
示例9: insertLoad
// insertLoad add a very basic load that inserts into a unique table and checks
// that the inserted values are indeed correct.
func insertLoad(t *testing.T, dc *dynamicClient, ID int) {
// Initialize the db.
if _, err := dc.exec(`CREATE DATABASE IF NOT EXISTS Insert`); err != nil {
t.Fatal(err)
}
tableName := fmt.Sprintf("Insert.Table%d", ID)
createTableStatement := fmt.Sprintf(`
CREATE TABLE %s (
key INT PRIMARY KEY,
value INT NOT NULL
)`, tableName)
insertStatement := fmt.Sprintf(`INSERT INTO %s (key, value) VALUES ($1, $1)`, tableName)
selectStatement := fmt.Sprintf(`SELECT key-value AS "total" FROM %s WHERE key = $1`, tableName)
// Init the db for the basic insert.
if _, err := dc.exec(createTableStatement); err != nil {
t.Fatal(err)
}
var valueCheck, valueInsert int
nextUpdate := timeutil.Now()
// Perform inserts and selects
for dc.isRunning() {
// Insert some values.
valueInsert++
if _, err := dc.exec(insertStatement, valueInsert); err != nil {
if err == errTestFinished {
return
}
t.Fatal(err)
}
// Check that another value is still correct.
valueCheck--
if valueCheck < 1 {
valueCheck = valueInsert
}
var total int
if err := dc.queryRowScan(selectStatement, []interface{}{valueCheck}, []interface{}{&total}); err != nil {
if err == errTestFinished {
return
}
t.Fatal(err)
}
if total != 0 {
t.Fatalf("total expected to be 0, is %d", total)
}
if timeutil.Now().After(nextUpdate) {
log.Infof("Insert %d: inserted and checked %d values", ID, valueInsert)
nextUpdate = timeutil.Now().Add(time.Second)
}
}
}
示例10: Logs
// Logs returns the log entries parsed from the log files stored on
// the server. Log entries are returned in reverse chronological order. The
// following options are available:
// * "starttime" query parameter filters the log entries to only ones that
// occurred on or after the "starttime". Defaults to a day ago.
// * "endtime" query parameter filters the log entries to only ones that
// occurred before on on the "endtime". Defaults to the current time.
// * "pattern" query parameter filters the log entries by the provided regexp
// pattern if it exists. Defaults to nil.
// * "max" query parameter is the hard limit of the number of returned log
// entries. Defaults to defaultMaxLogEntries.
// * "level" query parameter filters the log entries to be those of the
// corresponding severity level or worse. Defaults to "info".
func (s *statusServer) Logs(ctx context.Context, req *serverpb.LogsRequest) (*serverpb.JSONResponse, error) {
log.Flush()
var sev log.Severity
if len(req.Level) == 0 {
sev = log.InfoLog
} else {
var sevFound bool
sev, sevFound = log.SeverityByName(req.Level)
if !sevFound {
return nil, fmt.Errorf("level could not be determined: %s", req.Level)
}
}
startTimestamp, err := parseInt64WithDefault(
req.StartTime,
timeutil.Now().AddDate(0, 0, -1).UnixNano())
if err != nil {
return nil, grpc.Errorf(codes.InvalidArgument, "StartTime could not be parsed: %s", err)
}
endTimestamp, err := parseInt64WithDefault(req.EndTime, timeutil.Now().UnixNano())
if err != nil {
return nil, grpc.Errorf(codes.InvalidArgument, "EndTime could not be parsed: %s", err)
}
if startTimestamp > endTimestamp {
return nil, grpc.Errorf(codes.InvalidArgument, "StartTime: %d should not be greater than endtime: %d", startTimestamp, endTimestamp)
}
maxEntries, err := parseInt64WithDefault(req.Max, defaultMaxLogEntries)
if err != nil {
return nil, grpc.Errorf(codes.InvalidArgument, "Max could not be parsed: %s", err)
}
if maxEntries < 1 {
return nil, grpc.Errorf(codes.InvalidArgument, "Max: %d should be set to a value greater than 0", maxEntries)
}
var regex *regexp.Regexp
if len(req.Pattern) > 0 {
if regex, err = regexp.Compile(req.Pattern); err != nil {
return nil, grpc.Errorf(codes.InvalidArgument, "regex pattern could not be compiled: %s", err)
}
}
entries, err := log.FetchEntriesFromFiles(sev, startTimestamp, endTimestamp, int(maxEntries), regex)
if err != nil {
return nil, err
}
return marshalJSONResponse(entries)
}
示例11: waitClientsStop
// Wait until all clients have stopped.
func waitClientsStop(num int, state *testState, stallDuration time.Duration) {
prevRound := atomic.LoadUint64(&state.monkeyIteration)
stallTime := timeutil.Now().Add(stallDuration)
var prevOutput string
// Spin until all clients are shut.
for numShutClients := 0; numShutClients < num; {
select {
case <-state.teardown:
case <-stopper:
state.t.Fatal("interrupted")
case err := <-state.errChan:
if err != nil {
state.t.Error(err)
}
numShutClients++
case <-time.After(time.Second):
var newOutput string
if timeutil.Now().Before(state.deadline) {
curRound := atomic.LoadUint64(&state.monkeyIteration)
if curRound == prevRound {
if timeutil.Now().After(stallTime) {
atomic.StoreInt32(&state.stalled, 1)
state.t.Fatalf("Stall detected at round %d, no forward progress for %s", curRound, stallDuration)
}
} else {
prevRound = curRound
stallTime = timeutil.Now().Add(stallDuration)
}
// Periodically print out progress so that we know the test is
// still running and making progress.
counts := state.counts()
strCounts := make([]string, len(counts))
for i := range counts {
strCounts[i] = strconv.FormatUint(counts[i], 10)
}
newOutput = fmt.Sprintf("round %d: client counts: (%s)", curRound, strings.Join(strCounts, ", "))
} else {
newOutput = fmt.Sprintf("test finished, waiting for shutdown of %d clients", num-numShutClients)
}
// This just stops the logs from being a bit too spammy.
if newOutput != prevOutput {
log.Infof(newOutput)
prevOutput = newOutput
}
}
}
}
示例12: RetryForDuration
// RetryForDuration will retry the given function until it either returns
// without error, or the given duration has elapsed. The function is invoked
// immediately at first and then successively with an exponential backoff
// starting at 1ns and ending at the specified duration.
func RetryForDuration(duration time.Duration, fn func() error) error {
deadline := timeutil.Now().Add(duration)
var lastErr error
for wait := time.Duration(1); timeutil.Now().Before(deadline); wait *= 2 {
lastErr = fn()
if lastErr == nil {
return nil
}
if wait > time.Second {
wait = time.Second
}
time.Sleep(wait)
}
return lastErr
}
示例13: testPutInner
func testPutInner(t *testing.T, c cluster.Cluster, cfg cluster.TestConfig) {
db, dbStopper := c.NewClient(t, 0)
defer dbStopper.Stop()
errs := make(chan error, c.NumNodes())
start := timeutil.Now()
deadline := start.Add(cfg.Duration)
var count int64
for i := 0; i < c.NumNodes(); i++ {
go func() {
r, _ := randutil.NewPseudoRand()
value := randutil.RandBytes(r, 8192)
for timeutil.Now().Before(deadline) {
k := atomic.AddInt64(&count, 1)
v := value[:r.Intn(len(value))]
if err := db.Put(fmt.Sprintf("%08d", k), v); err != nil {
errs <- err
return
}
}
errs <- nil
}()
}
for i := 0; i < c.NumNodes(); {
baseCount := atomic.LoadInt64(&count)
select {
case <-stopper:
t.Fatalf("interrupted")
case err := <-errs:
if err != nil {
t.Fatal(err)
}
i++
case <-time.After(1 * time.Second):
// Periodically print out progress so that we know the test is still
// running.
loadedCount := atomic.LoadInt64(&count)
log.Infof(context.Background(), "%d (%d/s)", loadedCount, loadedCount-baseCount)
c.Assert(t)
cluster.Consistent(t, c)
}
}
elapsed := timeutil.Since(start)
log.Infof(context.Background(), "%d %.1f/sec", count, float64(count)/elapsed.Seconds())
}
示例14: stop
// stop stops the cluster.
func (l *LocalCluster) stop() {
if *waitOnStop {
log.Infof("waiting for interrupt")
select {
case <-l.stopper:
}
}
log.Infof("stopping")
l.mu.Lock()
defer l.mu.Unlock()
if l.monitorCtxCancelFunc != nil {
l.monitorCtxCancelFunc()
l.monitorCtxCancelFunc = nil
}
if l.vols != nil {
maybePanic(l.vols.Kill())
maybePanic(l.vols.Remove())
l.vols = nil
}
if l.CertsDir != "" {
_ = os.RemoveAll(l.CertsDir)
l.CertsDir = ""
}
outputLogDir := l.logDir
for i, n := range l.Nodes {
ci, err := n.Inspect()
crashed := err != nil || (!ci.State.Running && ci.State.ExitCode != 0)
maybePanic(n.Kill())
if crashed && outputLogDir == "" {
outputLogDir = util.CreateTempDir(util.PanicTester, "crashed_nodes")
}
if crashed || l.logDir != "" {
// TODO(bdarnell): make these filenames more consistent with
// structured logs?
file := filepath.Join(outputLogDir, nodeStr(i),
fmt.Sprintf("stderr.%s.log", strings.Replace(
timeutil.Now().Format(time.RFC3339), ":", "_", -1)))
maybePanic(os.MkdirAll(filepath.Dir(file), 0777))
w, err := os.Create(file)
maybePanic(err)
defer w.Close()
maybePanic(n.Logs(w))
if crashed {
log.Infof("node %d: stderr at %s", i, file)
}
}
maybePanic(n.Remove())
}
l.Nodes = nil
if l.networkID != "" {
maybePanic(l.client.NetworkRemove(context.Background(), l.networkID))
l.networkID = ""
}
}
示例15: BenchmarkMVCCPutDelete_RocksDB
func BenchmarkMVCCPutDelete_RocksDB(b *testing.B) {
const cacheSize = 1 << 30 // 1 GB
rocksdb, stopper := setupMVCCInMemRocksDB(b, "put_delete")
defer stopper.Stop()
r := rand.New(rand.NewSource(int64(timeutil.Now().UnixNano())))
value := roachpb.MakeValueFromBytes(randutil.RandBytes(r, 10))
zeroTS := roachpb.ZeroTimestamp
var blockNum int64
for i := 0; i < b.N; i++ {
blockID := r.Int63()
blockNum++
key := encoding.EncodeVarintAscending(nil, blockID)
key = encoding.EncodeVarintAscending(key, blockNum)
if err := MVCCPut(context.Background(), rocksdb, nil, key, zeroTS, value, nil /* txn */); err != nil {
b.Fatal(err)
}
if err := MVCCDelete(context.Background(), rocksdb, nil, key, zeroTS, nil /* txn */); err != nil {
b.Fatal(err)
}
}
}