// -------------------RUNTIME METHODS ------------------------// public override void AcquireConnections(object transaction) { base.AcquireConnections(transaction); Debug.WriteLine(ComponentMetaData.CustomPropertyCollection[0]); var hostname = ComponentMetaData.CustomPropertyCollection["ServerAddress"].Value; connection = VoltConnection.Create("hosts=" + hostname).Open(); }
static void Main(string[] args) { try { // Read hosts from the command or use defaults string hosts = "10.10.180.176"; if (args.Length > 0) { hosts = string.Join(",", string.Join(",", args).Split(' ', ',')); } // Create a connection and open it immediately. // Notice the "using" block that will ensure the connection is closed and disposed of at the end. using (var connection = VoltConnection.Create("hosts=" + hosts).Open()) { // Define the procedure wrappers we will use var Insert = connection.Procedures.Wrap <Null, string, string, string>("Insert"); var Select = connection.Procedures.Wrap <SingleRowTable, string>("Select"); // Load the database - we initialize once only, so consecutive runs won't fail. if (!Select.Execute("English").Result.HasData) { Insert.Execute("Hello", "World", "English"); Insert.Execute("Bonjour", "Monde", "French"); Insert.Execute("Hola", "Mundo", "Spanish"); Insert.Execute("Hej", "Verden", "Danish"); Insert.Execute("Ciao", "Mondo", "Italian"); } // Get the result and wrap into a strongly-typed single-row table var result = Select.Execute("Spanish").Result.Wrap <string, string>(); // Print out the answer if (result.HasData) { Console.WriteLine("{0}, {1}", result.Column1, result.Column2); } else { Console.WriteLine("I can't say Hello in that language!"); } } } catch (Exception x) { Console.WriteLine(x.ToString()); } }
// Application entry-point static void Main(string[] args) { try { // Read hosts from the command or use defaults string hosts = "10.10.180.176"; if (args.Length > 0) { hosts = string.Join(",", string.Join(",", args).Split(' ', ',')); } // Initialize application state State = new ApplicationState( 32 // Key size (250 maximum) , 1024 // Minimum Value Size (Max 1048576) , 1024 // Maximum Value Size (Max 1048576) , 100000 // Number of Pairs to put in store before benchmark , 0.75 // % of Get (vs Put) during benchmark , ApplicationState.DataEncoding.Gzip // Type of data compression to use for benchmark , 0.5 // Value target compression ratio (only for Gzip and // Deflate: Raw will not compress, and Base64 will // by design cause a +33% payload increase) , 120000 // Benchmark duration ); // Print out introduction message Console.WriteLine( introFormat , line , new TimeSpan(0, 0, 0, 0, State.BenchmarkDuration) , State.KeySize , State.MinValueSize.ToByteSizeString() , State.MaxValueSize.ToByteSizeString() , State.InitialDatasetSize , State.PercentGet * 100 , State.ValueEncoding.ToString() , ( State.ValueEncoding == ApplicationState.DataEncoding.Deflate || State.ValueEncoding == ApplicationState.DataEncoding.Gzip ) ? string.Format("\r\n Approx. Value Compression Rate : {0,15:#0.## %}", State.ValueTargetCompressionRatio) : "" ); // Create & open connection using (VoltConnection voltDB = VoltConnection.Create("hosts=" + hosts + ";statistics=true;adhoc=true;").Open()) { // Define procedures var Initialize = voltDB.Procedures.Wrap <Null, int, int, string, byte[]>("Initialize", null); var Put = voltDB.Procedures.Wrap <Null, string, byte[]>("Put", PutCallback); var Get = voltDB.Procedures.Wrap <Table, string>("Get", GetCallback); // Process variables string key; byte[] value; byte[] compressedValue; // Initialize the data Console.Write("Initializing data store... "); for (int i = 0; i < State.InitialDatasetSize; i += 1000) { // Generate value if (State.MaxValueSize == State.MinValueSize) { value = State.BaseValue; } else { value = new byte[State.Rand.Next(State.MinValueSize, State.MaxValueSize)]; Buffer.BlockCopy(State.BaseValue, 0, value, 0, value.Length); } // Compress as needed switch (State.ValueEncoding) { case ApplicationState.DataEncoding.Gzip: compressedValue = value.Gzip(); break; case ApplicationState.DataEncoding.Deflate: compressedValue = value.Deflate(); break; default: compressedValue = value; break; } Initialize.Execute(i, Math.Min(i + 1000, State.InitialDatasetSize), "K%1$#" + (State.KeySize - 1) + "s", value); } Console.WriteLine(" Done."); // Initialize ticker Timer ticker = new Timer(delegate(object state) { Console.WriteLine("{0,24}", string.Join("\r\n", (state as VoltConnection).Statistics.GetStatisticsSummaryByNode(StatisticsSnapshot.SnapshotOnly).Select(r => r.Key.ToString() + " :: " + r.Value.ToString(StatisticsFormat.Short)).ToArray())); }, voltDB, 5000, 5000); // Now benchmark GET/PUT operations. int end = Environment.TickCount + State.BenchmarkDuration; Console.WriteLine("{0}\r\nRunning GET/PUT benchmark\r\n{0}", line); while (Environment.TickCount < end) { // Get a random key to act upon. key = State.Rand.Next(State.InitialDatasetSize).ToString().PadLeft(State.KeySize, 'k'); // Decide whether to do a Get/Put if (State.Rand.NextDouble() <= State.PercentGet) { Get.BeginExecute(key); } else { // Generate value if (State.MaxValueSize == State.MinValueSize) { value = State.BaseValue; } else { value = new byte[State.Rand.Next(State.MinValueSize, State.MaxValueSize)]; Buffer.BlockCopy(State.BaseValue, 0, value, 0, value.Length); } // Compress as needed switch (State.ValueEncoding) { case ApplicationState.DataEncoding.Gzip: compressedValue = value.Gzip(); break; case ApplicationState.DataEncoding.Deflate: compressedValue = value.Deflate(); break; default: compressedValue = value; break; } // Push PUT call to server. Note: Tracking ONLY performed upon successful completion of the call Put.BeginExecute(key, compressedValue, new SizeInfo(compressedValue.LongLength, value.LongLength)); } } // Drain connection voltDB.Drain(); // Dispose of ticket ticker.Dispose(); Console.WriteLine("{0}\r\nConnection Statistics by Procedure.\r\n{0}", line); // Print out statistics by procedure Console.WriteLine(string.Join("\r\n", voltDB.Statistics.GetStatistics(StatisticsSnapshot.SnapshotOnly).Select(i => string.Format("{0,8} :: {1}", i.Key, i.Value.ToString(StatisticsFormat.Short))).ToArray())); Console.WriteLine("{0}\r\nConnection Statistics.\r\n{0}", line); // Print out statistics and reset (though pointless, since we'll close the connection next!) Console.WriteLine(voltDB.Statistics.GetStatisticsSummary(StatisticsSnapshot.SnapshotAndResetWithIgnorePendingExecutions).ToString()); Console.WriteLine("{0}\r\nApplication Statistics.\r\n{0}", line); // Print out application statistics Console.WriteLine(resultFormat , "GET" , State.GetCount , State.GetBytesUncompressed.ToByteSizeString() , State.GetBytesCompressed.ToByteSizeString() , (double)(State.GetBytesUncompressed - State.GetBytesCompressed) / (double)State.GetBytesUncompressed , ((double)State.GetBytesUncompressed / (double)State.GetCount).ToByteSizeString() , ((double)State.GetBytesCompressed / (double)State.GetCount).ToByteSizeString() ); Console.WriteLine(resultFormat , "PUT" , State.PutCount , State.PutBytesUncompressed.ToByteSizeString() , State.PutBytesCompressed.ToByteSizeString() , (double)(State.PutBytesUncompressed - State.PutBytesCompressed) / (double)State.PutBytesUncompressed , ((double)State.PutBytesUncompressed / (double)State.PutCount).ToByteSizeString() , ((double)State.PutBytesCompressed / (double)State.PutCount).ToByteSizeString() ); } } catch (Exception x) { // You'll want to be more elaborate than having a global catch block! Console.WriteLine(x.ToString()); } }
/// <summary> /// This is the entry point for the sample application /// </summary> /// <param name="args">Command line arguments - you may pass a comma or space-separated list of hosts to be /// used (otherwise the application's default will be used)</param> static void NoviceMain(string[] args) { try { // Default hosts string hosts = "192.168.1.203,192.168.1.200"; // If arguments were given, assume it's a host list. I don't know if you space or comma separated them, // so I clean all that up if (args.Length > 0) { hosts = string.Join(",", string.Join(",", args).Split(' ', ',')); } // Initialize some variables for the process Random rand = new Random(); long phoneNumber = 0; sbyte contestantId = 0; int maximumAllowedVotesPerPhoneNumber = 3; // A little introduction message Console.WriteLine("Voter Benchmark (2 minutes)\r\n---------------------------------------------------" + "-------------------------------------------------"); // You create a connection by calling the VoltConnection.GetConnection static factory method. // You may pass a ConnectionSettings object or a connectionstring - the conversion is implicit and the // ConnectionSettings object is fully compatible with the standard ConnectionStringBuilder class from // which it actually derives. // Through action-chaining (Open, Drain, Close and ResetStatistics all return the acting instance // instead of 'void', allowing LINQ-like sequencing of actions), you can also immediately open the // connection after getting it, which we do here. VoltConnection voltDB = VoltConnection.Create("hosts=" + hosts + ";statistics=true") .Open(); // Create strongly-typed procedure wrappers for each procedure we will use. // - Voter will have a callback to track procedure results asynchronously. // - Initialize and Results don't because we will simply call them synchronously. var Vote = voltDB.Procedures.Wrap <long, long, sbyte, int>("Vote", VoterCallback); var Initialize = voltDB.Procedures.Wrap <long, int, string>("Initialize", null); var Results = voltDB.Procedures.Wrap <Table>("Results", null); // Initialize the catalog and request a random number of contestants to vote on. // Notice the result is strongly-typed, so we can access it directly! int numContestants = (int)Initialize.Execute( rand.Next(5, 10) , "Edwina Burnam,Tabatha Gehling,Kelly Clauss,Jessie Alloway,Alana Bregman,Jessie Eichman,Allie Rogalski,Nita Coster,Kurt Walser,Ericka Dieter,Loraine Nygren,Tania Mattioli" ).Result; // Print a startup message. Console.WriteLine( "Voting for {0} Contestants\r\nTracking Live Statistics (Connection Aggregates)\r\n" + "----------------------------------------------------------------------------------" + "------------------" , numContestants); // Start a display timer that will "tick" every 5 seconds to print live performance data. We don't // really need a context here, so we'll just use an anonymous delegate for the callback. Timer ticker = new Timer( delegate(object state) { Console.WriteLine("{0,24}" , string.Join( "\r\n" , (state as VoltConnection) .Statistics .GetLifetimeStatisticsByNode() .Select(r => r.Key.ToString() + " :: " + r.Value.ToString(StatisticsFormat.Short) ) .ToArray() ) ); } , voltDB // Pass the connection , 5000 // Start in 5 seconds , 5000 // Tick every 5 seconds ); // Run for 2 minutes. Use the environment's ticks to count time - not very precise but that's not // where it matters, and it's low-cost. int end = Environment.TickCount + 120000; while (Environment.TickCount < end) { // Get a random phone number. phoneNumber = 2000000000L + (long)(rand.NextDouble() * 9999999999L); contestantId = unchecked ((sbyte)rand.Next(0, numContestants)); // Randomly introduce some bad votes (0.1%) if (rand.Next(10000) < 10) { contestantId = 11; } // Randomly introduce vote abuse (0.05%) by calling with the same phone number more than the // maximum allowed. // Note how we call the BeginExecute method on our procedure wrapper - this is all it takes. Our // callback will be executed upon completion. // One final note: this could fail: not because of a failure on the server (this is the role of // exception-packaging in the response), but if you lost connectivity and your connection is now // closed! // This means you should use a try/catch block and adequate handling, or leverage the .TryExecute // & .TryExecuteAsync methods on your Procedure Wrapper. // Here we have a global try/catch block that's good enough for a sample app, but vastly inadequate // for a production client, of course! if (rand.Next(20000) < 10) { for (int i = 0; i < 5; i++) { Vote.BeginExecute( phoneNumber , unchecked ((sbyte)rand.Next(0, numContestants)) , maximumAllowedVotesPerPhoneNumber ); } } else { Vote.BeginExecute( phoneNumber , contestantId , maximumAllowedVotesPerPhoneNumber ); } } // Drain the connection until all the work is done. voltDB.Drain(); // Stop and dispose of the ticker - we're done here. ticker.Dispose(); // Get the vote results (who's the winner, etc...). Table result = Results.Execute().Result; // And close the connection. voltDB.Close(); // Enjoy the fully LINQ-compatible Table object: string resultDisplay = string.Join( "\r\n" , result.Rows .Select(r => string.Format( "{0,21} => {1,12:##,#} votes(s)" , r.GetValue <string>(0) , r.GetValue <long?>(2) ) ).ToArray() ) + string.Format( "\r\n\r\n {0,21} was the winner!\r\n" , result.Rows .OrderByDescending(r => r.GetValue <long?>(2)) .First() .GetValue <string>(0) ); // Print out the results, performance stats, etc. A fair bit of fancy LINQ there - not the easiest to // read, but it's good learning material :-) // You could of course do that with loops and 50 lines, but I prefer simple one-liners... Console.Write( ResultFormat , resultDisplay , VoteResults[0] , VoteResults[1] , VoteResults[2] , VoteResults[3] // Get the lifetime statistics for the connection. , string.Format("{0,21} :: {1}", "Connection" , voltDB.Statistics.GetLifetimeStatistics().ToString(StatisticsFormat.Short)) // Get lifetime statistics by node. , string.Join("\r\n" , voltDB.Statistics.GetLifetimeStatisticsByNode() .Select(p => string.Format("{0,21} :: {1}" , p.Key , p.Value.ToString(StatisticsFormat.Short) ) ) .ToArray() ) // Statistics by procedure, across all nodes. , string.Join("\r\n" , voltDB.Statistics.GetStatistics(StatisticsSnapshot.SnapshotOnly) .Select(p => string.Format("{0,21} :: {1}" , p.Key , p.Value.ToString(StatisticsFormat.Short) ) ) .ToArray() ) // And lifetime statistics again, but with the detailed latency information. , voltDB.Statistics.GetLifetimeStatistics().ToString(StatisticsFormat.Default) ); } catch (Exception x) { // You'll want to be more elaborate than having a global catch block! // Might happen if: failed to connect, lose connection during the test (wrap a try/catch around the // Vote.BeginExecute call, or use the Vote.TryExecuteAsync call to test the execution was launched // successfully! Console.WriteLine(x.ToString()); } }
// Application entry-point static void Main(string[] args) { try { // Read hosts from the command or use defaults string hosts = "192.168.1.203"; if (args.Length > 0) { hosts = string.Join(",", string.Join(",", args).Split(' ', ',')); } // Initialize some variables for the process Random rand = new Random(); long phoneNumber = 0; sbyte contestantId = 0; int maximumAllowedVotesPerPhoneNumber = 3; Console.WriteLine("Voter Benchmark (2 minutes)\r\n----------------------------------------------------------------------------------------------------"); // Create & open connection VoltConnection voltDB = VoltConnection.Create("hosts=" + hosts + ";statistics=true;").Open(); // Create strongly-typed procedure wrappers var Vote = voltDB.Procedures.Wrap <long, long, sbyte, int>("Vote", VoterCallback); var Initialize = voltDB.Procedures.Wrap <long, int, string>("Initialize", null); var Results = voltDB.Procedures.Wrap <Table>("Results", null); // Initialize application int numContestants = (int)Initialize.Execute(rand.Next(5, 10), "Edwina Burnam,Tabatha Gehling,Kelly Clauss,Jessie Alloway,Alana Bregman,Jessie Eichman,Allie Rogalski,Nita Coster,Kurt Walser,Ericka Dieter,Loraine Nygren,Tania Mattioli").Result; Console.WriteLine("Voting for {0} Contestants\r\nTracking Live Statistics (Connection Aggregates)\r\n----------------------------------------------------------------------------------------------------", numContestants); // Start display ticker for stats Timer ticker = new Timer(delegate(object state) { Console.WriteLine("{0,24}", string.Join("\r\n", (state as VoltConnection).Statistics.GetLifetimeStatisticsByNode().Select(r => r.Key.ToString() + " :: " + r.Value.ToString(StatisticsFormat.Short)).ToArray())); }, voltDB, 5000, 5000); /* * int workerThreads; * int completionPortThreads; * ThreadPool.GetMinThreads(out workerThreads, out completionPortThreads); * ThreadPool.SetMaxThreads(workerThreads*2, completionPortThreads*2); */ // Run for 2 minutes. int end = Environment.TickCount + 120000; while (Environment.TickCount < end) { phoneNumber = 2000000000L + (long)(rand.NextDouble() * 9999999999L); contestantId = unchecked ((sbyte)rand.Next(0, numContestants)); if (rand.Next(10000) < 10) { contestantId = 11; } if (rand.Next(20000) < 10) { for (int i = 0; i < 5; i++) { Vote.BeginExecute(phoneNumber, unchecked ((sbyte)rand.Next(0, numContestants)), maximumAllowedVotesPerPhoneNumber); } } else { Vote.BeginExecute(phoneNumber, contestantId, maximumAllowedVotesPerPhoneNumber); } } // Finalize operations and get vote results voltDB.Drain(); ticker.Dispose(); // Something new compared to the "Novice" version: you don't have to constantly when accessing data: // use .GetValue<type>(columnIndex) - simply wrap your table into a strongly-typed accessor and things // become a lot easier! var resultData = Results.Execute().Result.Wrap <string, int?, long?>(); voltDB.Close(); // Compile results & print summary string resultDisplay = string.Join("\r\n", resultData.Rows.Select(r => string.Format("{0,21} => {1,12:##,#} votes(s)", r.Column1, r.Column3)).ToArray()) + string.Format("\r\n\r\n {0,21} was the winner!\r\n", resultData.Rows.OrderByDescending(r => r.Column3).First().Column1); Console.Write(ResultFormat, resultDisplay, VoteResults[0], VoteResults[1], VoteResults[2], VoteResults[3] , string.Format("{0,21} :: {1}", "Connection", voltDB.Statistics.GetLifetimeStatistics().ToString(StatisticsFormat.Short)) , string.Join("\r\n", voltDB.Statistics.GetLifetimeStatisticsByNode().Select(p => string.Format("{0,21} :: {1}", p.Key, p.Value.ToString(StatisticsFormat.Short))).ToArray()) , string.Join("\r\n", voltDB.Statistics.GetStatistics(StatisticsSnapshot.SnapshotOnly).Select(p => string.Format("{0,21} :: {1}", p.Key, p.Value.ToString(StatisticsFormat.Short))).ToArray()) , voltDB.Statistics.GetLifetimeStatistics().ToString(StatisticsFormat.Default) , line ); } catch (Exception x) { // You'll want to be more elaborate than having a global catch block! Console.WriteLine(x.ToString()); } }