k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/api/extensions.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package api
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"time"
    23  )
    24  
    25  // MarshalJSON marshals Duration to string format.
    26  func (d *Duration) MarshalJSON() ([]byte, error) {
    27  	return json.Marshal(d.String())
    28  }
    29  
    30  // UnmarshalJSON unmarshals Duration from string.
    31  func (d *Duration) UnmarshalJSON(data []byte) error {
    32  	var v string
    33  	if err := json.Unmarshal(data, &v); err != nil {
    34  		return fmt.Errorf("unmarshaling error: %v", err)
    35  	}
    36  	duration, err := time.ParseDuration(v)
    37  	if err != nil {
    38  		return fmt.Errorf("parsing duration error: %v", err)
    39  	}
    40  	*d = Duration(duration)
    41  	return nil
    42  }
    43  
    44  // String converts Duration to string format.
    45  func (d *Duration) String() string {
    46  	return d.ToTimeDuration().String()
    47  }
    48  
    49  // ToTimeDuration converts Duration to time.Duration.
    50  func (d *Duration) ToTimeDuration() time.Duration {
    51  	return time.Duration(*d)
    52  }
    53  
    54  // IsMeasurement returns true whether a step is a measurement-step.
    55  func (s *Step) IsMeasurement() bool {
    56  	return len(s.Measurements) > 0
    57  }
    58  
    59  // IsPhase returns true whether a step is a phase-step.
    60  func (s *Step) IsPhase() bool {
    61  	return len(s.Phases) > 0
    62  }
    63  
    64  // IsModule returns true whether a step is a module.
    65  func (s *Step) IsModule() bool {
    66  	return s.Module.Path != ""
    67  }