1# Copyright 2011 the V8 project authors. All rights reserved.
2# Redistribution and use in source and binary forms, with or without
3# modification, are permitted provided that the following conditions are
4# met:
5#
6#     * Redistributions of source code must retain the above copyright
7#       notice, this list of conditions and the following disclaimer.
8#     * Redistributions in binary form must reproduce the above
9#       copyright notice, this list of conditions and the following
10#       disclaimer in the documentation and/or other materials provided
11#       with the distribution.
12#     * Neither the name of Google Inc. nor the names of its
13#       contributors may be used to endorse or promote products derived
14#       from this software without specific prior written permission.
15#
16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28
29import test
30import os
31from os.path import join, split
32
33def IsNumber(string):
34  try:
35    float(string)
36    return True
37  except ValueError:
38    return False
39
40
41class BenchmarkTestCase(test.TestCase):
42
43  def __init__(self, path, context, mode):
44    super(BenchmarkTestCase, self).__init__(context, split(path), mode)
45    self.root = path
46
47  def GetLabel(self):
48    return '%s benchmark %s' % (self.mode, self.GetName())
49
50  def IsFailureOutput(self, output):
51    if output.exit_code != 0:
52      return True
53    lines = output.stdout.splitlines()
54    for line in lines:
55      colon_index = line.find(':')
56      if colon_index >= 0:
57        if not IsNumber(line[colon_index+1:].strip()):
58          return True
59    return False
60
61  def GetCommand(self):
62    result = self.context.GetVmCommand(self, self.mode)
63    result.append(join(self.root, 'run.js'))
64    return result
65
66  def GetName(self):
67    return 'V8'
68
69  def BeforeRun(self):
70    os.chdir(self.root)
71
72  def AfterRun(self, result):
73    os.chdir(self.context.buildspace)
74
75  def GetSource(self):
76    return open(join(self.root, 'run.js')).read()
77
78  def GetCustomFlags(self, mode):
79    return []
80
81
82class BenchmarkTestConfiguration(test.TestConfiguration):
83
84  def __init__(self, context, root):
85    super(BenchmarkTestConfiguration, self).__init__(context, root)
86
87  def ListTests(self, current_path, path, mode, variant_flags):
88    path = self.context.workspace
89    path = join(path, 'benchmarks')
90    test = BenchmarkTestCase(path, self.context, mode)
91    return [test]
92
93  def GetBuildRequirements(self):
94    return ['d8']
95
96  def GetTestStatus(self, sections, defs):
97    pass
98
99def GetConfiguration(context, root):
100  return BenchmarkTestConfiguration(context, root)
101