00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <config.h>
00021 #include <drizzled/error.h>
00022 #include <drizzled/internal/m_string.h>
00023 #include <drizzled/plugin/function.h>
00024 #include <drizzled/session.h>
00025
00026 using namespace std;
00027 using namespace drizzled;
00028
00029 class BenchmarkFunction :public Item_int_func
00030 {
00031 public:
00032 BenchmarkFunction() :Item_int_func() {}
00033 int64_t val_int();
00034 virtual void print(String *str);
00035
00036 const char *func_name() const
00037 {
00038 return "benchmark";
00039 }
00040
00041 void fix_length_and_dec()
00042 {
00043 max_length= 1;
00044 maybe_null= false;
00045 }
00046
00047 bool check_argument_count(int n)
00048 {
00049 return (n == 2);
00050 }
00051 };
00052
00053
00054
00055 int64_t BenchmarkFunction::val_int()
00056 {
00057 assert(fixed == true);
00058
00059 char buff[MAX_FIELD_WIDTH];
00060 String tmp(buff,sizeof(buff), &my_charset_bin);
00061 type::Decimal tmp_decimal;
00062 uint64_t loop_count;
00063
00064 loop_count= (uint64_t) args[0]->val_int();
00065
00066 if (args[0]->null_value ||
00067 (args[0]->unsigned_flag == false && (((int64_t) loop_count) < 0)))
00068 {
00069 if (args[0]->null_value == false)
00070 {
00071 internal::int64_t10_to_str((int64_t)loop_count, buff, -10);
00072 push_warning_printf(&getSession(), DRIZZLE_ERROR::WARN_LEVEL_ERROR,
00073 ER_WRONG_VALUE_FOR_TYPE, ER(ER_WRONG_VALUE_FOR_TYPE),
00074 "count", buff, "benchmark");
00075 }
00076
00077 null_value= true;
00078 return 0;
00079 }
00080
00081 null_value= false;
00082
00083 uint64_t loop;
00084 for (loop= 0 ; loop < loop_count && not getSession().getKilled(); loop++)
00085 {
00086 switch (args[1]->result_type())
00087 {
00088 case REAL_RESULT:
00089 (void) args[1]->val_real();
00090 break;
00091 case INT_RESULT:
00092 (void) args[1]->val_int();
00093 break;
00094 case STRING_RESULT:
00095 (void) args[1]->val_str(&tmp);
00096 break;
00097 case DECIMAL_RESULT:
00098 (void) args[1]->val_decimal(&tmp_decimal);
00099 break;
00100 case ROW_RESULT:
00101 default:
00102
00103 assert(0);
00104 return 0;
00105 }
00106 }
00107 return 0;
00108 }
00109
00110 void BenchmarkFunction::print(String *str)
00111 {
00112 str->append(STRING_WITH_LEN("benchmark("));
00113 args[0]->print(str);
00114 str->append(',');
00115 args[1]->print(str);
00116 str->append(')');
00117 }
00118
00119 plugin::Create_function<BenchmarkFunction> *benchmarkudf= NULL;
00120
00121 static int initialize(module::Context &context)
00122 {
00123 benchmarkudf= new plugin::Create_function<BenchmarkFunction>("benchmark");
00124 context.add(benchmarkudf);
00125 return 0;
00126 }
00127
00128 DRIZZLE_DECLARE_PLUGIN
00129 {
00130 DRIZZLE_VERSION_ID,
00131 "benchmark",
00132 "1.0",
00133 "Devananda van der Veen",
00134 "Measure time for repeated calls to a function.",
00135 PLUGIN_LICENSE_GPL,
00136 initialize,
00137 NULL,
00138 NULL
00139 }
00140 DRIZZLE_DECLARE_PLUGIN_END;