new_contents: from __future__ import absolute_import, division, print_function
  from six.moves import range
  
  import os
  import sys
  
  import pandas
  
  from argparse import ArgumentParser
  from numpy import cbrt, floor, sqrt
  from mpi4py import MPI
  
  from firedrake.petsc import PETSc
  from firedrake import COMM_WORLD, ExtrudedMesh, UnitSquareMesh, assemble
  
  import form
  
  
  parser = ArgumentParser(description="""Profile assembly""", add_help=False)
  
  parser.add_argument("--mode", action="store", default="spectral",
                      help="Which TSFC mode to use?")
  
  parser.add_argument("--output-file", action="store",
                      default="assembly.csv",
                      help="Where to put the results")
  
  parser.add_argument("--overwrite", action="store_true", default=False,
                      help="Overwrite existing output?  Default is to append.")
  
  parser.add_argument("--help", action="store_true",
                      help="Show help")
  
  args, _ = parser.parse_known_args()
  
  if args.help:
      help = parser.format_help()
      PETSc.Sys.Print("%s\n" % help)
      sys.exit(0)
  
  filepath = os.path.abspath(args.output_file)
  
  
  PETSc.Log.begin()
  
  parloop_event = PETSc.Log.Event("ParLoopExecute")
  assemble_event = PETSc.Log.Event("AssembleMat")
  
  
  simplex_range = list(range(1, 5))
  if args.mode == "coffee":
      cube_range = list(range(1, 10))
  elif args.mode == "spectral":
      cube_range = list(range(1, 16))
  
  
  test_cases = [
      (form.stokes_momentum, False),
      (form.elasticity, False),
      (form.poisson, True),
      (form.mass_gll, True),
      (form.poisson_gll, True),
      (form.hyperelasticity, True),
      (form.curl_curl, True),
  ]
  for problem, tensor in test_cases:
      formname = problem.__name__
      if problem == form.curl_curl:
          degrees = list(range(1, 12))
      elif tensor:
          degrees = cube_range
      else:
          degrees = simplex_range
      for degree in degrees:
          cellname = 'cube' if tensor else 'simplex'
          PETSc.Sys.Print("%s: %s, degree=%d" % (formname, cellname, degree))
          num_cells = COMM_WORLD.size * max(1e5 / degree**4, 1e8 / (degree + 1)**9)
          num_cells = max(COMM_WORLD.size, min((1<<27) / (degree + 1)**6, num_cells))
          h = int(floor(cbrt(num_cells / COMM_WORLD.size)))
          w = int(floor(sqrt(num_cells / h)))
          d = int(round(num_cells / (w * h)))
          num_cells = w * d * h
  
          mesh = ExtrudedMesh(UnitSquareMesh(w, d, quadrilateral=tensor), h)
          comm = mesh.comm
          J = problem(mesh, int(degree))
  
          # Warmup and allocate
          A = assemble(J, form_compiler_parameters={'mode': args.mode})
          A.force_evaluation()
          Ap = A.petscmat
          x_size, y_size = Ap.getSize()
          assert x_size == y_size
          num_dofs = x_size
          stage = PETSc.Log.Stage("%s(%d) %s" % (formname, degree, cellname))
          with stage:
              with assemble_event:
                  assemble(J, form_compiler_parameters={'mode': args.mode}, tensor=A)
                  A.force_evaluation()
  
              parloop = parloop_event.getPerfInfo()
              assembly = assemble_event.getPerfInfo()
              assert parloop["count"] == 1
              assert assembly["count"] == 1
              parloop_time = comm.allreduce(parloop["time"], op=MPI.SUM) / comm.size
              assemble_time = comm.allreduce(assembly["time"], op=MPI.SUM) / comm.size
  
              assembly_overhead = (1 - parloop_time / assemble_time)
              PETSc.Sys.Print("Assembly overhead: %.1f%%" % (assembly_overhead * 100,))
  
          if COMM_WORLD.rank == 0:
              if args.overwrite:
                  mode = "w"
                  header = True
              else:
                  mode = "a"
                  header = not os.path.exists(filepath)
  
              data = {"num_cells": num_cells,
                      "num_dofs": num_dofs,
                      "num_procs": comm.size,
                      "tsfc_mode": args.mode,
                      "problem": formname,
                      "cell_type": cellname,
                      "degree": degree,
                      "assemble_time": assemble_time,
                      "parloop_time": parloop_time}
              df = pandas.DataFrame(data, index=[0])
              df.to_csv(filepath, index=False, mode=mode, header=header)
  
subject: add measure-assembly.py