perf.cpp
来自「MySQL数据库开发源码 值得一看哦」· C++ 代码 · 共 648 行 · 第 1/2 页
CPP
648 行
/* Copyright (C) 2003 MySQL AB This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#include <ndb_global.h>extern "C" {#include <dba.h>}#include <NdbOut.hpp>#include <NdbSleep.h>#include <NdbTimer.hpp>#include <NDBT_Stats.hpp>#include <NDBT_ReturnCodes.h>#include <NdbMain.h>#include <time.h>#undef min#undef maxstatic const int NP_Insert = 0;static const int NP_Update = 1;static const int NP_WriteUpdate = 2;static const int NP_WriteInsert = 3;static const int NP_Delete = 4;static const int NP_BulkRead = 5;static const int NP_MAX = 5;static const char * Operations[] = { "Insert ", "Update ", "WriteUpd", "WriteIns", "Delete ", "BulkRead"};/** * Configuration variables */static int NoOfTransactions = 10000;static int ParallellTransactions = 1000;static int OperationsPerTransaction = 10;static int NoOfColumns = 20;static int BytesPerInsert = 300;static int BytesPerUpdate = 200;static int LoopCount = 10;/** * Global variables */static char TableName[255];static DBA_ColumnDesc_t * ColumnDescriptions;static DBA_ColumnBinding_t * InsertBindings;static DBA_ColumnBinding_t * UpdateBindings; static int UpdateBindingColumns;static DBA_ColumnBinding_t * DeleteBindings;static char * TestData;static DBA_Binding_t * InsertB;static DBA_Binding_t * UpdateB;static DBA_Binding_t * DeleteB;/** * Function prototypes */static void sequence(int loops);inline void * getPtr(int rowNo) { return TestData+rowNo*BytesPerInsert;}inline void setPK(int rowNo, int pk){ * (int *)getPtr(rowNo) = pk; }static void SetupTestData();static void CleanupTestData();static bool CreateTable();static bool CleanTable();static bool CreateBindings();static void usage();staticvoidusage(){ int ForceSend, Interval; DBA_GetParameter(0, &Interval); DBA_GetParameter(3, &ForceSend); ndbout << "newtonPerf" << endl << " -n Transactions per loop and operation (" << NoOfTransactions << ")" << endl << " -p parallell transactions (" << ParallellTransactions << ")" << endl << " -o operations per transaction (" << OperationsPerTransaction << ")" << endl << " -a no of columns (" << NoOfColumns << ")" << endl << " -b Table size in bytes (" << BytesPerInsert << ")" << endl << " -u Bytes per update (" << BytesPerUpdate << ")" << endl << " -l Loop count (" << LoopCount << ")" << endl << " -i Interval (" << Interval << "ms)" << endl << " -f Force send algorithm (" << ForceSend << ")" << endl << " -h Help" << endl;}staticboolparseArgs(int argc, const char **argv){ bool a = false, b = false, u = false; for(int i = 1; i<argc; i++){ if(argv[i][0] != '-'){ ndbout << "Invalid argument: " << argv[i] << endl; return false; } if(argv[i][1] == 'h') return false; if(i == argc-1){ ndbout << "Expecting argument to " << argv[i] << endl; return false; } switch(argv[i][1]){ case 'n': NoOfTransactions = atoi(argv[i+1]); break; case 'p': ParallellTransactions = atoi(argv[i+1]); break; case 'o': OperationsPerTransaction = atoi(argv[i+1]); break; case 'a': NoOfColumns = atoi(argv[i+1]); a = true; break; case 'b': BytesPerInsert = atoi(argv[i+1]); b = true; break; case 'u': BytesPerUpdate = atoi(argv[i+1]); u = true; break; case 'l': LoopCount = atoi(argv[i+1]); break; case 'f': { const int val = atoi(argv[i+1]); if(DBA_SetParameter(3, val) != DBA_NO_ERROR){ ndbout << "Invalid force send algorithm: " << DBA_GetLatestErrorMsg() << "(" << DBA_GetLatestError() << ")" << endl; return false; } } break; case 'i': { const int val = atoi(argv[i+1]); if(DBA_SetParameter(0, val) != DBA_NO_ERROR){ ndbout << "Invalid NBP interval: " << DBA_GetLatestErrorMsg() << "(" << DBA_GetLatestError() << ")" << endl; return false; } } break; default: ndbout << "Invalid option: " << argv[i] << endl; return false; } i++; } if(a && !b) BytesPerInsert = 15 * NoOfColumns; if(!a && b) NoOfColumns = ((BytesPerInsert + 14) / 15)+1; if(!u) BytesPerUpdate = (2 * BytesPerInsert) / 3; bool t = true; if(NoOfColumns < 2) t = false; if(BytesPerInsert < 8) t = false; if(BytesPerUpdate < 8) t = false; if(!t){ ndbout << "Invalid arguments combination of -a -b -u not working out" << endl; return false; } return true;}NDB_COMMAND(newton_perf, "newton_perf", "newton_perf", "newton_perf", 65535){ if(!parseArgs(argc, argv)){ usage(); return NDBT_ProgramExit(NDBT_WRONGARGS); } ndbout << "-----------" << endl; usage(); ndbout << endl; SetupTestData(); DBA_Open(); if(!CreateTable()){ DBA_Close(); CleanupTestData(); return 0; } if(!CreateBindings()){ DBA_Close(); CleanupTestData(); return 0; } CleanTable(); sequence(LoopCount); DBA_Close(); CleanupTestData(); DBA_DestroyBinding(InsertB); DBA_DestroyBinding(UpdateB); DBA_DestroyBinding(DeleteB);}staticvoidErrorMsg(const char * s){ ndbout << s << ": " << DBA_GetLatestError() << "-" << DBA_GetLatestErrorMsg() << ", " << DBA_GetLatestNdbError() << endl;}staticintm4(int i){ const int j = i - (i & 3); return j;}staticvoidSetupTestData(){ ndbout << "Creating testdata" << endl; ColumnDescriptions = new DBA_ColumnDesc_t[NoOfColumns]; InsertBindings = new DBA_ColumnBinding_t[NoOfColumns]; const int sz = m4((BytesPerInsert - ((NoOfColumns+1)/2)*4)/(NoOfColumns/2)); int sum = 0; UpdateBindingColumns = 0; for(int i = 0; i<NoOfColumns; i++){ char tmp[16]; if((i % 2) == 0){ sprintf(tmp, "I%d", i); ColumnDescriptions[i].DataType = DBA_INT; ColumnDescriptions[i].Size = 4; sum += 4; } else { sprintf(tmp, "S%d", i); ColumnDescriptions[i].DataType = DBA_CHAR; ColumnDescriptions[i].Size = sz; sum += sz; } ColumnDescriptions[i].IsKey = 0; ColumnDescriptions[i].Name = strdup(tmp); InsertBindings[i].Name = strdup(tmp); InsertBindings[i].DataType = ColumnDescriptions[i].DataType; InsertBindings[i].Size = ColumnDescriptions[i].Size; InsertBindings[i].Offset = sum - ColumnDescriptions[i].Size; InsertBindings[i].Ptr = 0; if(sum <= BytesPerUpdate) UpdateBindingColumns++; } if(UpdateBindingColumns == 1) UpdateBindingColumns++; ColumnDescriptions[0].IsKey = 1; assert(sum <= BytesPerInsert); sprintf(TableName, "NEWTON_%d_%d", sum, NoOfColumns); UpdateBindings = new DBA_ColumnBinding_t[UpdateBindingColumns]; memcpy(UpdateBindings, InsertBindings, UpdateBindingColumns*sizeof(DBA_ColumnBinding_t)); DeleteBindings = new DBA_ColumnBinding_t[1]; memcpy(DeleteBindings, InsertBindings, 1*sizeof(DBA_ColumnBinding_t)); TestData = (char *)malloc(NoOfTransactions * OperationsPerTransaction * BytesPerInsert); assert(TestData != 0); for(int i = 0; i<NoOfTransactions; i++) for(int j = 0; j<OperationsPerTransaction; j++){ const int pk = i * OperationsPerTransaction + j; setPK(pk, pk);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?