~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/benchmark/benchmarkudf.cc

  • Committer: Monty Taylor
  • Date: 2010-12-24 02:13:05 UTC
  • mto: This revision was merged to the branch mainline in revision 2038.
  • Revision ID: mordred@inaugust.com-20101224021305-e3slv1cyjczqorij
Changed the bzrignore file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 * Copyright (C) 2008 MySQL AB
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; version 2 of the License.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 
18
 */
 
19
 
 
20
#include "config.h"
 
21
#include <drizzled/error.h>
 
22
#include <drizzled/session.h>
 
23
#include "drizzled/internal/m_string.h"
 
24
 
 
25
using namespace std;
 
26
using namespace drizzled;
 
27
 
 
28
class BenchmarkFunction :public Item_int_func
 
29
{
 
30
public:
 
31
  BenchmarkFunction() :Item_int_func() {}
 
32
  int64_t val_int();
 
33
  virtual void print(String *str, enum_query_type query_type);
 
34
 
 
35
  const char *func_name() const
 
36
  { 
 
37
    return "benchmark"; 
 
38
  }
 
39
 
 
40
  void fix_length_and_dec()
 
41
  { 
 
42
    max_length= 1; 
 
43
    maybe_null= false;
 
44
  }
 
45
 
 
46
  bool check_argument_count(int n)
 
47
  { 
 
48
    return (n == 2); 
 
49
  }
 
50
};
 
51
 
 
52
 
 
53
/* This function is just used to test speed of different functions */
 
54
int64_t BenchmarkFunction::val_int()
 
55
{
 
56
  assert(fixed == true);
 
57
 
 
58
  char buff[MAX_FIELD_WIDTH];
 
59
  String tmp(buff,sizeof(buff), &my_charset_bin);
 
60
  my_decimal tmp_decimal;
 
61
  Session *session= current_session;
 
62
  uint64_t loop_count;
 
63
 
 
64
  loop_count= (uint64_t) args[0]->val_int();
 
65
 
 
66
  if (args[0]->null_value ||
 
67
      (args[0]->unsigned_flag == false && (((int64_t) loop_count) < 0)))
 
68
  {
 
69
    if (args[0]->null_value == false)
 
70
    {
 
71
      internal::int64_t10_to_str((int64_t)loop_count, buff, -10);
 
72
      push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
73
                          ER_WRONG_VALUE_FOR_TYPE, ER(ER_WRONG_VALUE_FOR_TYPE),
 
74
                          "count", buff, "benchmark");
 
75
    }
 
76
 
 
77
    null_value= true;
 
78
    return 0;
 
79
  }
 
80
 
 
81
  null_value= false;
 
82
 
 
83
  uint64_t loop;
 
84
  for (loop= 0 ; loop < loop_count && not session->getKilled(); loop++)
 
85
  {
 
86
    switch (args[1]->result_type()) 
 
87
    {
 
88
    case REAL_RESULT:
 
89
      (void) args[1]->val_real();
 
90
      break;
 
91
    case INT_RESULT:
 
92
      (void) args[1]->val_int();
 
93
      break;
 
94
    case STRING_RESULT:
 
95
      (void) args[1]->val_str(&tmp);
 
96
      break;
 
97
    case DECIMAL_RESULT:
 
98
      (void) args[1]->val_decimal(&tmp_decimal);
 
99
      break;
 
100
    case ROW_RESULT:
 
101
    default:
 
102
      // This case should never be chosen
 
103
      assert(0);
 
104
      return 0;
 
105
    }
 
106
  }
 
107
  return 0;
 
108
}
 
109
 
 
110
void BenchmarkFunction::print(String *str, enum_query_type query_type)
 
111
{
 
112
  str->append(STRING_WITH_LEN("benchmark("));
 
113
  args[0]->print(str, query_type);
 
114
  str->append(',');
 
115
  args[1]->print(str, query_type);
 
116
  str->append(')');
 
117
}
 
118
 
 
119
plugin::Create_function<BenchmarkFunction> *benchmarkudf= NULL;
 
120
 
 
121
static int initialize(module::Context &context)
 
122
{
 
123
  benchmarkudf= new plugin::Create_function<BenchmarkFunction>("benchmark");
 
124
  context.add(benchmarkudf);
 
125
  return 0;
 
126
}
 
127
 
 
128
DRIZZLE_DECLARE_PLUGIN
 
129
{
 
130
  DRIZZLE_VERSION_ID,
 
131
  "benchmark",
 
132
  "1.0",
 
133
  "Devananda van der Veen",
 
134
  "Measure time for repeated calls to a function.",
 
135
  PLUGIN_LICENSE_GPL,
 
136
  initialize, /* Plugin Init */
 
137
  NULL,   /* system variables */
 
138
  NULL    /* config options */
 
139
}
 
140
DRIZZLE_DECLARE_PLUGIN_END;