Exemple #1
0
        /// <summary>
        /// Invokes the Avalara for Communications REST API to process the sample request.
        /// </summary>
        private async Task ProcessSampleRequest()
        {
            // Get client for invoking Avalara for Communications REST API
            AfcRestClient client = await GetAfcRestClient();

            if (client == null)
            {
                return;
            }

            // Get request from input
            CalcTaxesRequest request = GetSampleRequest();

            if (request == null)
            {
                return;
            }

            try
            {
                CalcTaxesResponse response = await client.CalcTaxes(request);

                responseTextBox.Text = Newtonsoft.Json.JsonConvert.SerializeObject(response, Newtonsoft.Json.Formatting.Indented);
            }
            catch (AggregateException ex)
            {
                MessageBox.Show(this, ex.InnerException?.Message ?? ex.Message, "Error Processing Sample Request",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error Processing Sample Request", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                client.Dispose();
            }
        }
Exemple #2
0
        /// <summary>
        /// Begins running the performance test for the Avalara for Communications REST service.
        /// </summary>
        private async Task StartPerformanceTest()
        {
            ClearPerformanceStats();

            // Get client for invoking Avalara for Communications REST API
            AfcRestClient afcClient = await GetAfcRestClient();

            if (afcClient == null)
            {
                EndPerformanceTest(false);
                return;
            }

            // Get request from input
            performanceTestRequest = GetSampleRequest();

            if (performanceTestRequest == null)
            {
                EndPerformanceTest(false);
                return;
            }

            try
            {
                // Process request once to validate request body
                await afcClient.CalcTaxes(performanceTestRequest);

                maxThreads = int.Parse(maxThreadsComboBox.Text);
                maxMinutes = int.Parse(maxTimeComboBox.Text);

                // Create tasks for the specified number of threads
                var tasks = new List <Task>();

                for (int threadCount = 0; threadCount < maxThreads; threadCount++)
                {
                    tasks.Add(new Task(async() =>
                    {
                        AfcRestClient client = afcClient;
                        Stopwatch stopwatch  = new Stopwatch();

                        try
                        {
                            while (!cancellationToken.IsCancellationRequested)
                            {
                                stopwatch.Restart();
                                var response = await client.CalcTaxes(performanceTestRequest);
                                stopwatch.Stop();
                                responseStats.Enqueue((int)stopwatch.ElapsedMilliseconds);
                                int taxCount = response.InvoiceResults?.Sum(inv => inv.ItemResults?.Sum(item => item.Taxes?.Count() ?? 0) ?? 0) ?? 0;
                                Interlocked.Increment(ref requestCount);
                                Interlocked.Add(ref taxesReturned, taxCount);
                            }
                        }
                        catch
                        {
                            Interlocked.Increment(ref failedRequests);
                        }
                    }));
                }

                // Begin making calls to Avalara for Communications REST API
                startTime = DateTime.UtcNow;
                tasks.ForEach(t => t.Start());
            }
            catch
            {
                afcClient.Dispose();
            }
        }