summaryrefslogtreecommitdiffhomepage
path: root/benchmarks/suites/startup.py
blob: a1b6c575307837283be1319cf1d9166283f0fe6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# python3
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Start-up benchmarks."""

from benchmarks import suites
from benchmarks.harness import machine
from benchmarks.suites import helpers


# pylint: disable=unused-argument
def startup_time_ms(value, **kwargs):
  """Returns average startup time per container in milliseconds.

  Args:
    value: The floating point time in seconds.
    **kwargs: Ignored.

  Returns:
    The time given in milliseconds.
  """
  return value * 1000


def startup(target: machine.Machine,
            workload: str,
            count: int = 5,
            port: int = 0,
            **kwargs):
  """Time the startup of some workload.

  Args:
    target: A machine object.
    workload: The workload to run.
    count: Number of containers to start.
    port: The port to check for liveness, if provided.
    **kwargs: Additional container options.

  Returns:
    The mean start-up time in seconds.
  """
  # Load before timing.
  image = target.pull(workload)
  netcat = target.pull("netcat")
  count = int(count)
  port = int(port)

  with helpers.Timer() as timer:
    for _ in range(count):
      if not port:
        # Run the container synchronously.
        target.container(image, **kwargs).run()
      else:
        # Run a detached container until httpd available.
        with target.container(image, port=port, **kwargs).detach() as server:
          (server_host, server_port) = server.address()
          target.container(netcat).run(host=server_host, port=server_port)
    return timer.elapsed() / float(count)


@suites.benchmark(metrics=[startup_time_ms], machines=1)
def empty(target: machine.Machine, **kwargs) -> float:
  """Time the startup of a trivial container.

  Args:
    target: A machine object.
    **kwargs: Additional startup options.

  Returns:
    The time to run the container.
  """
  return startup(target, workload="true", **kwargs)


@suites.benchmark(metrics=[startup_time_ms], machines=1)
def node(target: machine.Machine, **kwargs) -> float:
  """Time the startup of the node container.

  Args:
    target: A machine object.
    **kwargs: Additional statup options.

  Returns:
    The time to run the container.
  """
  return startup(target, workload="node", port=8080, **kwargs)


@suites.benchmark(metrics=[startup_time_ms], machines=1)
def ruby(target: machine.Machine, **kwargs) -> float:
  """Time the startup of the ruby container.

  Args:
    target: A machine object.
    **kwargs: Additional startup options.

  Returns:
    The time to run the container.
  """
  return startup(target, workload="ruby", port=3000, **kwargs)