Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
TBench - Form1.vb

Form1.vb

Caricato da: Totem
Scarica il programma completo

  1. Imports TBench.TBench
  2. Imports System.Reflection
  3. Imports System.Runtime.InteropServices
  4. Imports System.CodeDom
  5. Imports System.ComponentModel
  6. Public Class Form1
  7.     Private Open As New OpenFileDialog
  8.     Private TestMethods As New Dictionary(Of String, MethodInfo)
  9.  
  10.     Private Sub PopulateArgumentList(ByVal GridItem As GridItem)
  11.         Dim Name As String = GridItem.Label
  12.         Dim Method As MethodInfo = Me.TestMethods(Name)
  13.         Dim Item As ListViewItem
  14.         Dim ParType As New System.Text.StringBuilder
  15.  
  16.         lstArguments.Items.Clear()
  17.         For Each PI As ParameterInfo In Method.GetParameters
  18.             ParType.Remove(0, ParType.Length)
  19.             Item = New ListViewItem
  20.             Item.SubItems(0).Text = PI.Name
  21.             Item.SubItems.Add(PI.ParameterType.FullName)
  22.             If PI.IsOptional Then
  23.                 ParType.Append("Optional ")
  24.             End If
  25.             If PI.ParameterType.IsByRef Then
  26.                 ParType.Append("ByRef")
  27.             Else
  28.                 ParType.AppendFormat("ByVal")
  29.             End If
  30.             Item.SubItems.Add(ParType.ToString)
  31.             lstArguments.Items.Add(Item)
  32.         Next
  33.     End Sub
  34.  
  35.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  36.         Open.Filter = "Assembly|*.dll;*.exe"
  37.         CodeGenerator.LoadExpandable()
  38.     End Sub
  39.  
  40.     Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
  41.         If Open.ShowDialog = Windows.Forms.DialogResult.OK Then
  42.             txtAssembly.Text = Open.FileName
  43.         End If
  44.     End Sub
  45.  
  46.     Private Sub btnAnalyze_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnalyze.Click
  47.         If String.IsNullOrEmpty(txtAssembly.Text) OrElse (Not IO.File.Exists(txtAssembly.Text)) Then
  48.             MessageBox.Show("Inserire un percorso valido per l'assembly!", "TBench", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  49.             Exit Sub
  50.         End If
  51.  
  52.         Dim M As New TBenchManager(txtAssembly.Text)
  53.         Dim BMethods As List(Of MethodInfo)
  54.         Dim Item As ListViewItem
  55.         Dim Attr As TBenchAttribute
  56.  
  57.         BMethods = M.GetBenchMethods
  58.         lstAllMethods.Items.Clear()
  59.  
  60.         For Each MI As MethodInfo In BMethods
  61.             Attr = MI.GetCustomAttributes(GetType(TBenchAttribute), False)(0)
  62.             Item = New ListViewItem
  63.             Item.SubItems(0).Text = MI.Name
  64.             Item.SubItems.Add(Attr.Name)
  65.             Item.SubItems.Add(Attr.Group)
  66.             Item.SubItems.Add(MI.DeclaringType.FullName)
  67.             Item.Checked = True
  68.             Item.Tag = MI
  69.             lstAllMethods.Items.Add(Item)
  70.         Next
  71.     End Sub
  72.  
  73.     Private Sub btnArguments_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnArguments.Click
  74.         Dim Code As New System.Text.StringBuilder
  75.         Dim MI As MethodInfo
  76.         Dim Arguments() As ParameterInfo
  77.         Dim Attr As TBenchAttribute
  78.  
  79.         Code.AppendLine("Imports System")
  80.         Code.AppendLine("Imports System.ComponentModel")
  81.         Code.AppendLine(CodeGenerator.UniversalConverter)
  82.         Code.AppendLine("Public Class Arguments")
  83.         TestMethods.Clear()
  84.  
  85.         For Each Item As ListViewItem In lstAllMethods.CheckedItems
  86.             MI = Item.Tag
  87.             Arguments = MI.GetParameters
  88.             TestMethods.Add(MI.Name, MI)
  89.  
  90.             If Arguments IsNot Nothing AndAlso Arguments.Length > 0 Then
  91.                 CodeGenerator.CodeIndent = 8
  92.                 Code.AppendFormat("    Public Class Type_{0}{1}", MI.Name, vbCrLf)
  93.                 For Each PI As ParameterInfo In Arguments
  94.                     If Not PI.ParameterType.FullName.Contains("System") Then
  95.                         MessageBox.Show(MI.Name & " richiede dei parametri non appartenenti al Framework .Net. Impossibile proseguire!", "TBench", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  96.                         Exit Sub
  97.                     End If
  98.                     'Code.Append(Me.GetFieldText(PI.Name, PI.ParameterType.FullName, 8))
  99.                     'Code.Append(Me.GetPropertyText(PI.Name, PI.ParameterType.FullName, "Parametri", "Inserire un valore di tipo " & PI.ParameterType.Name & ".", 8, False))
  100.                     Code.Append(CodeGenerator.GetFieldText(PI))
  101.                     Code.Append(CodeGenerator.GetPropertyText(PI))
  102.                 Next
  103.                 Code.AppendFormat("    End Class{0}{0}", vbCrLf)
  104.                 CodeGenerator.CodeIndent = 4
  105.                 'Code.Append(Me.GetExpandable(ClassType, Arguments, 4))
  106.                 'Code.Append(Me.GetFieldText(MI.Name, ClassType, 4))
  107.                 'Code.Append(Me.GetPropertyText(MI.Name, ClassType, Attr.Group, Description, 4))
  108.                 Code.Append(CodeGenerator.GetExpandable(MI))
  109.                 Code.Append(CodeGenerator.GetFieldText(MI))
  110.                 Code.Append(CodeGenerator.GetPropertyText(MI))
  111.             Else
  112.                 Code.AppendFormat("    Public Class Type_{0}{1}", MI.Name, vbCrLf)
  113.                 Code.AppendFormat("        'Vuoto{0}", vbCrLf)
  114.                 Code.AppendFormat("    End Class{0}", vbCrLf)
  115.                 CodeGenerator.CodeIndent = 4
  116.                 'Code.Append(Me.GetFieldText(MI.Name, ClassType, 4))
  117.                 'Code.Append(Me.GetPropertyText(MI.Name, ClassType, Attr.Group, Description, 4, False))
  118.                 Code.Append(CodeGenerator.GetFieldText(MI))
  119.                 Code.Append(CodeGenerator.GetPropertyText(MI))
  120.             End If
  121.         Next
  122.         Code.AppendLine("End Class")
  123.  
  124.         Dim Params As New Compiler.CompilerParameters
  125.         Dim CodeProvider As New VBCodeProvider
  126.         Dim Result As Compiler.CompilerResults
  127.         Dim Asm As Assembly
  128.  
  129.         Params.GenerateExecutable = False
  130.         Params.ReferencedAssemblies.Add("System.dll")
  131.         Params.ReferencedAssemblies.Add("System.Xml.dll")
  132.         Result = CodeProvider.CompileAssemblyFromSource(Params, Code.ToString)
  133.         IO.File.WriteAllText(Application.StartupPath & "\Codice.vb", Code.ToString)
  134.         If Result.Errors.Count > 0 Then
  135.             Dim Errors As New System.Text.StringBuilder
  136.             For Each [Error] As Compiler.CompilerError In Result.Errors
  137.                 Errors.AppendFormat("Linea: {0}{1}Messaggio: {2}{1}{1}", [Error].Line, vbCrLf, [Error].ErrorText)
  138.             Next
  139.             IO.File.WriteAllText(Application.StartupPath & "\Errors.txt", Errors.ToString)
  140.             MessageBox.Show("Si sono verificati errori nel codice generato. E' probabile che uno dei parametri analizzati non sia convertibile in stringa. Consultare il file Errors.txt per maggiori informazioni.", "TBench", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  141.             Exit Sub
  142.         End If
  143.         Asm = Result.CompiledAssembly
  144.  
  145.         CodeProvider.Dispose()
  146.  
  147.         Dim Arg As Object = Asm.CreateInstance("Arguments")
  148.         pgArguments.SelectedObject = Arg
  149.  
  150.         btnTest.Enabled = True
  151.     End Sub
  152.  
  153.     Private Sub pgArguments_SelectedGridItemChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.SelectedGridItemChangedEventArgs) Handles pgArguments.SelectedGridItemChanged
  154.         If e.NewSelection.Expandable Then
  155.             Me.PopulateArgumentList(e.NewSelection)
  156.         Else
  157.             If e.NewSelection.Parent IsNot Nothing Then
  158.                 '...
  159.             Else
  160.                 lstArguments.Items.Clear()
  161.             End If
  162.         End If
  163.     End Sub
  164.  
  165.     Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
  166.         If TestMethods.Count = 0 Then
  167.             MessageBox.Show("Nessun metodo selezionato!", "TBench", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  168.             Exit Sub
  169.         End If
  170.  
  171.         Dim Temp As New Dictionary(Of MethodInfo, List(Of Object))
  172.  
  173.         For Each PI As PropertyInfo In pgArguments.SelectedObject.GetType.GetProperties
  174.             'Proprietà -> Metodo
  175.             Dim Obj As Object = PI.GetValue(pgArguments.SelectedObject, Nothing)
  176.             Dim Objs As New List(Of Object)
  177.             'Proprietà -> Parametro del metodo
  178.             For Each ObjPI As PropertyInfo In Obj.GetType.GetProperties
  179.                 Objs.Add(ObjPI.GetValue(Obj, Nothing))
  180.             Next
  181.             Temp.Add(TestMethods(PI.Name), Objs)
  182.         Next
  183.  
  184.         Dim Result As BenchReport
  185.         Dim M As New TBenchManager()
  186.  
  187.         Try
  188.             Result = M.Bench(Temp, 1)
  189.         Catch Ex As Exception
  190.             MessageBox.Show("Si è verificato un errore nel testing. Probabilmente non hai assegnato tutti i valori correttamente ai parametri dei metodi da testare.", "TBench", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  191.             Exit Sub
  192.         End Try
  193.  
  194.         lblResult.Text = String.Format("Test di benchmarking completato: {0} metodi testati in {1} secondi.", Result.TotalMethods, Result.TotalTime)
  195.  
  196.         tabResults.TabPages.Clear()
  197.         For Each Group As GroupResult In Result.Groups
  198.             Dim Tab As New TabPage
  199.             Dim List As New ListView
  200.  
  201.             Group.MakeComparison()
  202.             List.View = View.Details
  203.             List.FullRowSelect = True
  204.             List.GridLines = True
  205.             List.Dock = DockStyle.Fill
  206.             List.Columns.Add("Nome / Alias", 150)
  207.             List.Columns.Add("Tempo impiegato", 120)
  208.             List.Columns.Add("Comparazione", 150)
  209.             AddHandler List.ColumnClick, AddressOf ResultLists_ColumnClick
  210.  
  211.             For Each Method As MethodResult In Group
  212.                 List.Items.Add(New ListViewItem(New String() {Method.Name, String.Format("{0:N0} ms", Method.Time.Value), Method.Comparation}))
  213.             Next
  214.  
  215.             Tab.Text = Group.Name
  216.             Tab.Controls.Add(List)
  217.             tabResults.TabPages.Add(Tab)
  218.         Next
  219.     End Sub
  220.  
  221.     Private Sub ResultLists_ColumnClick(ByVal sender As Object, ByVal e As ColumnClickEventArgs)
  222.         Select Case e.Column
  223.             Case 0
  224.                 DirectCast(sender, ListView).Sort()
  225.         End Select
  226.     End Sub
  227. End Class